Why does `int ;` compile fine in C, but not in C++?

后端 未结 3 1817
情歌与酒
情歌与酒 2020-12-09 07:04

Consider the following program (see live demo here).

#include 
int main(void)
{
      int ;  // Missing variable name
      puts(\"Surprise\")         


        
3条回答
  •  醉话见心
    2020-12-09 08:04

    Your code is illegal (i.e. erroneous, ill-formed, constraint-violating) in both C and C++. The reason you get a "warning" in one language and "error" in another is just a quirk of your compiler and your compiler setup. After all, neither language really formally differentiates between "warnings" and "errors". GCC under its default settings just happens to be more permissive in C mode (mostly for historical reasons).

    Use -pedantic-errors in GCC, and you will get an "error" in C code as well. (Note that -pedantic-errors does not simply blindly turn all "warnings" into "errors". It attempts to report only actual constraint violations as "errors".)

提交回复
热议问题