Integer Promotions In C

C Example:

Example

#include <stdio.h>
int main() {
    char var1 = 54;
    char var2 = 48;
    char var3 = var1+var2;
    printf("var1 is : %d\n",var1);
    printf("var2 is : %d\n",var2);
    printf("var3 is : %d\n",var3);
    return 0;
}

Output:

Output

var1 is : [value]
var2 is : [value]
var3 is : [value]

Explanation:

In the provided code snippet, there are three character variables namely var1, var2, and var3. var1 is assigned the integer value 54, while var2 is assigned 48. When character variables are initialized with integer values, they are automatically converted to integers, allowing for straightforward arithmetic operations on them.

We introduce a third variable, var3, designated to hold the total of var1 and var2, resulting in it storing the sum of 54 and 48, which equals 102, and this will be outputted as an integer.

Given that a character variable in signed format spans from -128 to +127 and in unsigned format from 0 to 255, it's due to the fact that a character occupies 1 byte of memory, equivalent to 8 bits, allowing for representation within these specified ranges in both signed and unsigned forms.

When considering an integer variable, it typically occupies 4 bytes of memory, equivalent to 32 bits. Consequently, in signed format, the variable spans from -2147483648 to 2147483647, while in unsigned format, it ranges from 0 to 4294967295.

If we assign a number outside of its valid range, it may lead to overflow. This concept can be clarified through an illustrative example.

C Example:

Example

#include <stdio.h>
int main() {
    char var1 = -129;
    char var2 = 48;
    char var3 = var1+var2;
    printf("var1 is : %d\n",var1);
    printf("var2 is : %d\n",var2);
    printf("var3 is : %d\n",var3);
    return 0;
}

Output:

Output

var1 is : [value]
var2 is : [value]
var3 is : [value]

Explanation:

In the provided code snippet, we initialized var1 with a value of -129, causing it to exceed the range of a character and resulting in an overflow. As a result, the value stored in var1 will be 127. Subsequently, var2 holds a value of 48. By adding these values together and assigning the sum to var3, we get 127 + 48 = 175. Due to the overflow, the value in var3 will wrap around and be reassigned as -81.

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:

Example

#include <stdio.h>
int main() {
    unsigned char var1 = 152;
    char var2 = 152;
    char var3 = var1==var2;
    printf("var1 is : %d\n",var1);
    printf("var2 is : %d\n",var2);
    printf("var3 is : %d\n",var3);
    return 0;
}

Output:

Output

var1 is : [value]
var2 is : [value]
var3 is : [value]

Explanation:

In the scenario provided, there are three character variables denoted as var1, var2, and var3. Var1 represents an unsigned character with a value assignment of 152, while var2 signifies a signed character also assigned the value of 152. Var3 is designed to hold either the value of one if var1 and var2 are equal; otherwise, it will retain the value 0. Given that var1 is an unsigned character, its range spans from 0 to 255, with the specific value in this case being 152. On the other hand, var2 being a signed variable, the assigned value will result in overflow, leading to a final value of -104.

So variable 1 is unequal to variable 2, and variable 3 will be assigned the value of 0 or false.

We can apply identical regulations to integer promotion when dealing with short data types. Given that a short variable typically utilizes 2 bytes or 16 bits of memory, the unsigned spectrum for a short variable spans from 0 to 65535, while the signed range extends from -32768 to +32767.

C Example:

Example

#include <stdio.h>
int main() {
    unsigned short var1 = 40546;
    short var2 = 40546;
    short var3 = var1==var2;
    printf("var1 is : %d\n",var1);
    printf("var2 is : %d\n",var2);
    printf("var3 is : %d\n",var3);
    return 0;
}

Output:

Output

var1 is : [value]
var2 is : [value]
var3 is : [value]

Explanation:

The variable var1 is set to 40546, falling within the range of an unsigned short integer. On the other hand, var2 also holds the value 40546, but it experiences an overflow, resulting in it being assigned -24990. Consequently, var1 and var2 are not equivalent, and var3 is initialized as 0 or false.

Input Required

This code uses input(). Please provide values below: