printf() with no arguments in C compiles fine. how?

Your program will compile fine, as printf() is a variadic function and the matching check of the number of format specifiers with supplied argument is not performed by default.

At runtime, your program exhibits undefined behaviour, as there in no argument supplied which has to be printed using the supplied format specifier.

As per chapter 7.19.6.1, c99 standard, (from fprintf())

If there are insufficient arguments for the format, the behavior is
undefined.

If you compile using -Wformat flag in gcc, your compiler will produce the warning for the mismatch.

Leave a Comment