Originally Posted by
Amit Choubey
I tried that, I get a compile error
error: expected expression before ‘double’
Probably due to a typographical error on your part. This compiles perfectly fine for me:
Code:
#include <stdio.h>
int main(void)
{
int i = 0;
float f;
printf("%d %d %d\n", i > 0, i < 0, i == 0);
printf("%d %d %f\n", i > 0, i < 0, (double)(i == 0));
f = (i == 0);
printf("f=%f\n", (double)f);
printf("%d %d %f\n", i > 0, i < 0, (double)(i == 0));
return 0;
}
Originally Posted by
Amit Choubey
I cant understand why the results are different in the two similar printf statements.
Because you did not pass arguments of the expected type to printf, there is undefined behaviour. The results could have been the same, or the compiler could have refused to compile the program, or the compiler could have inserted malicious code to destroy your computer, etc. Realistically, what happened is that some kind of pre-condition assumption made in the implementation of printf no longer held true, and this in turn affected the output.