Type Conversions in C Language & Type Conversion Rules

Type Conversions in C Language – when an operator has operands of different types, they are converted to a common type according to type conversion rules.

This is called implicit (automatic)  type conversion.

Automatic conversions are those that convert a ‘narrower’ operand to a ‘wider’ one (no loss of information).
Example, Conversion of int to float in f+i

Expressions that might lose some information, like assigning a floating point type to an integer, may draw a warning but not illegal.

Implicit Type Conversion Rules

  1. All chars and short ints are converted to ints. All floats are converted to doubles.
  2. In case of binary operators, if one of the two operands is a long double, the other operand is converted to long double, else if one operand is double, the other is converted to double, else if one operand is long, the other is converted to long,  else if one operand is unsigned, the other is converted to unsigned.

It is possible to force an expression to be of specific type is called explicit type conversion or type casting.

 

Leave a Reply

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

%d bloggers like this: