C Type Conversion
In C programming, every variable has a specific data type — int, float, char, and so on. But sometimes you need to use a value of one type in a place that expects a different type. Type conversion is the process of changing a value from one data type to another.
C supports two kinds of type conversion: implicit (done automatically by the compiler) and explicit (done manually by the programmer using a cast).
Why Type Conversion is Needed
Imagine you have two boxes — one holds whole apples (integers) and another holds juice amounts (decimals). If you pour apple count into a juice measurement system, some adjustment is needed. That adjustment is type conversion.
int a = 5;
int b = 2;
float result = a / b; // result = 2.0, NOT 2.5 — integer division!
The division gives 2 (integer), not 2.5 (decimal), because both operands are integers. Type conversion fixes this problem.
Implicit Type Conversion (Automatic)
Implicit type conversion happens automatically when the compiler converts a smaller data type to a larger one during an operation. This is also called type promotion or widening.
Type Promotion Hierarchy
Low Precision -----> High Precision
char --> short --> int --> long --> float --> double --> long double
Rule: When two types are mixed, the smaller type is promoted to the larger type.
Example: int promoted to float
#include <stdio.h>
int main()
{
int num = 7;
float result;
result = num; // int automatically becomes float
printf("Result: %f\n", result); // Output: 7.000000
return 0;
}
Example: Mixed arithmetic
#include <stdio.h>
int main()
{
int a = 5;
float b = 2.0;
float result;
result = a + b; // a (int) is promoted to float before addition
printf("Result: %f\n", result); // Output: 7.000000
return 0;
}
Since one operand is float, the compiler promotes a (int) to float and then adds them.
Implicit Conversion in Assignment
float pi = 3.99;
int n = pi; // float is truncated to int — n becomes 3 (decimal part lost)
When converting from a larger type to a smaller type, data can be lost. This is called narrowing.
Explicit Type Conversion (Type Casting)
Explicit type conversion — also called type casting — is when you manually tell the compiler to convert a value to a specific type. You write the target type in parentheses before the value.
Syntax
(target_type) expression
Example: Fixing integer division
#include <stdio.h>
int main()
{
int a = 5;
int b = 2;
// Without cast — integer division
printf("Without cast: %f\n", (float)(a / b)); // 2.000000
// With cast — float division
printf("With cast : %f\n", (float)a / b); // 2.500000
return 0;
}
In the second printf, casting a to float before division forces the entire division to use floating-point arithmetic.
Example: char to int
#include <stdio.h>
int main()
{
char letter = 'A';
int ascii_value;
ascii_value = (int) letter; // cast char to int
printf("ASCII value of 'A': %d\n", ascii_value); // Output: 65
return 0;
}
Example: double to int
#include <stdio.h>
int main()
{
double price = 99.75;
int rounded;
rounded = (int) price; // decimal part is truncated (NOT rounded)
printf("Truncated price: %d\n", rounded); // Output: 99
return 0;
}
Implicit vs Explicit Conversion Summary
| Feature | Implicit Conversion | Explicit Conversion (Cast) |
|---|---|---|
| Done by | Compiler automatically | Programmer manually |
| Syntax | No special syntax needed | (type) value |
| Direction | Usually small → large (safe) | Any direction (may lose data) |
| Risk | Low (widening is safe) | Higher (narrowing loses data) |
| Control | Compiler decides | Programmer decides |
Common Conversion Scenarios
Scenario 1: Calculating percentage
#include <stdio.h>
int main()
{
int scored = 73;
int total = 100;
// Wrong — gives 0 due to integer division
float pct1 = scored / total * 100;
// Correct — cast forces float division
float pct2 = (float)scored / total * 100;
printf("Wrong : %.2f%%\n", pct1); // 0.00%
printf("Correct : %.2f%%\n", pct2); // 73.00%
return 0;
}
Scenario 2: Character arithmetic
#include <stdio.h>
int main()
{
char ch = 'a';
char upper;
upper = (char)(ch - 32); // ASCII trick: 'a'=97, 'A'=65, difference=32
printf("Lowercase: %c\n", ch); // a
printf("Uppercase: %c\n", upper); // A
return 0;
}
Type Conversion Pitfalls
Pitfall 1: Data loss in narrowing
double d = 1234567890.123;
int n = (int) d; // n = 1234567890, decimal part lost
// If d were too large for int, the result would be undefined
Pitfall 2: Integer division before cast
int a = 1, b = 4;
float result = (float)(a / b); // a/b = 0 (int), then cast = 0.000000
float correct = (float)a / b; // a cast first, then divide = 0.250000
Pitfall 3: Signed to unsigned
int negative = -1;
unsigned int u = (unsigned int) negative;
printf("%u\n", u); // Prints a very large number (4294967295 on 32-bit)
Visual Summary Diagram
IMPLICIT (Compiler promotes automatically):
char --[auto]--> int --[auto]--> float --[auto]--> double
Safe — no data loss going UP the chain
EXPLICIT (Programmer forces conversion):
double --[(int)]--> int (decimal part truncated)
float --[(char)]--> char (value reduced to 0-255 range)
int --[(float)]--> float (may have precision change)
Casting syntax: result = (target_type) expression;
Summary
Type conversion in C changes a value from one data type to another. Implicit conversion happens automatically when the compiler promotes a smaller type to a larger type during operations — this is safe and causes no data loss. Explicit conversion (type casting) is done manually using the (type) syntax and gives full control over how conversion happens. Casting from a larger type to a smaller type (narrowing) can cause data loss, particularly the loss of decimal parts or overflow. Understanding when to use casting — especially in division involving integers — is a skill every C programmer needs to master early.
