Type Conversion and Type Casting in C
What happens when you add an int and a float? C first converts them to the same type, then adds them. This is called type conversion.
Sometimes C does it automatically, and sometimes you do it yourself. In this lesson you will learn both, and why 7 / 2 gives 3 but (float)7 / 2 gives 3.5.
What You Will Learn
- What type conversion is.
- Implicit conversion (done by the compiler).
- Explicit conversion, also called type casting (done by you).
- The order in which types are promoted.
- How to fix integer division with a cast.
Type Conversion in C Explained
Exam definition: Type conversion is changing a value from one data type to another. It is of two types: implicit (automatic, done by the compiler) and explicit (manual, done by the programmer using a cast).
| Implicit conversion | Explicit conversion (type casting) | |
|---|---|---|
| Done by | The compiler, automatically | The programmer |
| Also called | Type promotion, automatic conversion | Type casting |
| Direction | Usually small type → bigger type | Any type → any type |
| Data loss? | Usually no | Possible (e.g. 9.99 → 9) |
| Example | float f = 5; (5 → 5.0) | int n = (int)9.99; (9.99 → 9) |
Order of implicit promotion
When two different types meet in an expression, the smaller one is converted to the bigger one:
char → short → int → long → long long → float → double → long double
Memory trick: "Small water flows into the big bucket". The smaller type is poured into the bigger type, so nothing spills (no data is lost). Casting is like using a smaller cup: some water can spill (the decimal part is lost).
Rules to remember
int+float→float.int+double→double.charandshortare converted tointbefore any arithmetic.- Storing a decimal into an
intcuts off the decimal part (it does not round): 9.99 → 9. int/intis always integer division, even if you store the result in afloat.
Syntax of Type Casting
(new_type) expression
Examples: (float)7 → 7.0, (int)3.75 → 3, (char)66 → 'B'.
Example 1: Fix Integer Division with Type Casting
#include <stdio.h>
int main(void)
{
int total = 7, count = 2;
float wrong = total / count; /* integer division */
float right = (float)total / count; /* cast first */
printf("Without cast: %.2f\n", wrong);
printf("With cast : %.2f\n", right);
return 0;
}
Output
Without cast: 3.00 With cast : 3.50
Code Explanation
total / count: both areint, so C does integer division: 7 / 2 = 3. Then 3 is converted to 3.00 when stored in thefloat. The .5 is already lost.(float)total: converts 7 to 7.0 before dividing.- Now it is
float/int. C promotescountto float (implicit), and 7.0 / 2.0 = 3.5.
How Type Conversion Works
- C looks at the two values on each side of an operator.
- If their types are different, it converts the smaller type to the bigger type (implicit conversion).
- It does the operation in the bigger type.
- When the result is stored in a variable, it is converted again to that variable's type.
- A cast
(type)forces a conversion at exactly the place where you write it.
Example 2: Character, Integer and Float Conversions
#include <stdio.h>
int main(void)
{
char letter = 'A';
int next = letter + 1; /* char promoted to int: 65 + 1 */
double price = 99.99;
int rupees = (int)price; /* decimal part cut off */
printf("letter + 1 = %d\n", next);
printf("As character: %c\n", (char)next);
printf("(int)99.99 = %d\n", rupees);
printf("5 / 2.0 = %.1f\n", 5 / 2.0);
return 0;
}
Output
letter + 1 = 66 As character: B (int)99.99 = 99 5 / 2.0 = 2.5
5 / 2.0: 5 is an int and 2.0 is a double, so 5 is promoted to 5.0 automatically.
Important Points to Remember
- 2 types: implicit (automatic) and explicit (type casting).
- Promotion goes from smaller to bigger type.
- Cast syntax:
(type) value. - Converting float → int truncates (cuts), it does not round.
int / int= integer division; cast one value to get decimals.
Common Mistakes Beginners Make
| Mistake | Result | Fix |
|---|---|---|
float avg = (a + b) / 2; with ints | Decimal part lost | (a + b) / 2.0f or (float)(a + b) / 2 |
(float)(7 / 2) | 3.0 (the division happens first) | (float)7 / 2 |
Expecting (int)4.9 to be 5 | It is 4 (truncation) | Add 0.5 before casting for positive numbers, or use round() |
Storing a big double in an int | Undefined result if it doesn't fit | Use a big enough type |
Type Conversion Practice Questions
- What is type conversion? Name its two types.
- What is the difference between implicit and explicit conversion?
- What is the output of
printf("%d", (int)7.8);? - What is the value of
float x = 9 / 4;? - Write the order of type promotion from
chartodouble.
Show answers
- Changing a value from one data type to another. Types: implicit and explicit.
- Implicit is done automatically by the compiler (smaller to bigger type). Explicit (casting) is done by the programmer using
(type). 7(decimal part is cut off).- 2.0 (9 / 4 is integer division = 2, then converted to 2.0).
char→short→int→long→long long→float→double.
Coding Exercise
A student scored 433 marks out of 500. Store both in int variables and print the percentage with 2 decimal places using a type cast.
Show solution
#include <stdio.h>
int main(void)
{
int scored = 433, total = 500;
float percent = (float)scored / total * 100;
printf("Percentage = %.2f%%\n", percent);
return 0;
}
Percentage = 86.60%
Quick Quiz: Type Conversion in C (MCQ)
-
What is the result of
7 / 2in C (both int)?A) 3.5 B) 3 C) 4 D) 3.0
-
Type casting is:
A) Implicit conversion B) Explicit conversion C) A loop D) A keyword
-
What does
(int)5.99give?A) 6 B) 5 C) 5.99 D) Error
-
int+doublegives which type?A) int B) float C) double D) char
-
Which gives 3.5?
A)
(float)(7 / 2)B)7 / 2C)(float)7 / 2D)(int)7.0 / 2
Show answers
- B: 3.
- B: explicit conversion.
- B: 5 (truncated).
- C: double.
- C:
(float)7 / 2
Quick Revision for Exams and Interviews
Read this before your exam. The highlighted words are the answer.
Must-remember facts
| Types | Implicit (automatic) and Explicit (casting) |
|---|---|
| Cast syntax | (type) expression |
| Promotion order | char → short → int → long → long long → float → double |
7 / 2 | 3 (integer division) |
(float)7 / 2 | 3.5 |
(int)9.99 | 9 (truncation, not rounding) |
'A' + 1 | 66 |
Frequently asked questions
- What is the difference between type conversion and type casting?
- Conversion is automatic by the compiler; casting is forced by the programmer with
(type). - Why does 7 / 2 give 3 in C?
- Both are integers, so C does integer division and drops the decimal part.
- Does (int)3.9 round to 4?
- No. It truncates to 3.
- What is type promotion?
- Automatic conversion of a smaller type to a bigger type in an expression.
Remember
- Implicit = small water into big bucket. Casting = small cup, can spill.
- Cast before dividing:
(float)a / b.
Next Topic
Next lesson: Scope of Variables in C. You will learn where a variable can be used in a program and how long it lives.