Type Conversion & Casting
What is Type Conversion?
In C, when an expression involves different data types, the compiler needs to convert one type to another. This is called type conversion (also called type casting). There are two kinds:
- Implicit Conversion — done automatically by the compiler
- Explicit Conversion — done manually by the programmer using cast operator
Implicit Conversion (Automatic / Widening)
When you mix types in an expression, C automatically converts the "smaller" type to the "larger" type to avoid data loss. This happens automatically — no special syntax needed.
Promotion hierarchy (lower → higher): char → int → unsigned int → long → float → double
#include <stdio.h> int main() { int i = 10; float f = 3.5f; char c = 'A'; // ASCII value 65 // int + float → float (int promoted to float) float result1 = i + f; printf("10 + 3.5 = %.1f\n", result1); // 13.5 // char + int → int (char promoted to int) int result2 = c + i; printf("'A' + 10 = %d\n", result2); // 75 (65+10) // Assigning float to int → TRUNCATION (not rounding!) int truncated = 3.9f; printf("(int)3.9 = %d\n", truncated); // 3 (not 4!) // int / int → int (no decimal!) int div = 7 / 2; printf("7 / 2 = %d\n", div); // 3 (not 3.5!) return 0; }
Assigning a larger type to a smaller type (float → int) is narrowing conversion. The decimal part is truncated (cut off, NOT rounded). 3.9 becomes 3, not 4! The compiler may warn but will allow it.
Explicit Conversion (Type Casting)
You force a type conversion using the cast operator: (type)expression
(target_type) expression // Examples: (float) 7 // converts 7 to 7.0 (int) 3.9 // converts 3.9 to 3 (char) 65 // converts 65 to 'A'
#include <stdio.h> int main() { int a = 7, b = 2; // Without cast: integer division printf("Without cast: %d / %d = %d\n", a, b, a / b); // 3 // With cast: float division printf("With cast: %d / %d = %.2f\n", a, b, (float)a / b); // 3.50 // char to int char ch = 'Z'; printf("ASCII of Z = %d\n", (int)ch); // 90 // int to char int ascii = 66; printf("Char 66 = %c\n", (char)ascii); // B // double to int (truncation) double d = 9.99; printf("(int)9.99 = %d\n", (int)d); // 9 return 0; }
Conversion Rules Summary
| Expression | Result Type | Example | Output |
|---|---|---|---|
| int + int | int | 7 / 2 | 3 (truncated) |
| int + float | float | 7 + 2.5f | 9.5 |
| float + double | double | 2.5f + 1.0 | 3.5 (double) |
| char op int | int | 'A' + 1 | 66 |
| (float)int / int | float | (float)7/2 | 3.500000 |
| (int)float | int | (int)3.9 | 3 (truncated) |
Complete Programs
Average with Correct Float Division
#include <stdio.h> int main() { int marks[] = {85, 90, 78, 92, 88}; int n = 5, sum = 0; for (int i = 0; i < n; i++) sum += marks[i]; // WRONG: int / int = int printf("Wrong avg: %d\n", sum / n); // CORRECT: cast to float first printf("Correct avg: %.2f\n", (float)sum / n); return 0; }
ASCII Value Converter
#include <stdio.h> int main() { char ch; printf("Enter a character: "); scanf("%c", &ch); printf("'%c' ASCII = %d\n", ch, (int)ch); // Uppercase from lowercase if (ch >= 'a' && ch <= 'z') { char upper = (char)(ch - 32); printf("Uppercase: %c\n", upper); } return 0; }
Summary
- Implicit conversion — automatic, smaller type promoted to larger (widening)
- Explicit casting — manual using
(type)operator int / int→int(decimal part truncated — not rounded!)- Use
(float)a / bto get float division result - Narrowing (float → int) truncates decimal part
charandintcan be interchanged using ASCII values
Write programs: (1) Correct average of 5 integers (2) Find percentage: (marks/total)*100 — avoid integer division (3) Temperature conversion: Celsius to Fahrenheit with float precision (4) Convert lowercase to uppercase using casting
Frequently Asked Questions
What is type conversion in C?
int into a float. It happens either automatically (implicit) or when you request it (explicit), and it lets values of different types work together in an expression.What is the difference between implicit and explicit type conversion?
int to a float in an expression. Explicit conversion, or casting, is when you deliberately convert a value using a cast like (int)x.What is type casting in C?
(float)5 to treat the integer 5 as a float. It gives you direct control over how a value is interpreted.What is type promotion in C?
char or short being promoted to int. It ensures calculations happen in a consistent, wide-enough type.Why does integer division lose the decimal part in C?
5 / 2 gives 2, not 2.5. To keep the decimals you convert at least one operand to a floating type, for example (float)5 / 2.