c - Meaning of "%lf" place holder -
here small program intently put place holder %lf
in second printf
. why second printf
has same result first printf
( both printf
1.3).
int main() { double f = 1.3; long l = 1024l; printf("f = %lf", f); printf("l = %lf", l); return 0; }
it's undefined behaviour if printf()
has format specifier mismatch. %lf
expects double
passing long int
.
c11, 7.21.6.1 fprintf function
9 if conversion specification invalid, behavior undefined.282) if argument not correct type corresponding conversion specification, behavior undefined.
that said, happens when call printf()
first time, value of f
passed in floating point register or @ location in stack double. next time call printf()
, reads same location due format specifier %lf
. opposed reading value of l
stored. if swap order of printf() calls, observe different output. platform specific. once program invokes undefined behaviour, can happen. basically, can't expect sensible , there absolutely no guarantee behaviour.
Comments
Post a Comment