C Type Conversion
Converting between different data types in C
π What is Type Conversion?
Type conversion is the process of converting a value from one data type to another. C supports both automatic (implicit) and manual (explicit) type conversion.
// Automatic conversion
int num = 10;
float result = num; // int to float
// Manual conversion (casting)
float pi = 3.14f;
int whole = (int)pi; // float to int
Result:
result = 10.000000
whole = 3
Types of Conversion
Implicit Conversion
Automatic conversion by compiler
int a = 5;
float b = a; // auto
Explicit Conversion
Manual conversion using casting
float f = 3.7f;
int i = (int)f;
Promotion
Converting to larger type
char c = 'A';
int ascii = c;
Demotion
Converting to smaller type
double d = 3.14159;
float f = (float)d;
πΉ Implicit Type Conversion
Implicit type conversion occurs automatically when the compiler converts between compatible data types. When mixing integers and floating-point numbers in expressions, integers automatically convert to floating-point. For example, int x = 5; float y = x; converts x to 5.0. The compiler follows promotion rules converting smaller types to larger ones. Understanding implicit conversions prevents unexpected results and enables writing correct arithmetic expressions.
#include <stdio.h>
int main() {
// Implicit conversions in assignments
int integer = 42;
float floating = integer; // int to float
double precise = floating; // float to double
printf("Original int: %d\n", integer);
printf("Converted to float: %.2f\n", floating);
printf("Converted to double: %.6f\n", precise);
// Implicit conversion in expressions
int a = 10;
float b = 3.5f;
float result = a + b; // a is promoted to float
printf("\nExpression: %d + %.1f = %.1f\n", a, b, result);
// Character to integer conversion
char letter = 'A';
int ascii_value = letter; // char to int
printf("Character '%c' has ASCII value: %d\n", letter, ascii_value);
return 0;
}
Output:
Original int: 42
Converted to float: 42.00
Converted to double: 42.000000
Expression: 10 + 3.5 = 13.5
Character 'A' has ASCII value: 65
πΉ Explicit Type Conversion (Casting)
Explicit type conversion manually converts values using the cast operator for precise type control. Use syntax like (float)integer_variable or (int)floating_point_variable to convert between types. Casting enables truncating decimal values, converting between numeric types, and handling pointer types. Explicit casting clarifies intent, providing control over conversion behavior and potentially preventing data loss from implicit conversions.
#include <stdio.h>
int main() {
// Explicit casting examples
float decimal = 9.8f;
int whole = (int)decimal; // Truncates decimal part
printf("Original float: %.1f\n", decimal);
printf("Cast to int: %d\n", whole);
// Division with casting
int numerator = 7;
int denominator = 2;
// Integer division
int int_result = numerator / denominator;
// Float division using casting
float float_result = (float)numerator / denominator;
printf("\nInteger division: %d / %d = %d\n", numerator, denominator, int_result);
printf("Float division: %d / %d = %.2f\n", numerator, denominator, float_result);
// Converting between char and int
int number = 65;
char character = (char)number;
printf("\nASCII %d as character: '%c'\n", number, character);
return 0;
}
Output:
Original float: 9.8
Cast to int: 9
Integer division: 7 / 2 = 3
Float division: 7 / 2 = 3.50
ASCII 65 as character: 'A'
πΉ Type Conversion Hierarchy
C follows a strict hierarchy when performing automatic type conversions in mixed expressions. The hierarchy from lowest to highest priority is: char β short β int β long β float β double. Rule: In mixed expressions, lower types automatically promote to higher types. For example, in int + float, the integer promotes to float before addition. This hierarchy prevents precision loss and maintains numeric consistency throughout calculations.
Conversion Hierarchy (Low to High):
- char β short β int β long β long long
- float β double β long double
Rule: In mixed expressions, lower types are promoted to higher types.
#include <stdio.h>
int main() {
// Demonstrating conversion hierarchy
char c = 100;
short s = 200;
int i = 300;
float f = 1.5f;
double d = 2.5;
// Mixed expressions - all promoted to highest type
printf("char + short = int: %d\n", c + s); // Result: int
printf("int + float = float: %.1f\n", i + f); // Result: float
printf("float + double = double: %.1f\n", f + d); // Result: double
// Complex expression
double result = c + s + i + f + d; // All promoted to double
printf("Mixed expression result: %.1f\n", result);
return 0;
}
Output:
char + short = int: 300
int + float = float: 301.5
float + double = double: 4.0
Mixed expression result: 604.0
πΉ Common Conversion Pitfalls
Avoid common mistakes with type conversion that cause bugs and unexpected program behavior. Integer division truncates results losing decimalsβ5/2 equals 2, not 2.5. Casting to smaller types causes overflow; for example, assigning 300 to a char loses data. Comparing signed and unsigned integers may produce unexpected results. Converting pointers without care causes segmentation faults. Being aware of these pitfalls enables writing safe code with predictable behavior and proper error handling.
#include <stdio.h>
int main() {
// 1. Loss of precision
double precise = 3.14159265359;
float less_precise = (float)precise;
printf("Double: %.10f\n", precise);
printf("Float: %.10f\n", less_precise);
// 2. Truncation (not rounding)
float decimal = 9.99f;
int truncated = (int)decimal; // Becomes 9, not 10!
printf("\nTruncation: %.2f becomes %d\n", decimal, truncated);
// 3. Overflow in smaller types
int big_number = 300;
char small = (char)big_number; // char range: -128 to 127
printf("Overflow: %d becomes %d in char\n", big_number, small);
// 4. Unexpected results in division
int a = 5, b = 2;
printf("\nInteger division: %d / %d = %d\n", a, b, a/b);
printf("Correct division: %d / %d = %.2f\n", a, b, (float)a/b);
return 0;
}
Output:
Double: 3.1415926536
Float: 3.1415927410
Truncation: 9.99 becomes 9
Overflow: 300 becomes 44 in char
Integer division: 5 / 2 = 2
Correct division: 5 / 2 = 2.50