Leandros
Leandros

Reputation: 16825

Why are empty declarations allowed?

Why does C allows empty declarations? They're both explicitly allowed at the grammar level and only generate a warning if compiled.

The production declaration, from the Annex A of the C standard, is allowing it at the grammar level:

declaration
    = declaration_specifiers , ";"
    | declaration_specifiers , init_declarator_list , ";"
    | static_assert_declaration
    ;

(turned into EBNF by me)

Upvotes: 1

Views: 425

Answers (1)

Ben Voigt
Ben Voigt

Reputation: 283694

C does not allow empty declarations. See https://stackoverflow.com/a/33273777/103167

But it does allow declarations without any declarators, only specifiers, as long as those specifiers create a type tag. For example:

/* here begins the specifier */
struct tagS /* <-- there's the tag */
{
   int x;
} /* here ends the specifier */
/* no declarators */
;

Which is a perfectly useful and legal way to define the structure of a user-defined type.

And that's why the grammar has to specify the declarator list as optional.

Upvotes: 5

Related Questions