C / C++ FAQs & Programming Resources - ProkutFAQ : InsufficientFormatStringArguments

HomePage Recent Changes Recently Commented Login/Register
Quick Links
Categories
Links

What is the output of printf("%d"); ?


This statement invokes undefined behaviour, because the C langauge standard says "If there are insufficient arguments for the format, the behavior is undefined". In some compiler implementations, this might end up printing the values of the previously declared integer, which is stored in the stack, but this is not a standard behaviour, and should not be relied upon. This might print garbage value on some implementations, and on some others this can even crash the program.

There is a known as format string vulnerability which was often used to crash programs a few years back. A string of printf("%s") would attempt to read data from the stack (on implementations where a stack is supported) till it attempts to read from an area which it is not supposed to access and hence would ultimately crash the program.
 Comments [Hide comments/form]
I doubt if printf("%d"); will lead to a crash -> but it definitely is UB.
printf("%s"); on the contrary could crash the program.
-- byethost15.com (2006-11-28 23:18:18)
I never said that printf("%d") would lead to a crash. I was referring to printf("%s");
-- PriyaSridharan (2006-12-11 20:27:00)
Page was generated in 0.0158 seconds