Destructor Destructor - 1 year ago 70
C Question

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

I tried the below c program & I expected to get compile time error, but why compiler isn't giving any error?

#include <stdio.h>
int main(void)
return 0;

Why output is compiler dependent?
Here is the output on various compilers

Output on Orwell Dev C++ IDE (uses gcc 4.8.1) : 0

Output on Visual C++ provided by Visual Studio 2010 : 0

CodeBlocks IDE (uses gcc 4.7.1) : garbage value

Online compiler : garbage value

Please help me.

Answer Source

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, 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.