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.