can't understand why this output is generated

89 Views Asked by At

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

3

There are 3 best solutions below

1
Eric Postpischil On BEST ANSWER

for the next %6.1f ** why it is printing the 0.0 instead of the -17.8 output of the equation

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 printf is:

  • You pass the format string and two floating-point arguments.
  • The format string is automatically converted to a pointer to its first element. Since it is a pointer it is stored in R7.
  • Since fahr has a floating-point type, it is stored in F4.
  • Since celsius has a floating-point type, it is stored in F5.
  • printf is called.
  • printf examines the format string. To do this, it uses the pointer in R7.
  • printf sees %3.0d in the format string. d tells 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. printf prints this value.
  • printf sees %6.1f in the format string. f tells 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 of fahr in F4. printf prints this value.
  • printf never gets to F5, so it does not print the value of celsius.

The behavior of calling printf with 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.

1
gulpr On
printf("%3.0d %6.1f\n",fahr,celsius);

%d is to print an integer and your fahr is float. It invokes Undefined Behaviour. Change to:

printf("%3.0f %6.1f\n",fahr,celsius);

https://godbolt.org/z/rxP1zWfKa

  • Also, use double instead of float. If you use MCU without FPU and 16KB of flash use float
  • use the correct main prototype (you forgot the return type which is int)
3
Abhishek Verma On

Looks like there is some issue with data type declaration, you have declared 'fahr' of float data type in line number 4, but while printing the value of 'fahr' you are using '%d' which is used for int type formatting. Just replace '%3.0d' with '%3.0f' and you are good to go.