Type casting is the process of converting a value from one data type to another. In C, this happens in two ways:
(type).Understanding type casting is critical to avoid data loss, precision errors, and unexpected behaviour in arithmetic operations.
When operands of different types appear in an expression, C automatically promotes the smaller type to the larger type. The promotion hierarchy is:
| Rank (lowest -> highest) | Type |
|---|---|
| 1 | char, short |
| 2 | int |
| 3 | unsigned int |
| 4 | long |
| 5 | unsigned long |
| 6 | float |
| 7 | double |
| 8 | long double |
#include <stdio.h>
int main() {
int a = 5;
float b = 2.5;
// int + float -> float (a is promoted to float automatically)
float result = a + b;
printf("5 + 2.5 = %.1f\n", result); // 7.5
// Integer division - both operands are int, result is int
int x = 7, y = 2;
printf("7 / 2 = %d\n", x / y); // 3 (truncated, NOT 3.5)
// char is promoted to int in arithmetic
char c = 'A'; // ASCII 65
printf("'A' + 1 = %d\n", c + 1); // 66
// Mixed: int * double -> double
int i = 3;
double d = 1.5;
printf("3 * 1.5 = %f\n", i * d); // 4.500000
return 0;
}
/*
Output:
5 + 2.5 = 7.5
7 / 2 = 3
'A' + 1 = 66
3 * 1.5 = 4.500000
*/
Use the cast operator (type)expression to manually convert a value. This is necessary when you want to control the conversion - for example, getting a floating-point result from integer division.
#include <stdio.h>
int main() {
// Fix integer division with explicit cast
int a = 7, b = 2;
float result = (float)a / b; // cast a to float BEFORE division
printf("(float)7 / 2 = %.2f\n", result); // 3.50
// double to int - truncates decimal part (no rounding)
double pi = 3.14159;
int truncated = (int)pi;
printf("(int)3.14159 = %d\n", truncated); // 3
// int to char - takes the ASCII value
int code = 72;
char letter = (char)code;
printf("(char)72 = %c\n", letter); // H
// Pointer casting - void* to int*
void *vptr;
int num = 100;
vptr = #
int *iptr = (int*)vptr;
printf("Via void*: %d\n", *iptr); // 100
// Overflow example - be careful!
int big = 300;
char small = (char)big; // 300 % 256 = 44 -> ','
printf("(char)300 = %d ('%c')\n", small, small); // 44 ','
return 0;
}
/*
Output:
(float)7 / 2 = 3.50
(int)3.14159 = 3
(char)72 = H
Via void*: 100
(char)300 = 44 (',')
*/
| Pitfall | Wrong | Correct |
|---|---|---|
| Integer division losing decimal | float r = 7/2; -> 3.0 | float r = (float)7/2; -> 3.5 |
| Casting after division (too late) | (float)(7/2) -> 3.0 | (float)7/2 -> 3.5 |
| Overflow on narrowing cast | (char)300 -> 44 | Check range before casting |
| Signed/unsigned mismatch | (unsigned int)-1 -> 4294967295 | Avoid mixing signed/unsigned |
#include <stdio.h>
int main() {
int scores[] = {85, 92, 78, 95, 88};
int n = 5;
int sum = 0;
for (int i = 0; i < n; i++) {
sum += scores[i];
}
// Without cast: integer division -> wrong average
printf("Wrong average: %d\n", sum / n); // 87
// With cast: float division -> correct average
printf("Correct average: %.2f\n", (float)sum / n); // 87.60
return 0;
}
/*
Output:
Wrong average: 87
Correct average: 87.60
*/
Explore 500+ free tutorials across 20+ languages and frameworks.