Monday, December 12, 2016

Type Casting or Conversion

The C programming language has ability to convert one type of the data into another type tempoparily. This is called as type casting or type conversion. When a C expression contains different types of the data, the result of that expression is always be in highest data types in the
expression.

int x = 10, y = 4;
float result;
result = x / y;
printf(“Division: %f”, result);

But, it is not the accurate answer. So type casting is required. Such as,
result = (float) x / y;
This statement will convert value of x temporarily into float type. So
result of expression will automatically be float. Now the printf statement
will output:
Division: 2.500000
This concept is referred as type casting, which is found to be one of the
useful concepts in ‘C’.

No comments:

Post a Comment