Friday, July 29, 2016

C Basics - Type Conversion in C

In C type conversion, typecasting, and coercion are different ways of, implicitly or explicitly, changing an entity of one data type into another. This is done to take advantage of certain features of type hierarchies or type representations.

Implicit Type Conversion:
Implicit type conversion, also known as coercion, is an automatic type conversion by the compiler. In a mixed-type expression, data of one or more subtypes can be converted to a supertype as needed at runtime so that the program will run correctly.

#include <stdio.h>

int main(void)
    int i_value   = 16777217;
    float f_value = 16777216.0;
    printf("The integer is: %d\n", i_value);
    printf("The float is:   %f\n", f_value);
    printf("Their equality: %d\n", i_value == f_value);
On compilers that implement float as IEEE single precision, and int as at least 32 bits, this code will give this peculiar print-out:
The integer is: 16777217
The float is: 16777216.000000
Their equality: 1

Note that 1 represents equality in the last line above. This odd behavior is caused by an implicit conversion of i_value to float when it is compared with f_value. The conversion causes loss of precision, which makes the values equal before the comparison.
Important takeaways:
  • float to int causes truncation, i.e., removal of the fractional part.
  • double to float causes rounding of digit.
  • long long to int causes dropping of excess higher order bits.
It is necessary to understand the rules that are used for the implicit conversion of floating point and integer values in C.
  • An arithmetic operation between an integer and integer always yields an integer result.
  • An operation between a real and real always yields a real result.
  • An operation between an integer and real always yields a real result. In this operation the integer is first promoted to a real and then the operation is performed. Hence the result is real.
Operation Result Operation Result
7 / 3 2 3 / 7 0
7.0 / 3 2.333333 3.0 / 7 0.428571
7 / 3.0 2.333333 3 / 7.0 0.428571
7.0 / 3.0 2.333333 3.0 / 7.0 0.428571

The value of the expression is promoted or demoted depending on the type of the variable on left-hand side of =.
Expression Result Expression Result
int x = 1.9 x = 1 float y = 19 y = 19.000000
int x = 1+9.0 x = 10 float y = 1+9 y = 19.000000

Explicit Type Conversion:
Explicit type conversion is a type conversion which is explicitly defined within a program (instead of being done by a compiler for implicit type conversion).
Consuider the following,

double da = 3.3;
double db = 3.3;
double dc = 3.4;
int result = (int)da + (int)db + (int)dc;

The value stored in result variable is 9.
if implicit conversion would be used (as with "result = da + db + dc"), result would be equal to 10

Related topics:
Overview of Storage Class in C   |   Overview of Operators in C   |   Expression in C   |   Overview of Instruction in C   |   Overview of Statements in C

List of topics: C Programming

No comments:

Post a Comment