Summary of Two Types of Type Conversion in C: Understanding Implicit and Explicit Conversion

“From today onwards, study hard and make progress every day”

Repetition is the best method for memory; spend one minute each day to remember the basics of C language.

“C Language Beginner’s Essential Knowledge Notes Series of 100 Articles”

24. Summary of Two Types of Type Conversion: Understanding Implicit and Explicit Conversion

1. Basic Concept of Type Conversion

Type conversion refers to the process of converting data from one type to another. The C language supports two types of conversion:

  1. 1. Implicit Conversion (Automatic Conversion): Performed automatically by the compiler
  2. 2. Explicit Conversion (Forced Conversion): Specified explicitly by the programmer

2. Rules of Implicit Conversion

1. Implicit Conversion in Arithmetic Operations (Type Promotion)

int + float → float
short + int → int
char + long → long

2. Implicit Conversion during Assignment

float f = 5;      // int→float
int i = 3.14;     // float→int (truncating decimal)

3. Conversion during Function Calls

void func(double);
func(2);          // int→double

3. Syntax of Explicit Conversion

Use the casting operator:

(target type) expression

Example:

double d = 3.14;
int i = (int)d;    // Explicitly convert to int

4. Analysis of Common Conversion Scenarios

1. Conversion between Floating Point and Integer

float f = 2.718;
int i = (int)f;     // i=2 (truncating)
double d = i;       // d=2.0

2. Pointer Type Conversion

int x = 65;
char *p = (char *)&x;  // Access a single byte of x

3. Sign Extension Issues

char c = -5;
unsigned int u = (unsigned int)c;  // 0xFFFFFFFB

5. Common Issues with Type Conversion

1. Loss of Precision

long l = 1234567890123;
int i = (int)l;    // Possible overflow

2. Misuse of Pointers

float *fp = (float *)malloc(sizeof(int));  // Dangerous!

3. Sign Bit Extension

char c = 0xFF;     // -1
int i = c;         // i=0xFFFFFFFF (-1)
unsigned u = c;     // u=0xFFFFFFFF (large integer)

6. Summary of Advanced Usage (Essential for Experts)

1. Function Pointer Conversion

// Function pointer, pointing to a function with no parameters and returning void
void (*func_ptr)() = (void(*)())other_ptr;

2. Conversion between Two Different Types of Structures

struct A { int x; };
struct B { int y; };
struct A a;
struct B b = *(struct B *)&a;

Some students have contacted me, wanting to have a study exchange group. Previously, I was concerned about advertisements, so I didn’t create a group. Considering that having a group is indeed convenient, I will try to create one this time.

If you need it, hurry up and join; the validity period is 7 days.

Summary of Two Types of Type Conversion in C: Understanding Implicit and Explicit Conversion

———- End ———-

[Special Statement: All articles in this public account are original or authorized by the author, please feel free to consume, and the views are for learning reference only~~]

Summary of Two Types of Type Conversion in C: Understanding Implicit and Explicit Conversion

Summary of Two Types of Type Conversion in C: Understanding Implicit and Explicit Conversion

“If you like C, please like it”Summary of Two Types of Type Conversion in C: Understanding Implicit and Explicit Conversion Click the bottom right corner to seeSummary of Two Types of Type Conversion in C: Understanding Implicit and Explicit Conversion

Leave a Comment