Sunday, January 17, 2016

Rules for Implicit Type Casting

When different operands of an expression are of different type, complier performs implicit type conversion to match them. Below are the rules followed by compiler to perform the implicit conversion.
  • Integer Promotion: All types lower than int (char, short etc) are first promoted to int.
  • If the types of operands differ even after integer promotion, then following actions are taken
    • If one of the operand is long double, convert all others to long double
    • If one of the operand is double, convert all others to double
    • If one of the operand is float, convert all others to float
    • If one of the operand is float, convert all others to float
... This operation proceeds from the highest type to lowest.
Order of the data types from highest to lowest is given below
long double > double > float > unsigned long long > long long > unsigned long > long > unsigned int > int

No comments: