Skip to content

Type Conversion and Casting in C

Updated 6 min read Aimerscrew

Sooner or later, you'll mix types in one calculation: an int quantity times a double price, or a total divided by a count. Type conversion in C is what happens when a value of one type is turned into another type. Sometimes C does it for you; sometimes you have to ask for it. Understanding the difference saves you from one of the most common beginner bugs: an average that's mysteriously wrong.

🌍 Real-life example

A teacher adds up three test scores, 88, 92 and 79, and divides by 3 to get the class average. On a calculator, the answer is 86.33. A careless C program prints 86, because it divided two whole numbers and threw the decimals away. By the end of this lesson, you'll know exactly why and how to fix it.

What is type conversion in C?

Type conversion means changing a value from one data type to another, for example turning the int 7 into the double 7.0. There are two kinds:

  • Implicit conversion (also called automatic conversion or coercion): C converts the value by itself.
  • Explicit conversion (a cast): you tell C to convert the value, by writing the type in parentheses.

Implicit conversion: automatic promotion

When an operator gets two different types, C converts the "smaller" one to the "larger" one before doing the math. This is called promotion. Here, the int 3 is promoted to the double 3.0 before it's multiplied:

#include <stdio.h>

int main(void) {
    int items = 3;
    double price = 2.50;

    double total = items * price;   /* items is promoted to 3.0 */
    printf("Total: %.2f\n", total);

    double budget = 20;             /* the int 20 becomes 20.0 */
    printf("Budget: %f\n", budget);
    return 0;
}
Total: 7.50
Budget: 20.000000

Small integer types like char and short are always promoted to int before arithmetic, and integers are promoted to floating-point when mixed with a float or double.

Promotion ladder from char, short, int, long, long long to float and double; below, 7 / 2 with ints gives 3 while (double)7 / 2 gives 3.5
Mixed types are promoted up the ladder. Two ints stay ints, so the decimal part is lost.

Why does 7 / 2 equal 3 in C?

When both sides of / are integers, C does integer division: it keeps only the whole-number part and drops the remainder. No rounding happens; the decimals are simply cut off. As soon as one side is a floating-point number, you get a decimal result:

#include <stdio.h>

int main(void) {
    printf("7 / 2 = %d\n", 7 / 2);
    printf("7.0 / 2 = %f\n", 7.0 / 2);
    printf("7 / 2.0 = %f\n", 7 / 2.0);
    printf("2 / 3 = %d\n", 2 / 3);
    return 0;
}
7 / 2 = 3
7.0 / 2 = 3.500000
7 / 2.0 = 3.500000
2 / 3 = 0

Notice 2 / 3 is 0, not 0.67. Integer division is handy when you really want whole units (like full boxes), but it's a trap when you want an exact answer. You'll see more of it in C arithmetic operators.

Explicit conversion: casting

A cast converts a value to the type you write in parentheses in front of it: (type) value. Casting one side of a division to double is enough, because the other side is then promoted too:

#include <stdio.h>

int main(void) {
    int total_points = 255;
    int tests = 4;

    double wrong = total_points / tests;
    double right = (double)total_points / tests;

    printf("Without cast: %.2f\n", wrong);
    printf("With cast: %.2f\n", right);
    return 0;
}
Without cast: 63.00
With cast: 63.75

In the first line, 255 / 4 is calculated as integers (63) before it's stored in the double, so the .75 is already gone. Storing the result in a double doesn't bring it back.

💡 Tip

Put the cast on a value, not on the finished division. (double)(total_points / tests) still gives 63.00, because the integer division happens first, inside the parentheses. Write (double)total_points / tests instead.

Converting a decimal to an int (truncation)

Converting a double or float to an int truncates it: the fractional part is chopped off, moving toward zero. It does not round:

#include <stdio.h>

int main(void) {
    double temperature = 23.9;
    double below_zero = -23.9;

    int whole = (int)temperature;
    int whole_negative = (int)below_zero;
    int rounded = (int)(temperature + 0.5);

    printf("(int)23.9 = %d\n", whole);
    printf("(int)-23.9 = %d\n", whole_negative);
    printf("Rounded 23.9 = %d\n", rounded);
    return 0;
}
(int)23.9 = 23
(int)-23.9 = -23
Rounded 23.9 = 24

Adding 0.5 before casting is a quick way to round positive numbers. For a proper rounding function that handles negatives too, see round() in C math functions.

Converting between char and int

A char is stored as a small number, its character code. In ASCII, 'A' is 65, 'B' is 66, and lowercase letters start at 97. That means you can do arithmetic with characters:

#include <stdio.h>

int main(void) {
    char letter = 'A';
    int code = letter;
    char next = letter + 1;
    char lower = letter + 32;

    char digit = '7';
    int value = digit - '0';

    printf("'%c' has code %d\n", letter, code);
    printf("The letter after %c is %c\n", letter, next);
    printf("Lowercase of %c is %c\n", letter, lower);
    printf("The character '%c' as a number: %d\n", digit, value);
    return 0;
}
'A' has code 65
The letter after A is B
Lowercase of A is a
The character '7' as a number: 7

The last trick is useful when reading digits as text: the codes for '0' to '9' sit next to each other, so subtracting '0' turns the character '7' into the number 7.

Implicit vs explicit conversion

Implicit conversionExplicit conversion (cast)
Who does it?The compiler, automaticallyYou, by writing (type)
When?Mixing types, assigning to another typeWhenever you choose
Exampledouble d = 5;(double)sum / count
RiskSilent data loss (e.g. double to int)Clear in the code, easy to spot

Real-world example: average test score

#include <stdio.h>

int main(void) {
    int score1 = 88, score2 = 92, score3 = 79;
    int count = 3;

    int sum = score1 + score2 + score3;
    double average = (double)sum / count;

    printf("Sum: %d\n", sum);
    printf("Average: %.2f\n", average);
    return 0;
}
Sum: 259
Average: 86.33

Without the (double) cast, 259 / 3 would be 86 and the report card would be wrong.

Common mistakes with type conversion

1. Expecting decimals from integer division

double half = 1 / 2; stores 0.0, because 1 / 2 is 0 in integer math. Write 1.0 / 2 or cast one side.

2. Casting too late

(double)(a / b) converts a result that has already lost its decimals. Cast a itself: (double)a / b.

3. Expecting a cast to round

(int)9.99 is 9, not 10. Casting to int always truncates toward zero.

4. Converting a value that doesn't fit

Converting a huge double such as 5e12 to an int doesn't fit in its range, and the result is undefined: you may get a strange number. Make sure the value fits the target type.

Practice exercise

🏋️ Try it yourself

A podcast episode lasts 135 minutes. Store that in an int. Print the length in hours with two decimals, then print how many full hours it is.

Show solution
#include <stdio.h>

int main(void) {
    int minutes = 135;

    double hours = minutes / 60.0;
    int full_hours = minutes / 60;

    printf("%d minutes = %.2f hours\n", minutes, hours);
    printf("That is %d full hours\n", full_hours);
    return 0;
}
135 minutes = 2.25 hours
That is 2 full hours

Dividing by 60.0 (a double) gives the exact answer, while dividing by 60 (an int) uses integer division on purpose.

What's next?

You now know that types have different sizes. Next, measure them yourself with the sizeof operator in C.

Key takeaways

  • Type conversion changes a value from one type to another, either automatically (implicit) or with a cast (explicit).
  • In mixed calculations, the smaller type is promoted to the larger one, for example int to double.
  • Dividing two integers gives an integer: 7 / 2 is 3 and the remainder is dropped.
  • Cast one side before dividing, as in (double)sum / count, to get a decimal result.
  • Converting a double to an int truncates toward zero; (int)23.9 is 23, not 24.
  • A char is stored as a number, so 'A' + 1 is 'B' and '7' - '0' is 7.

Frequently asked questions

What is type casting in C?

Type casting is explicitly converting a value to another type by writing the type in parentheses before it, for example (double)total converts the int total to a double.

Why does 7 / 2 give 3 in C?

Both 7 and 2 are integers, so C performs integer division and drops the remainder. Write 7.0 / 2 or (double)7 / 2 to get 3.5.

What is the difference between implicit and explicit type conversion in C?

Implicit conversion happens automatically, for example when an int is mixed with a double. Explicit conversion is a cast that you write yourself, such as (int)price.

Does casting a float to int round in C?

No. Casting to int truncates toward zero, so (int)9.99 is 9 and (int)-9.99 is -9. Use round() from math.h when you need real rounding.

How do you convert a char to an int in C?

Assign it to an int to get its character code, so int code = 'A'; gives 65. To turn a digit character into its number, subtract '0', so '7' - '0' is 7.