×

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

Actual and Formal Parameters

Any variable declared within the parenthesis is referred to as the parameters during the function declaration. Parameters tell the function about the argument datatype, their order, and the number of...

5 minutes read.

Fibonacci Series in C Using For Loop

In this C article, we will let you know about the procedure of displaying the Fibonacci series of the first “n” positive integers. The syntax of for loop used in the...

4 minutes read.

Memory leak in C

What is memory leak in C? Memory leak occurs when we keep allocating memory in the heap without freeing it, i.e., the allocated memory in heap is not released back to...

3 minutes read.

One Dimensional Array in C

One Dimensional Array in C: In the C programming language, an array is defined as a collection of one or more values of the same type. Every value present in...

4 minutes read.

Positioning of file in C

Positioning() function in c The fseek() function is utilized to change the document position of the stream . The fully extent of value from the source should be one of the...

3 minutes read.

C Program of Fencing the Ground

The college's ground is rectangular. Fencing the ground the management makes the decision to construct a fence around the ground. They planned to wrap a thick rope around the ground...

1 minute read.

Length of an Array Function in C

In C, there is no built-in function to get the length of an array. However, there are a few ways you can determine the length of an array. It is necessary...

15 minutes read.

Recursion in C

Recursion is a programming technique that allows the programmer to call the function within the same function. The function which calls the same function is known as recursive function but a...

1 minute read.

Find a subarray with a given sum.

Find a subarray with a given sum. The simple solution is to recognize all subarrays one by one and to check each subarray's sum. The quick solution follows the following program. Algorithm: From...

4 minutes read.

Bit Fields in C

The size of a structure in C is specified in bits. The primary goal of it is to use memory efficiently, once we understand that a bit's value must fall...

3 minutes read.

Modulus on Negative Numbers in C

In this tutorial, we will explore some examples of modulus on negative number. What is Modulus of Negative number? By omitting the minus sign, one can determine the modulus of a negative...

3 minutes read.

C Program to Find Largest Number Using Dynamic Memory Allocation

This tutorial will teach us how to locate the highest number a user has ever input into a dynamically allocated memory. C Program: #include <stdio.h> #include <stdlib.h> int main ()  {   int no;   double *data_n;   printf...

1 minute read.

Array Example in C

An array is a collection of similar types of data elements arranged in such a way that any number of values can be assigned to it. It can store values that...

4 minutes read.

Garbage in C

Garbage in C The C programming language is a perfect fit for embedded systems as it provides low level control, structured programming, and portability. On the contrary, it does not provide...

3 minutes read.

Multi-dimensional Arrays in C

Multi-dimensional Arrays in C The C programming allows the concept of multi-dimensional arrays. The data within the multidimensional arrays are stored in the tabular form, that is, in a row significant...

3 minutes read.

GPA Calculator in C

GPA stands for Grade Point Average. This GPA is used to measure the student's academic performance in educational institutes. By using this, segregation takes place and lets us know how...

4 minutes read.

C Program Swap Numbers in cyclic order Using Call by Reference

The three integers that the user entered in cyclical sequence are swapped in this tutorial using call by reference. C Program: #include <stdio.h> void cyclic_Swap ( int *X, int *Y, int *Z ); int...

2 minutes read.

getch() function in C

getch() is a pre-define or built-in function present in the conio.h library. It returns the given character immediately without waiting for the enter key to be entered. By using getch()...

3 minutes read.

Expressions in C

Expressions in C: In the C programming language, an expression defines a formula in which the operands are linked to each other by using operators to compute the value. The operand...

4 minutes read.

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...

3 minutes read.