Course contentsShow
← C
Lesson 122 of 1,2192. Variables and TypesFree lesson

Integer promotion rules

Integer promotion rules

What you'll learn: How C automatically upgrades "small" integers like char and short to int during calculations to keep the CPU happy.

The CPU’s comfort zone

Imagine you’re a professional mover. You have massive, standard-sized moving boxes (let's call them int boxes) that fit perfectly onto your dolly. Occasionally, a client gives you a tiny shoebox (char) or a medium jewelry box (short).

Even though these small boxes save space in the truck, they are awkward to stack individually. To make your job easier, you simply place each small box inside a standard-sized int box before moving it. C does the exact same thing. Before performing math, it "promotes" small types to a full-sized int.

The promotion process

In C, the int type is considered the natural size for the processor to handle. Because of this, C is very impatient with types smaller than an int. If you use a char or a short in a mathematical expression—even just adding two char variables together—C silently converts them to int values first.

This happens automatically behind the scenes. The original variables in memory don't change their size; instead, their values are copied into a temporary "integer-sized" workspace to do the math.

Seeing promotion in action

Consider this example. We have two small numbers, but C insists on treating them as full integers during the addition:

#include <stdio.h>

int main() {
    char a = 100;
    char b = 50;

    // Even though 'a' and 'b' are chars, 
    // they are promoted to 'int' before the addition.
    int result = a + b; 

    printf("Result: %d", result);
    return 0;
}

Without promotion, adding two char values that exceed 127 could cause an "overflow" (where the number wraps around to a negative). However, because C promotes them to int first, the calculation happens in a much larger space, preventing errors during the intermediate steps of your math.

Why does this matter?

You might wonder why we don't just use int for everything. Small types are great for saving memory when storing thousands of values (like pixels in an image). However, for the actual "work" of calculation, C upgrades them to ensure speed and consistency across different types of computer hardware.

Key takeaway

C automatically promotes char and short types to int during calculations to ensure the CPU can process the data efficiently.