So I have the following code snippet in C:
int a = 25, b = 100, c;
if (b++ <= (a * 4))
c = 3;
else
c = 20;
printf ("%f\n", (float) (b / c));
When I ran the code, the output returned the value 33.0000. I was expecting the output to be 33.6667. I was wondering why was it so? My calculations are as follows:
Thank you for your help!!
The expression b / c
is performing integer division because both operands are integers. This means the resulting value gets truncated.
If you want to perform floating point division, cast one of the operands to float
.
printf ("%f\n", (float)b / c);