Javatpoint Logo
Javatpoint Logo

Integer Promotions in C

There are a lot of data types we use in C language like integers, float, short, character etc. Each type of data type has its own size in bytes and the limit of the number they can have.

For example, an integer variable takes 4 bytes, whereas a character variable takes 2 bytes of memory. The integer is one of the most used data types because we can do arithmetic operations on it easily. When we do arithmetic operations on non-integer variables like character or short, then the compiler considers them as integer variables and converts them into their equivalent integer value. This phenomenon is called integer promotion.

C Example:

Output:

Integer Promotions in C

Explanation:

In the above code, we have three character variables var1,var2 and var3. We have initialized the var1 with the value 54 and var2 with the value 48. Now since we have initialized the character variables with the integer value, then these variables are promoted to integer, and we can do arithmetic operations on these variables easily.

We have a third variable, var3, which will store the sum of var1 and var2 so that it will store the value 54+48 = 102, and we will print it as an integer.

Since we know the range of a character variable in signed format is from -128 to +127 and in the unsigned format, it is from 0 to 255, because character takes 1 byte of memory and it is equal to 8 bits which can be represented in a signed and unsigned format with the above range.

If we talk about an integer variable, then it takes 4 bytes of memory or 32 bits. So the variable range in signed format is from -2147483648 to 2147483647, and in the unsigned format, it is from 0 to 4294967295.

If we assign any number out of their range, then overflow can be possible, so we can understand it with the example.

C Example:

Output:

Integer Promotions in C

Explanation:

In the above code, we have assigned var1 with value -129, which is smaller than the range of a character so that it will overflow, and the value will be 127 assigned to var1. Now, var2 has the value of 48, and we will add these values and assign them to var3, which will be 127+48 = 175. so the value in var3 will overflow, and it will again assign as -81.

Integer Promotions in C

Note: by default, integer promotion of a variable is done in signed format. If we use an unsigned variable, then we can have a signed format value.

C Example:

Output:

Integer Promotions in C

Explanation:

In the above example, we have three character variables var1, var2 and var3. Var1 is an unsigned character which is assigned a value of 152, and var2 is a signed character which is also assigned the value of 152. The var3 will store either one if var1 and var2 are equal; otherwise, it will store the value 0. Since var1 is an unsigned character, the range is from 0 to 255, and the value is assigned 152.var2 is a signed variable, so there will be overflow and the value assigned will be -104.

So var1 is not equal to var2, and var3 will get the value 0 or false.

We can have the same rules for the integer promotion in short variables. Since one short variable occupies 2 bytes or 16 bits of memory. So the unsigned range of a short variable is 0 to 65535, and the signed variable is from -32768 to +32767.

C Example:

Output:

Integer Promotions in C

Explanation:

The var1 is assigned 40546, which is in the range of unsigned short and var2 has the same value, but it is overflow, and it will be assigned -24990. So, var1 and var2 are not equals, and var3 is assigned 0 or false.


Next TopicBit Fields in C





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA