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

Thursday, January 14, 2016

Implicit and Explicit Type Casting

Type casting can either be performed by the compiler automatically or user can specify them using the syntax described above. Former type is called implicit type casting and the latter is called explicit type casting.
What is implicit type casting/ implicit type conversion?
We've explained the explicit type conversion in the above paragraphs. Now lets's take a look at how implicit type conversion works. In implicit type conversion compiler takes care of the casting without requiring the programmer to specify them explicitly. For example in the program given below, compiler converts the sum of two float numbers to an int.
  1. ##include <stdio.h>
  2.  
  3. int main()
  4. {
  5. float value1 = 2.2;
  6. float value2 = 3.3;
  7. int result;
  8.  
  9. result = value1 + value2;
  10. printf("Result : %d", result);
  11. return 0;
  12. }
  13.  
  14. Output:
  15. Result : 5
Note: Though compiler performs implicit type casting, it's always recommended to specify the conversion explicitly because this improves the code readability and makes it easier to port to other platform.

Wednesday, January 13, 2016

C Type Casting with examples - float to int / int to char / int to long

The value of a variable or expression can be converted to another data type if desired. It is called type casting or type conversion. If you have a variable of data type long and you want to assign it to an int type variable, type casting can be used for that purpose. To type cast a variable or expression it must be preceded by the desired datatype as follows.
(data type) expression;

Why do we need type casting?

Type casting would be pretty handy when your inputs to an operation is of a particular type and you need more precision in the output. Let's see the use of type casting through a simple example
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5. int value1 = 10, value2 = 3;
  6. float result;
  7. result = value1 / value2;
  8. printf("Result without type casting: %f", result);
  9. return 0;
  10. }
  11.  
  12. Output:
  13. Result without type casting: 3.000000
In the above example we assign the result of division operation to float type variable result. The result is supposed to be 3.333333. But we get the result as 3.000000. Can you think why? Because the two operands of the expression, value1 and value2 are of int type. This causes compiler to treat the result also as integer; that means, it will ignore the decimal part of the result and store the integer result in float variable.