Integer and Float Conversion
Integer and Float conversion
In order to effectively develop C program, it will be necessary to
understand the rules that are used for the implicit conversion of
floating point and integer values in C. These are mentioned below.
Note them carefully:
- 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 real
and then the operation is performed. Hence the result is real.
I think a few practical examples shown below would put the
issue beyond doubt.
Operation | Result |
---|---|
5 / 2 | 2 |
5.0 / 2 | 2.500000 |
5 / 2.0 | 2.500000 |
5.0 / 2.0 | 2.500000 |
2 / 5 | 0 |
2.0 / 5 | 0.400000 |
2 / 5.0 | 0.400000 |
2.0 / 5.0 | 0.400000 |
No comments