Notice: Undefined variable: meta in /home/customer/www/demo.emertxe-group.com/public_html/wp-content/themes/Divi-child/functions.php on line 1109

Deprecated: implode(): Passing glue string after array is deprecated. Swap the parameters in /home/customer/www/demo.emertxe-group.com/public_html/wp-content/plugins/divi-filterable-blog-module/includes/controller/autoloader.php on line 94
Type Promotion in C - Emertxe

Emertxe-News & Blog

Home » Blog » Type Promotion in C
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 conversion 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:

 #include 
int main() 
{ 
 int a,c; 
char b; 
 a = 1;
b = 'a'; 
 c = a+b; 
 printf("%d",c);
 }

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:

#include 
int main() 
{ 
int a = 11; // integer a 
 char b = 'g'; // character b 
// b implicitly converted to int. 
// ASCII value of 'a' is 103 
a = a + b; 
// a is implicitly converted to double 
double c = a + 1.0; 
printf("a = %dnc = %en", a,c); 
return 0; 
}

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:

#include<stdio.h> 
  int main()
  {
  int a = 11; // integer a 
  char b = 'g'; // character b 
  
  unsigned long int d = 1; 
  float c = 3.0; 
  double e = 1.0; 
  long double f = 2.0; 
  
  d = a + d; // a is implicitly converted into unsigned int 
  c = a + c; // a is implicitly converted into float 
  e = a + e; // a is implicitly converted into double 
  f = a + f; // a is implicitly converted into long double 
  
  printf("unsigned long int d = %ldnfloat c = %endouble e = %enlong double f = %Lfn",d,c,e,f); 
  return 0; 
  }
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.

#include<stdio.h> 
  int main() 
  { 
  int i_var1, i_var2; 
  char c_var1, c_var2; 
  float f_var1, f_var2; 
  
  i_var1 = 10; c_var1 = 'A';
  f_var1 = 100.92; 
  c_var1 = i_var1; // Demotion: variable i_var1 of type int is converted into type char 
  i_var1 = f_var1; // Demotion: variable f_var1 of type float is converted into type integer
  printf("Value stored in c_var1 = %dnValue stored in i_var1 = %dn",c_var1,i_var1);
  
  i_var2 = 20; 
  c_var2 = 'B'; 
  f_var2 = 101.72;
  f_var2 = i_var2; // Promotion: variable i_var2 of type int is converted into type float 
  i_var2 = c_var2; // Promotion: variable c_var2 of type char is converted into type int
  
  printf("Value stored in i_var2 = %dnValue stored in f_var2 = %fn",c_var2,f_var2);
  }

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:

#include<stdio.h> 
  int main() 
  { 
  float result,result1; 
  int var1 = 73,var2 = 31;
  
  result = var1/var2;
  printf("Result before type casting %fn", result);
  
  result1 = (float)var1/var2; 
  printf("Result after type casting %fn", result1);
  }

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

Best Embedded systems training institute with placements in Bangalore.

YOU MAY ALSO LIKE

The Future of Embedded Systems : Trends and Predictions

The Future of Embedded Systems : Trends and Predictions

Introduction Embedded systems have become an integral part of modern technology, and their importance is increasing day by day. These systems are designed to perform specific tasks and are embedded in everyday devices, making them intelligent and interactive. From...

Online Free IoT Internship for Engineering Students – 2023

Online Free IoT Internship for Engineering Students – 2023

The Internship Ecosystem An internship is a type of professional learning opportunity where engineering students get practical work related to their field of study or career interest. Through an internship, a student can explore and advance their career while...

0 Comments

Submit a Comment

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