TExcelTAD
Home › C › C Type Conversion

C Type Conversion

Sometimes a value of one type has to become another type — an int used in a double calculation, or a double stored into an int. C performs some conversions automatically (implicit conversion) and lets you request others explicitly with a cast.

The integer division surprise

Before anything else, look at this:

c · try it yourself
#include <stdio.h>

int main(void) {
    int a = 7, b = 2;
    double result = a / b;
    printf("7 / 2 = %f\n", result);
    return 0;
}
7 / 2 = 3.000000

Why not 3.5? Because a / b divides two ints, and integer division throws away the fraction — the result is the int 3. Only after that is 3 converted to double and stored. The type of the variable on the left does not change how the right-hand side is calculated.

Implicit conversion

C converts values automatically in two common situations.

1. Assignment — the value is converted to the type of the variable:

c · try it yourself
#include <stdio.h>

int main(void) {
    double d = 9;          // int 9 becomes 9.0
    int i = 9.99;          // fraction is dropped: 9
    int n = -9.99;         // truncates toward zero: -9
    char c = 66;           // the number 66 is the code for 'B'

    printf("d = %f\n", d);
    printf("i = %d\n", i);
    printf("n = %d\n", n);
    printf("c = %c\n", c);
    return 0;
}
d = 9.000000
i = 9
n = -9
c = B

Converting a double to an int truncates toward zero; it never rounds. GCC can warn about such lossy conversions with -Wconversion.

2. Mixed arithmetic — when an operator gets two different types, the "smaller" one is promoted to the "larger" one before the calculation:

Operands Converted to
char or short with anything at least int (integer promotion)
int and long long
int and double double
float and double double
int and unsigned int unsigned int (careful!)

So 7 / 2.0 is a double division, because 7 is promoted to 7.0.

Explicit conversion: casting

You can force a conversion by writing the target type in parentheses in front of a value. This is called a cast:

c · try it yourself
#include <stdio.h>

int main(void) {
    int total = 7, count = 2;

    double wrong = total / count;
    double right = (double) total / count;   // cast BEFORE dividing

    printf("wrong: %.2f\n", wrong);
    printf("right: %.2f\n", right);

    double price = 19.99;
    int whole_euros = (int) price;
    printf("whole euros: %d\n", whole_euros);
    return 0;
}
wrong: 3.00
right: 3.50
whole euros: 19

The cast applies to total only, but that is enough: now the division mixes double and int, so count is promoted too and the result keeps its fraction.

Watch out: (double)(total / count) does not help — the parentheses make the integer division happen first, and only the already-truncated result is cast.

Rounding instead of truncating

To round a positive double to the nearest integer without the math library, add 0.5 before the cast:

c · try it yourself
#include <stdio.h>

int main(void) {
    double values[] = {2.4, 2.5, 2.6, 7.49};
    for (int i = 0; i < 4; i++) {
        printf("%.2f -> truncated %d, rounded %d\n",
               values[i], (int) values[i], (int) (values[i] + 0.5));
    }
    return 0;
}
2.40 -> truncated 2, rounded 2
2.50 -> truncated 2, rounded 3
2.60 -> truncated 2, rounded 3
7.49 -> truncated 7, rounded 7

(For negative numbers you would subtract 0.5 instead; the round() function from <math.h> handles both.)

The signed/unsigned trap

Mixing signed and unsigned integers converts the signed value to unsigned, which can turn a small negative number into a huge positive one:

c · try it yourself
#include <stdio.h>

int main(void) {
    int temperature = -5;
    unsigned int limit = 10;

    if (temperature < limit) {
        printf("colder than the limit\n");
    } else {
        printf("surprise: -5 is NOT less than 10u\n");
    }
    printf("-5 as unsigned: %u\n", (unsigned int) temperature);
    return 0;
}
surprise: -5 is NOT less than 10u
-5 as unsigned: 4294967291

Tip: Compile with -Wall -Wextra; GCC warns about comparisons between signed and unsigned values. Avoid mixing them whenever possible.

Go further: Signedness and Truncation Bugs shows how a negative length passes a bounds check and how to fix it.

Test yourself

What is the value of double x = 9 / 4;?

  1. 2.25
  2. 2.0
  3. 2.5
  4. 3.0

What does (int) 6.99 evaluate to?

  1. 7
  2. 6
  3. 6.99
  4. It is a compile error

Which expression gives 2.5?

  1. 5 / 2
  2. (double)(5 / 2)
  3. (double) 5 / 2
  4. (int) 5.0 / 2