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

前端 未结 10 1952
后悔当初
后悔当初 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: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.

提交回复
热议问题