Invalid c declaration that passes k&r grammar

On k&r 2nd edition p123, it shows the grammar of declarator enter image description here

I've found similar syntax on c standard n1256 enter image description here

according to this int foo()()() is also a valid declarator (which is obviously not), what am I missing here?

ps: there's a cdecl site that deciphers c declarations to plain words, and it translates above decl to "declare foo as function returning function returning function returning int"


  • int foo()(); is a syntactically valid declaration according to the grammar rules you quoted. However, it is semantically invalid; it fails the constraint in [C11/N1570; probably in the older edition you've got]. Function declarators (including prototypes)


    1. A function declarator shall not specify a return type that is a function type or an array type.

    This is a semantic rather than a syntactic rule because it was easier to write the standard that way. Forbidding T name ( ... ) ( ... ) syntactically might not even be possible in a LR grammar. (C's syntax was designed to fit within LR(1) as much as possible; as I recall there are only a couple of places where it isn't.) If it is possible, it would still make the definition of a direct-declarator much more complicated. And T name ( ... ) ( ... ) isn't the only way to [attempt to] declare a function whose return type is a function type. A semantic constraint covers all the possibilities in one sentence.

    The difference between semantic and syntactic rules only matters if you are writing a C parser yourself. If you're just trying to understand what is and is not a valid C program, int foo()(); is invalid either way.

    If you're trying to understand what is and is not a valid C program by reading the text of the C standard, you should know that the C standard never says that a compiler must issue a hard error for something. Failing that, implementers usually take the first sentence of to define the line between warnings and errors:

    A conforming implementation shall produce at least one diagnostic message (identified in an implementation-defined manner) if a preprocessing translation unit or translation unit contains a violation of any syntax rule or constraint

    (boldface: my emphasis). So, you need to read the grammar rules and also the text of all the "constraints" sections.