Type conversion Implicit and Explicit in c

12 Aug 2022 Balmiki Mandal 0 C Programming

Type conversion / type Promotion in c 

When the constant and different variable in between the same operator is There then they all are converted into the same operand. The compiler converts all operands up to the type of biggest Operand 

Example

char ch;
int I, Result;
float f;
double d;

Result=  ( ch/i +  f*d  -  f-i )

Conclusion: here char is converted into int and float is converted into double. so any primitive type of data converted into another primitive type of data is called type Type promotion or type conversion.

Implicit conversion is done by the compiler and Explicit conversion is user-defined conversion.

There are two of type conversion

  1. implicit type conversion
  2. Explicit type conversion

Implicit Type conversion

Implicit Type conversion bone by the compiler we no need to do any anything. it also in two types 

  1. Automatic Type conversion
  2. Type conversion in assignment

 Automatic type conversion also two types

  1. Automatic unary conversion
  2. Automatic binary conversion

Automatic type conversion also has two types

Automatic unary Type conversion (integral promotion): all character and short integer values are automatically converted into integers is called integral or unary promotion 

Automatic Binary conversion: if one operand is a long double other will be converted into a long double 

Type conversion in assignment

  • in Expression, if data types or operand are different then Right-hand side data is converted into left-hand side data of the operand.
  • if the right-hand side operand is lowest than the left-hand side operand then the right hand side operand is promoted.
  • if the right-hand side operand is higher than the left-hand side operand then right-hand side operand is promoted
  • some higher order bits are dropped when long is converted into int or int is converted into short int or char
  • the fractional part is truncated during the conversion of float to int 
  • when double is converted into float then digits are round off
  • when the signed type is changed to an unsigned type then sign may be dropped
  • when int is converted into float or float is converted into double ther will be no increase in accuracy or presion 

Explicit type conversion(type casting):

Type conversion / type Promotion:

When the constant and different variable in between the same operator is There then they all are converted into the same operand. The compiler converts all operands up to the type of biggest Operand 

Example

char ch;
int I, Result;
float f;
double d;

Result=  ( ch/i +  f*d  -  f-i )

Conclusion: here char is converted into int and float is converted into double. so any primitive type of data converted into another primitive type of data is called type Type promotion or type conversion.

Implicit conversion is done by the compiler and Explicit conversion is user-defined conversion.

There are two of type conversion

  1. implicit type conversion
  2. Explicit type conversion

Implicit Type conversion

Implicit Type conversion bone by the compiler we no need to do any anything. it also in two types 

  1. Automatic Type conversion
  2. Type conversion in assignment

 Automatic type conversion also two types

  1. Automatic unary conversion
  2. Automatic binary conversion

Automatic type conversion also has two types

Automatic unary Type conversion (integral promotion): all character and short integer values are automatically converted into integers is called integral or unary promotion 

Automatic Binary conversion: if one operand is a long double other will be converted into a long double 

Type conversion in assignment

  • in Expression, if data types or operand are different then Right-hand side data is converted into left-hand side data of the operand.
  • if the right-hand side operand is lowest than the left-hand side operand then the right hand side operand is promoted.
  • if the right-hand side operand is higher than the left-hand side operand then right-hand side operand is promoted
  • some higher order bits are dropped when long is converted into int or int is converted into short int or char
  • the fractional part is truncated during the conversion of float to int 
  • when double is converted into float then digits are round off
  • when the signed type is changed to an unsigned type then sign may be dropped
  • when int is converted into float or float is converted into double ther will be no increase in accuracy or presion 

 

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.