Type Conversion in C
What is Type Conversion?
Type conversion is the process of changing a variable from one data type to another.
This is important because operations in C often involve variables of different types, and the compiler must standardize them before performing calculations.
Implicit Conversion (Type Promotion)
C performs implicit conversions to a common type in expressions. First, the *integer promotions* apply (e.g., `char`/`short` → `int` or `unsigned int`). Then the *usual arithmetic conversions* choose a common real type: `long double` > `double` > `float` (note: in expressions, `float` operands are promoted to `double`).
Example: if you add an `int` and a `double`, the `int` is converted to `double` before the operation.
#include <stdio.h>
int main(void) {
int x = 5;
double y = 2.5;
double result = x + y; // x converted to double
printf("%.1f\n", result);
return 0;
}
7.5
Explicit Conversion (Casting)
You can manually convert one type to another using casting. This is done with the syntax `(type) value`.
Casting is useful when you want to control precision or discard fractions.
#include <stdio.h>
int main(void) {
double pi = 3.14;
int x = (int)pi; // explicitly cast to int
printf("%d\n", x);
return 0;
}
3
Type Conversion in Expressions
When multiple data types appear in an expression, C converts operands to a common type: apply integer promotions, then use the usual arithmetic conversions. For floating types, `float` is promoted to `double` in expressions; if one operand is `double`, the other is converted to `double`, etc.
Example: `int a = 3; float b = 2.5f; double c = a + b;` → `a` and `b` are both converted to `double`, and the result is `double`.
#include <stdio.h>
int main(void) {
int a = 3; float b = 2.5f; double c = a + b;
printf("c = %.1f\n", c);
return 0;
}
c = 5.5
Common Pitfall: Integer Division
If both operands of `/` are integers, the result is integer division (fraction is discarded) before any assignment or further conversion. Cast at least one operand to get floating-point division.
#include <stdio.h>
int main(void) {
int m = 7, n = 2;
double r1 = m / n; // integer division → 3, then converted to 3.0
double r2 = (double)m / n; // 7.0 / 2 → 3.5
printf("r1=%.1f r2=%.1f\n", r1, r2);
return 0;
}
r1=3.0 r2=3.5
Signed vs Unsigned Mixing
When a signed and an unsigned integer are combined, the signed value may be converted to unsigned, which can yield surprising results if the signed value is negative.
#include <stdio.h>
int main(void) {
int a = -1;
unsigned int b = 1u;
printf("a < b? %d\n", a < b); // often prints 0 because a converts to a large unsigned value
return 0;
}
a < b? 0
Best Practices
• Avoid unnecessary casts — let the compiler handle safe conversions.
• Cast explicitly to control behavior (e.g., to prevent integer division or to narrow intentionally).
• Remember that casting does not change the variable’s actual type, only the value used in that expression.
• Be careful mixing signed and unsigned integers.
• Prefer matching types in APIs and expressions to minimize implicit conversions.