Due to the way C variable variables work, the compiler cannot track their proper use. It is still (syntactically) legal to provide fewer or more parameters that a function should perform, although this usually looks like undefined behavior when viewing a standard.
The printf declaration is as follows:
int printf(const char*, ...);
The compiler only sees ... and knows that there may be zero or more additional arguments that the function may or may not use. The called function does not know how many arguments are passed; he can, at best, assume that all the information that he needs has been transferred to him, and nothing more.
Compare this with other languages like C #:
void WriteLine(string format, params object[] arguments);
Here, the method knows exactly how many additional arguments were passed (execution of arguments.Length ).
In C, function variables and especially printf are a common cause of security vulnerabilities. printf finishes reading raw bytes from the stack, which could leak important information about your application and its security environment.
For this reason, Clang and GCC support a special extension for checking printf formats. If you use the wrong format string, you will receive a warning (not an error).
code.c:4:11: warning: more '%' conversions than data arguments [-Wformat] printf("%d\n"); ~~^
zneak source share