Why does defining main() with no return type compile with no error?

后端 未结 2 1307
情话喂你
情话喂你 2021-01-21 08:16

I was just testing this code by compiling it with GCC (g++);

#include 

main(int argc, char **argv){

    printf(\"something\");

}
<         


        
2条回答
  •  孤独总比滥情好
    2021-01-21 08:35

    The program is ill-formed. Omitting the return type is not permitted by the C++ standard.

    The reason the compiler doesn't treat it as a fatal error is historical. Prior to the 1999 standard, C did permit the return type of a function to be omitted; it would default to int. C++ is derived from C, so early (pre-standard) versions of C++ had the same rule.

    In modern C++, omitting the return type is an error. The compiler is required to diagnose such an error, but it's not required to treat it as fatal. By printing a warning, the compiler has done its job as far as the standard is concerned.

    Don't ignore warnings.

提交回复
热议问题