Search code examples
csyntaxlanguage-lawyerfunction-declarationc17

Is it standard C17 to wrap a parameter in a function declaration in parenthesis


Is the following a standard C function declaration according to ISO/IEC 9899:2017 (c17)?

int foo(int (bar), int (baz));

If so, please point me to the section in the standard that defines this.

In N2310 Appendix Phrase structure grammar, A.2.2 Declarations, Section 6.7.6, I see the following:

parameter-list:
    parameter-declaration
    parameter-list , parameter-declaration

I'm not familiar with this type of grammar expression, so I'm not sure how to interpret it.

The following program compiles without errors with gcc --std=c17 -Wall and clang --std=c17 -Wall

static int foo(int (bar), int (baz));
static int foo(int bar, int baz)
{
        return bar + baz;
}
int main() {
        return foo(1, 2);
}

However if I run cppcheck (a static analysis tool) on this program, it appears to parse incorrectly.

I'm most interested if this grammar is standard C, or a compiler-specific behavior so I can try to fix the parser or submit a bug report if I can't.


Solution

  • The declaration is allowed by the standard.

    Sectin 6.7.6p1 of the C standard gives the full syntax for a declaration, including the portion you quoted. The relevant parts are as follows:

    A parameter-declaration is defined as:

    parameter-declaration:

    • declaration-specifiers declarator
    • declaration-specifiers abstract-declaratoropt

    A declarator is defined as:

    declarator:

    • pointeropt direct-declarator

    And a direct-declarator is defined (in part) as:

    direct-declarator:

    • ( declarator )

    So we can see from the above that a parameter name can be enclosed in parenthesis.