Integer Promotions in C

CProgrammingServer Side Programming

There are some data types which take less number of bytes than integer datatype such as char, short etc. If any operations are performed on them, they automatically get promoted to int. This is known as integer promotions.

A program that demonstrates integer promotion in C is given as follows.

Example

 Live Demo

#include <stdio.h>
int main() {
   char x = 68;
   char y = 34;
   printf("The value of x is: %d", x);
   printf("\nThe value of y is: %d", y);
   char z = x/y;
   printf("\nThe value of z : %d", z);
   return 0;
}

Output

The output of the above program is as follows.

The value of x is: 68
The value of y is: 34
The value of z : 2

Now, let us understand the above program.

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. This is known as integer promotion. The code snippet for this is given as follows.

char x = 68;
char y = 34;
printf("The value of x is: %d", x);
printf("\nThe value of y is: %d", y);
char z = x/y;
printf("\nThe value of z : %d", z);
raja
Published on 21-Nov-2018 12:43:48
Advertisements