Type Promotion in C

Type promotion in C

Introduction

Type promotion in C is a method to convert any variable from one datatype to another. C allows variables of different datatypes to be present in a single expression. There are different types of type conversions available in C. They are Implicit type conversion and Explicit type conversion.

Different types of type conversions are mentioned in the below image. Let us understand each conversion with an example as a part of this blog post.

Type promotion in C

Fig 1: Type conversion diagram

Implicit Type Conversions

Implicit type conversions are done by compilers. Implicit type conversions is also called as coercion. Only some programming languages allow compilers to provide implicit type conversion. These conversions are done by C compilers according to the predefined rules of C language. Here is an example to understand implicit type conversion.

Example:

Fig 2: Implicit type conversion example

Output:

Type promotion in C

Fig 3: Output image of implicit type conversion example

In the above given example, variable a is of type int and variable b is of type char. When addition is done in line 8 and stored in variable c and when we print it (line 9), we get the output as 98. ASCII value of variable ‘a’ is 97 added with 1. Thus char variable b is implicitly type converted to integer.

Implicit type conversion can be further classified into automatic type conversion and type conversion in assignments. Automatic type conversion can be further split into unary and binary. Let us understand each one of the conversions with an example.

Automatic Unary and Binary Conversions

Unary

In any given expression, all operands of type char and type short will be converted into datatype int even before any operation is performed. Some compilers convert float operand to double before any operation is performed.
This is Automatic Unary Conversion.

Example:

Fig 4: Automatic Unary Conversion Example

Output:

Type promotion in C

Fig 5: Output image of Automatic Unary Conversion example

In the above given example, variable c is of type double and variable a is of type int. In line 11, variable a is implicitly converted into double and added with value 1.0. The final output is stored in variable c.

Binary

In any given expression, if one of the operands is long double or double or float or unsigned long int, the other operand will be converted into long double or double or float or unsigned long int respectively.

Example 1:

Fig 6: Automatic Binary Conversion Example

Output:

Type promotion in C

Fig 7: Output image of Automatic Binary Conversion example

In the above given example, variable a in line 12 is implicitly converted into unsigned int since variable ‘d’ is of type unsigned int, in line 13 variable ‘a’ is implicitly converted into type float since variable ‘c’ is of type float, in line 14 variable ‘a’ is implicitly converted into type double since variable ‘e’ is of type double, in line 15 variable ‘a’ is implicitly converted into type long double since variable ‘f’ is of type long double.

  • In an expression, if one operand is long int and the other operand is unsigned int and if long int could hold all values of unsigned int, then the unsigned int variable will be converted into long int. Otherwise both the variables will be converted into unsigned long int.
  • In an expression, if any one of the operands is long int or unsigned int the other variable will be converted into long int or unsigned int respectively.
  • Otherwise both operands in an expression will be int and the result will also be int.

Type promotion in C

Fig 8 : Image describing higher rank and lower rank datatypes

Whenever there are two operands of different data types in an expression, the operand with a lower rank will be converted to the datatype of higher rank operand.

Type Conversion In Assignment

Consider an assignment expression, where the types of two operands are different. The datatype of operand in Right Hand Side will be converted into datatype of operand in Left Hand Side. During this type conversion, either promotion or demotion of operand on right hand side.

Promotion

During datatype conversion, if operand on the right hand side is of lower rank type then it becomes the type of left hand side operand which is of higher rank. This kind of type conversion from lower rank to higher rank is called Promotion.

Demotion

During datatype conversion, if operand on the right hand side is of higher rank type then it becomes the type of left hand side operand which is of lower rank. This kind of type conversion from higher rank to lower rank is called Demotion.

This promotion and demotion of type gives rise to some consequences. They are described below.

1. Some high order bits may be dropped when long is converted to int, or int is converted to short int or char.

2. Fractional part may be truncated during conversion of float type to int type.

3. When double type is converted to float type, digits are rounded off.

4. When a signed type is changed to unsigned type, the sign may be dropped.

5. When an int is converted to float, or float to double there will be no increase in accuracy or precision.

Fig 9: Example describing type conversion in assignment

Output:

Type promotion in C

Fig 10: Output image of Example describing type conversion in assignment

In the above given example, integer variable i_var1 and float variable f_var1 are demoted in line 12 and line 13, whereas in line 20 and line 21, integer variable i_var2 and character variable c_var2 are promoted to type float and int respectively.

Explicit Type Conversion or Type Casting

Generally in some cases, implicit type conversion will not solve the required purpose for which the expression is written. In that case, we could specify/write our own conversions. This is known as type casting/coercion and it can be achieved with the help cast operator. Cast operator is an unary operator specially used for the purpose of converting an
expression to a particular datatype temporarily, where the expression can be of any constant or variable.

Syntax:

(datatype)expression

Example:

Fig 11: Example explaining Explicit type conversion

Output:

Type promotion in C

Fig 12: Output image of explicit type conversion example

In the above given example, var1 and var2 are two variables of type int declared and initialized with values in line 5. When we try to divide var1 by var2 and save it in a float variable ”result” we don’t obtain the exact value. All decimal values are truncated.

When we type cast var1 as float as seen in line 10 we will obtain the decimal values in the result without getting lost. Whenever cast operator is used in an expression, floating point arithmetic is performed. Hence the value of result1 is 2.35. This is because of the cast operator which temporarily typecasts var1 as float for evaluation of this expression. Note that the variable var1 is of type integer throughout the program other than line 10.

Let us understand some more examples of usage of cast operator here.

(int)100.23223

Here constant 20.3 is converted to integer type. Hence the fractional part is lost and final result will be 100.

(float)100/3

Here constant 100 is converted to float type, then it is divided by 3. The value obtained is 33.33.

(float)(100/3)

Here at first, 100 is divided by 3 and then the result is converted into float type. The result will be 33.00.

(double)(x +y -z)

Here in the above expression result of expression x+y-z is converted to double.

(double)x+y-z

Here in the above expression x is first converted to double and then used in expression.

Conclusion

Though there are many promotion and demotion of types occurs as an effect of type conversions, they are of great help while dealing with variables of different data types in an expression. Especially in explicit type conversion, where a datatype change occurs temporarily only in expression where cast operator is used.

Happy Learning!!!

About

View all posts by

3 thoughts on “Type Promotion in C

  1. In fig 4: Automatic unary conversation example,
    Line 10, variable a is implicitly concerted into double not as float and added with 1.0, because if we right simply 1.0 it will read as double defaultly not as float. Am I right sir?

Leave a Reply

Your email address will not be published. Required fields are marked *