Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why it's print grabage value?

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;
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading