here i mistaken in printf statement by trying to print a float value in %d format specifier so it gives a random value it is ok but for the next %6.1f ** why it is printing the 0.0 instead of the -17.8 output of the equation ** code
#include <stdio.h>
main()
{
float fahr, celsius;
int lower, upper, step;
lower = 0;
upper = 300;
step = 20;
fahr = lower;
while (fahr <= upper){
celsius = (5.0/9.0) * (fahr-32.0); // equation
printf("%3.0d %6.1f\n",fahr,celsius); // this line is confusing
fahr = fahr + step;
}
}
output
-1693570760 0.0
32109216 20.0
32109216 40.0
32109216 60.0
32109216 80.0
32109216 100.0
32109216 120.0
32109216 140.0
32109216 160.0
32109216 180.0
32109216 200.0
32109216 220.0
32109216 240.0
32109216 260.0
32109216 280.0
32109216 300.0
help me to understand how to this code is executed and why the output is 0.0 and not -17.8
A common arrangement for passing arguments to routines is that integer or pointer arguments are passed in specific general processor registers, say R7, R8, and R9, and that floating-point arguments are passed in specific floating-point registers, say F4, F5, F6, and F7. (If there are more arguments of a type than registers for it, then the further arguments are passed on the stack.)
When you use arguments with incorrect types, the mismatch between registers the calling routine uses and registers the called routine uses causes the behavior you observe. Using these fictional registers as an example, what happens in your
printfis:fahrhas a floating-point type, it is stored in F4.celsiushas a floating-point type, it is stored in F5.printfis called.printfexamines the format string. To do this, it uses the pointer in R7.printfsees%3.0din the format string.dtells it to look for an integer argument. So it gets a value from the next register for integer arguments, R8. The caller did not store an argument in R8; it contains whatever value is left over from other work the program did.printfprints this value.printfsees%6.1fin the format string.ftells it to look for a floating-point argument. So it gets a value from the next register for floating-point arguments, F4. The caller put the value offahrin F4.printfprints this value.printfnever gets to F5, so it does not print the value ofcelsius.The behavior of calling
printfwith mismatched argument types is not defined by the C standard, but you should recognize this common behavior as a symptom of mismatching the argument types.