×

Integer Promotions in C

As we know that some of the data types such as char, short int, Enum takes a smaller number of bytes than compared to int. When an operation is applied to these char, Enum and short int data type, these are directly further encouraged to unsigned int. Such type of promotion is known as integer promotion.

Example: When any operation is applied on short int, char, Enum like arithmetic as we know these takes lesser number of bytes, so this is firstly converted into an int or unsigned int. If int represents all original type values, then it is converted into int else the value is converted into unsigned int.

Code:

#include<stdio.h>
        int main ()
        {
             char a=20, b=30, c=10;
             char d=(a*b)/c;
             printf (“%d”, d);
             return 0;
         }

Output:

60

As we know that the signed char takes values from -128 to 127 and from the above given expression that is “(a*b)/c” appears to happen arithmetic flow but the result is 600 which is greater than 128 so, integer promotion happens in this case, and we get appropriate result without overflow.

Example:

#include<stdio.h>
            int main ()
            {
                   Char a = 0xfb;
                   unsigned char b = 0xfb;
printf (“a = %c”, a);
printf (“\nb = %c”, b);
                   if(a==b)
                         printf(“\nsame”);
                   else
printf (“\nNot Same”);
                   return 0;
             }

Output:

Not Same

From the above code the results of ‘a’ and ‘b’ is “Not Same” because at the time of comparison they both are converted into int , but ‘a’ is assigned to signed char so 0xfb becomes -5 and ‘b’ is assigned to unsigned char and it takes the value 251 as they have different values , so the result is “Not Same”.

Note:
1. Unsigned and signed char rank should always equal to char rank.
2. There should be equal ranks for unsigned char and any singed int type.
3. Extended int rank must always be smaller than standard int rank.

Arithmetic Conversions

If an operation consists of two operands, it is known as binary. When a binary operation is applied the two operands must have same type, if it is different, C impose implicit conversion to operand one and its type to other.

  • If long double is the type of each of the two operands then without any changes of domain, the next operand is also converted into long double type.
  • If double is the type of each of the two operands then without any changes of domain, the next operand is also converted into double type.
  • If float is the type of each of the two operands then without any changes of domain, the next operand is also converted into float type.

Else when integer promotions are applied on both operands then the rules are:

  • If two of them have same type then changes are not needed.
  • If both operands contain signed or unsigned int then the operand with smaller rank must be changed into operand with the higher rank.
  • If the operand is of unsigned type and rank is higher or equals to another type then that operand which is singed int must be changed to operand with type of unsigned int.

Example:

int main ()
                            unsigned char x =0;
                            unsigned char y = 1;
printf (“%u\n”, x-y);
printf (“%d\n”, x-y);
}

 As int is higher than unsigned char so integer promotion applies on the code therefore, int (x) – int (y) = int (-1) and the value of unsigned (-1) is 42949697295.

Output:

  424967295
      -1

From the above code changing one to an unsigned int

int main ()
                          unsigned int x=0;
unsigned char y=1;
                           printf (“%u\n”, x-y);
                           printf (“%d\n”, x-y);
}

As x is unsigned int, integer promotions apply to y then we obtain (unsigned int) x – (int) y, we can see the data types are still not same arithmetic conversions applies here, therefore we obtain:

(unsigned int) x – (unsigned int) y = 4294967295

Output:

4294967295
    -1

Likewise, the upcoming code results to the same output:

int main ()
                   unsigned char x =0;
                    unsigned int y=1;
                    printf (“%u\n”, x-y);
printf (“%d\n”, x-y);
             }

By changing either of them into unsigned, we obtain the following code:

int main () {
   unsigned int x =0;
                     unsigned int y=1;
printf (“%u\n”, x-y);
printf (“%d\n”, x-y);
                }

As each of them are unsigned int, integer promotions do not apply, and the output is:

4294967295
-1

We can set the code in the following way:

int main ()
              unsigned char x =0;
              unsigned char y=1;
              printf (“%u\n”, x-y);
printf (“%d\n”, x-y);
              unsigned char z = x-y;4
               printf (“%u\n”, z);
}

Output:  

4294967295
   -1
   255

Related Topics

#error #pragma in C

#error, #pragma in C #error, also known as the error directive in the C language. It will not allow you to make any compilation fail and immediately issues a statement which...

4 minutes read.

Diffie-Hellman Algorithm in C

Background: A method of public-key encryption known as elliptic curve cryptography (ECC) is based on the algebraic structure of elliptic curves over finite fields. To ensure equal security, ECC encryption requires fewer...

4 minutes read.

Top Array Keywords in C

Sorting array in C Types of array in C Array of structure in C How do you initialize an array in C Array declaration in C Array of strings in C Multidimensional array in C Reverse array...

1 minute read.

C Pointers

Pointers in C A pointer is a variable, which contains the address of another variable, i.e., it’s a variable which has the address of another variable as its value. The utilization...

4 minutes read.

Static function in C

The functions in the C programming language are by default global. This means the programmer can easily access the function which is outside from the file where it was initially...

3 minutes read.

Tower of Hanoi in C

What is Tower of Hanoi? The Tower of Hanoi is a gaming problem that was created in 1883 by a French mathematician named Édouard Lucas. The Tower of Hanoi temple in...

4 minutes read.

Prime Number code in C

Prime Number Programs in C language In this tutorial, we will learn how to check whether the given number is a prime number or not, and how to print all the...

4 minutes read.

Flow Chart of Do while loop in C

This is a flowchart that represents the process of executing the Do while loop in the C programming language Generally, as we know there are three main components of Do While...

3 minutes read.

C Program to find the size of a File

Before knowing about the Program, you need to understand what the size of a file is. After that, you should know how it is going to work with the given...

4 minutes read.

Null character in C

Introduction The Null character in the C programming language is used to terminate the character strings. In other words, the Null character is used to represent the end of the string...

3 minutes read.

Scope of variables in C

Introduction The scope of variables in C can be defined as the scope of reach of a variable, the term scope is used to determine the visible range of an object....

4 minutes read.

Union in C

Union is a user-defined data type that is used to hold the different types of elements like structure. In union, all members share the same memory location. Syntax: union [union name] { ...

1 minute read.

Difference between rand() and srand() function in C

What is the rand()?The rand() means random function. This function is used in C. It generates random numbers in the range of 0 to the RAND_MAX. Suppose we generate a...

3 minutes read.

Int in C

The keyword int in C programming stands for integer and it is a data type which is used for variable declarations or declaration of functions of different types. Similar to...

4 minutes read.

#define in C

#define in C In the C programming language, the preprocessor directive acts an important role within which the #define directive is present that is used to define the constant or the...

3 minutes read.

Multiplication table program in C using For loop

Before we move on the program of multiplication table of any natural number, first we have to know about the For loop statement. The syntax of ‘for’ loop in C programming...

3 minutes read.

Sum of digits in C++

Sum of digits in C++ There are various ways to find the sum of the digits of a number in C++. We can use containers like arrays or other simple cases...

4 minutes read.

Ferror() in c

Ferror():  In C, the ferror() function checks assuming there is a blunder in the given stream. The ferror() function is utilized to check for the document blunder on given stream. A return...

4 minutes read.

Do-While Loop in C

Both 'While' and 'Do-While' loops in C mostly have a similar concept, and the code of both loops runs for mainly similar purposes in the same manner. But, there is...

3 minutes read.

Merge and Merge sort with example in C

Assume we have two ordered Integer arrays, a[] and b[]. If we wish to combine them into another ordered array, such as c[], we can use the following straightforward technique. Compare...

3 minutes read.