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

Popular posts from this blog

SVG stroke-linecap doesn't work for circles in Firefox? -

routes - Laravel 4 Wildcard Routing to Different Controllers -

cross browser - XSLT namespace-alias Not Working in Firefox or Chrome -