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

前端 未结 10 1949
后悔当初
后悔当初 2020-12-02 00:10

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

#include 
int main(void)
{
          


        
相关标签:
10条回答
  • 2020-12-02 01:03

    This compiles well. Because it matches the printf() prototype which is

    printf(const char *,...);
    

    During run-time the call

    printf("%d\n");
    

    Tries to fetch the value from second argument and since you have not passed anything it might get some garbage value and print it out so the behavior is undefined here.

    0 讨论(0)
  • 2020-12-02 01:10

    Because of how C variadic arguments work, the compiler cannot track their correct usage. It is still (syntactically) legal to provide less, or more, parameters that the function needs to work, although this generally ends up as undefined behavior when looking at the standard.

    The declaration of printf looks like this:

    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 it is passed; it can, at best, assume that it was passed all the information it needs and nothing more.

    Contrast this with other languages, like C#:

    void WriteLine(string format, params object[] arguments);
    

    Here, the method knows exactly how many additional arguments is was passed (doing arguments.Length).

    In C, variadic functions and especially printf are a frequent cause of security vulnerabilities. Printf ends up reading raw bytes from the stack, which may leak important details about your application and its security environment.

    For this reason, Clang and GCC support a special extension to validate printf formats. If you use an invalid format string, you'll get a warning (not an error).

    code.c:4:11: warning: more '%' conversions than data arguments [-Wformat]
        printf("%d\n");
               ~~^
    
    0 讨论(0)
  • 2020-12-02 01:11

    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.

    0 讨论(0)
  • 2020-12-02 01:13

    In the context of printf() and fprintf(), as per C standard C11 clause 7.21.6.1, "If there are insufficient arguments for the format, the behavior is undefined. If the format is exhausted while arguments remain, the excess arguments are evaluated (as always) but are otherwise ignored."

    0 讨论(0)
提交回复
热议问题