Skip to content
StudyFame

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 conversionExplicit conversion (type casting)
Done byThe compiler, automaticallyThe programmer
Also calledType promotion, automatic conversionType casting
DirectionUsually small type → bigger typeAny type → any type
Data loss?Usually noPossible (e.g. 9.99 → 9)
Examplefloat 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.
  • char and short are converted to int before any arithmetic.
  • Storing a decimal into an int cuts off the decimal part (it does not round): 9.99 → 9.
  • int / int is always integer division, even if you store the result in a float.

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

  1. total / count: both are int, so C does integer division: 7 / 2 = 3. Then 3 is converted to 3.00 when stored in the float. The .5 is already lost.
  2. (float)total: converts 7 to 7.0 before dividing.
  3. Now it is float / int. C promotes count to float (implicit), and 7.0 / 2.0 = 3.5.

How Type Conversion Works

  1. C looks at the two values on each side of an operator.
  2. If their types are different, it converts the smaller type to the bigger type (implicit conversion).
  3. It does the operation in the bigger type.
  4. When the result is stored in a variable, it is converted again to that variable's type.
  5. 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

MistakeResultFix
float avg = (a + b) / 2; with intsDecimal 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 5It is 4 (truncation)Add 0.5 before casting for positive numbers, or use round()
Storing a big double in an intUndefined result if it doesn't fitUse a big enough type

Type Conversion Practice Questions

  1. What is type conversion? Name its two types.
  2. What is the difference between implicit and explicit conversion?
  3. What is the output of printf("%d", (int)7.8);?
  4. What is the value of float x = 9 / 4;?
  5. Write the order of type promotion from char to double.
Show answers
  1. Changing a value from one data type to another. Types: implicit and explicit.
  2. Implicit is done automatically by the compiler (smaller to bigger type). Explicit (casting) is done by the programmer using (type).
  3. 7 (decimal part is cut off).
  4. 2.0 (9 / 4 is integer division = 2, then converted to 2.0).
  5. 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)

  1. What is the result of 7 / 2 in C (both int)?

    A) 3.5   B) 3   C) 4   D) 3.0

  2. Type casting is:

    A) Implicit conversion   B) Explicit conversion   C) A loop   D) A keyword

  3. What does (int)5.99 give?

    A) 6   B) 5   C) 5.99   D) Error

  4. int + double gives which type?

    A) int   B) float   C) double   D) char

  5. Which gives 3.5?

    A) (float)(7 / 2)   B) 7 / 2   C) (float)7 / 2   D) (int)7.0 / 2

Show answers
  1. B: 3.
  2. B: explicit conversion.
  3. B: 5 (truncated).
  4. C: double.
  5. C: (float)7 / 2

Quick Revision for Exams and Interviews

Read this before your exam. The highlighted words are the answer.

Must-remember facts

TypesImplicit (automatic) and Explicit (casting)
Cast syntax(type) expression
Promotion orderchar → short → int → long → long long → float → double
7 / 23 (integer division)
(float)7 / 23.5
(int)9.999 (truncation, not rounding)
'A' + 166

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.