Integer Promotions in C
Integer Promotions in C
Integer Promotions in C
The C compiler promotes certain data types to a higher rank for the sake of
achieving consistency in the arithmetic operations of integers.
In addition to the standard int data type, the C language lets you work with its
subtypes such as char, short int, long int, etc. Each of these data types occupy a
different amount of memory space. For example, the size of a standard int is 4
bytes, whereas a char type is 2 bytes of length. When an arithmetic operation
involves integer data types of unequal length, the compiler employs the policy of
integer promotion.
Integer Promotions
As a general principle, the integer types smaller than int are promoted when an
operation is performed on them. If all values of the original type can be represented
as an int, the value of the smaller type is converted to an int; otherwise, it is
converted to an unsigned int.
One must understand the concept of integer promotion to write reliable C code,
and avoid unexpected problems related to the size of data types and arithmetic
operations on smaller integer types.
Example
In this example, the two variables a and b seem to be storing the same value, but
they are not equal.
#include <stdio.h>
int main(){
char a = 251;
unsigned char b = a;
if (a == b)
printf("\n Same");
https://www.tutorialspoint.com/cprogramming/c_integer_promotions.htm 1/6
6/16/24, 11:00 AM Integer Promotions in C
else
printf("\n Not Same");
return 0;
}
Output
When you run this code, it will produce the following output −
a=√
b=√
Not Same
You get this output because "a" and "b" are treated as integers during comparison.
"a" is a signed char converted to int as -5, while "b" is an unsigned char converted to
int as 251.
Let us try to understand the mechanism of integer promotions with this example −
#include <stdio.h>
int main(){
return 0;
}
Output
d as int: 65 as char: A
https://www.tutorialspoint.com/cprogramming/c_integer_promotions.htm 2/6
6/16/24, 11:00 AM Integer Promotions in C
The C compiler applies integer promotion when it deals with arithmetic operations
involving small types like char. Before the multiplication of these char types, the
compiler changes them to int type. So, in this case, (a * b) gets converted to int,
which can accommodate the result of multiplication, i.e., 1200.
Example
Integer promotions are applied as part of the usual arithmetic conversions to certain
argument expressions; operands of the unary +, -, and ~ operators; and operands
of the shift operators. Take a look at the following example −
#include <stdio.h>
int main(){
char a = 10;
int b = a >> 3;
return 0;
}
Output
When you run this code, it will produce the following output −
b as int: 1 as char:
In the above example, shifting the bit structure of "a" to the left by three bits still
results in its value within the range of char (a << 3 results in 80).
Example
https://www.tutorialspoint.com/cprogramming/c_integer_promotions.htm 3/6
6/16/24, 11:00 AM Integer Promotions in C
In this example, the rank of the char variable is prompted to int so that its left shift
operation goes beyond the range of char type.
#include <stdio.h>
int main(){
char a = 50;
int b = a << 2;
return 0;
}
Output
The integer types in C are char, short, int, long, long long and enum.
Booleans are also treated as an integer type when it comes to type
promotions.
No two signed integer types shall have the same rank, even if they have the
same representation.
The rank of a signed integer type shall be greater than the rank of any signed
integer type with less precision.
The rank of long int > the rank of int > the rank of short int > the rank of
signed char.
The rank of char is equal to the rank of signed char and unsigned char.
https://www.tutorialspoint.com/cprogramming/c_integer_promotions.htm 4/6
6/16/24, 11:00 AM Integer Promotions in C
Example
Here, the variables x and y are of char data type. When the division operation is
performed on them, they automatically get promoted to int and the resultant value
is stored in z.
#include <stdio.h>
int main(){
char x = 68;
char y = 34;
char z = x/y;
printf("\nThe value of z: %d", z);
return 0;
}
Output
When you run this code, it will produce the following output −
https://www.tutorialspoint.com/cprogramming/c_integer_promotions.htm 5/6
6/16/24, 11:00 AM Integer Promotions in C
https://www.tutorialspoint.com/cprogramming/c_integer_promotions.htm 6/6