-1

In this code I want to print floating point value of 9/5 which is 1.8 than why it print the garbage value. why I try to print the int value than it print 1 which is correct but why it print grabage rather than printing 1.8?

#include <stdio.h>

int main() {
      printf("%f", 9/5);
      //printf("%d", 9/5);
    
    return 0;
}

1 Answers1

2

The type of a variadic argument (argument in the ... part of the function declaration) passed to a function depends only on what you pass to the function (in your case 9/5), not on the string argument. 9 and 5 are of type int, so their quotient is the fractional quotient converted to int by truncation towards zero, which is 1. Then, by integer promotions, int is converted to int (if you passed (short) (9/5), then by integer promotions, this would also have been converted to int).

Kolodez
  • 553
  • 2
  • 9