int
a;
double b, c;
a = 3;
b = 5.1;
c= a + b;
When adding an int to a double, the int is converted to a double for the purpose of adding. The memory space
retains the int. The
location of the sum, c,
must be a double. |
int
a, b;
double c;
b = 21;
a = 5;
c = b/a;
Integer division takes place
and gives an answer of 4. This answer is
stored as a double 4.0.
But what if we
wanted the correct division answer ... |
c = ( double)
b/a;
c= 21.0 / 5
c = 4.2
It is possible to force the type you want by type casting.
Be careful to force the double to either the numerator or denominator, not both.
Note:
c = ( double) (b/a);
would give an answer
4.0 since integer division would be done first. |