i...">

EOF output using% f

#include<stdio.h>
int main()
{
    printf("%d",EOF);
}

      

generates -1 which is perfectly fine, but

#include<stdio.h>
int main()
{
    printf("%f",EOF);
}

      

produces 0.000. How can someone explain this when the expected result is -1.000?

+3


source to share


2 answers


Using an invalid format specifier for any particular argument in printf()

causes undefined behavior .

EOF

has a type int

. You can only use %d

for variable type int

.



FWIW, if you want floating point representation int

you need a cast

variable (but I personally recommend avoiding this)

printf("%f",(float)EOF);

      

+5


source


EOF

is of type int

( signed

). You must not use the wrong format specifier for printing int

, or it will cause undefined behavior.



+5


source







All Articles