Loading....
Coupon Accepted Successfully!

 

Integer Promotion

Type casting is a way to convert a variable from one data type to another data type. For example, if you want to store a long value into a simple integer then you can type cast long to int. You can convert values from one type to another explicitly using the cast operator as follows:

 

(type_name) expression

Integer promotion is the process by which values of integer type "smaller" than int or unsigned int are converted either to int or unsigned int. Consider an example of adding a character in an int:

#include <stdio.h>

main()
{
   int  i = 17;
   char c = 'c'; /* ascii value is 99 */
   int sum;

   sum = i + c;
   printf("Value of sum : %d\n", sum );

}

When the above code is compiled and executed, it produces the following result:

Value of sum : 116

Here, value of sum is coming as 116 because compiler is doing integer promotion and converting the value of 'c' to ascii before peforming actual addition operation.

Using Arithmetic Conversion

The usual arithmetic conversions are implicitly performed to cast their values in a common type. Compiler first performs integer promotion, if operands still have different types then they are converted to the type that appears highest in the following hierarchy:


 

The usual arithmetic conversions are not performed for the assignment operators, nor for the logical operators && and ||. Let us take following example to understand the concept:

#include <stdio.h>

main()
{
   int  i = 17;
   char c = 'c'; /* ascii value is 99 */
   float sum;

   sum = i + c;
   printf("Value of sum : %f\n", sum );

}

When the above code is compiled and executed, it produces the following result:

Value of sum : 116.000000




Test Your Skills Now!
Take a Quiz now
Reviewer Name