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

后端 未结 3 1815
情歌与酒
情歌与酒 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 07:59

    The C standard says

    A declaration other than a static_assert declaration shall declare at least a declarator (other than the parameters of a function or the members of a structure or union), a tag, or the members of an enumeration.

    C++ says

    In a simple-declaration, the optional init-declarator-list can be omitted only when declaring a class (Clause 9) or enumeration.

    A violation of this in either language requires a diagnostic. The standards do not talk about compiler errors or warnings. A warning is a diagnostic.

提交回复
热议问题