2 ms·
There is no printf because there is no defined behavior. To call a variadic function, you must have a prototype declaration in scope. (A correct prototype, nee
by kazinator 5y ago
There is no printf because there is no defined behavior.
To call a variadic function, you must have a prototype declaration in scope. (A correct prototype, needless to say.)
A non-prototype (i.e. old-style) declaration cannot declare a variadic function such as printf, so no such declaration can be correct.
If the function is not declared, then a declaration will effectively be assumed for the call, deduced from the types of the actual arguments, and a return value of int. In this case, the function will be "implicitly declared" to look something like
int printf(char *);
but that is not the correct declaration for printf. Therefore, the call has undefined behavior.
The optimizer is allowed to rewrite this to:
puts("daemons are running in your nose");
Likewise, it is allowed to rewrite it to:
puts("Hello World!");
- lifthrasiir 5y agoNo, this optimization also happens when you `#include <stdio.h>`. More accurately speaking, GCC (among others) evidently assumes that printf is the standard C library function no matter prototype is defined or not and replaces it with puts under the same assumption plus the following additional conditions: - No other arguments are present. - The format argument has no format specifiers and ends with `\n`. - The return value is unused. (So `return printf("...\n");` would have prevented this optimization.)