I found a very good answer to this question on this thread
I want to understand a little bit more about why printf() can't print a floating point number as a decimal (with %d).
The program is a simple one converting Fahrenheit to Celsius degree.
I understand that %.f or %.0f is doing what i want. But when i try to do the same thing with %d, the output is unpredictable.
I searched for more detailed pieces of information cplusplus, but i don't see where it overflows or why i get this result. For example, if you use an uninitialized variable, you will get some random (or not so random) value that is in that place of memory where your variable's name is "pointing" towards. Here, what is the reason ?
float fahr, celsius;
int lower, upper, step;
lower = 0; upper = 300; step = 20;
fahr = lower;
while(fahr <= upper){
celsius = (5 / 9.) * (fahr - 32);
printf("%d\t%d\n",fahr,celsius);
fahr+=step;
}
i was expecting : 0 20 40 60 .... 300 (first column) -17, -6, -4, ... (second column)
instead i got 0 0 0 0 0 0 0 ,,, 0 (first column) 0, 1077149696, 1078198272, ... (second column)