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

后端 未结 3 1806
情歌与酒
情歌与酒 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:52

    The syntax of declaration is defined as (omitting init-declarator-list and init-declarator):

    C11 6.7 Declarations

    declaration:
        declaration-specifiers init-declarator-list opt ;
        static_assert-declaration
    declaration-specifiers:
        storage-class-specifier declaration-specifiers opt
        type-specifier declaration-specifiers opt
        type-qualifier declaration-specifiers opt
        function-specifier declaration-specifiers opt
        alignment-specifier declaration-specifiers opt
    

    Note that declaration-specifiers is defined recursively, but each with an opt indicates it's optional.

    Also, the following clause 6 states:

    The declaration specifiers consist of a sequence of specifiers that indicate the linkage, storage duration, and part of the type of the entities that the declarators denote. The initdeclarator-list is a comma-separated sequence of declarators, each of which may have additional type information, or an initializer, or both. The declarators contain the identifiers (if any) being declared.

    Note the words if any.

提交回复
热议问题