csyntaxstaticconstantsqualifiers

What is the difference between static const int and static int const?


In this answer the OP used:

static int const var = 5;

in the context of a Conditional Compilation Control.


Is there a difference between using static const int and static int const?

Like for example:

static const int var;

vs.

static int const var;

I don´t know the technique to imply the type in the middle between static and const. Is there a difference?


Solution

  • The grammar for declaration specifiers is given in C 2018 6.7 1, and it shows that specifiers for storage class (such as static), type (such as short or double), qualifiers (such as const), functions (inline and _Noreturn), and alignment may appear in any order. Nothing in clause 6.7 gives any meaning to the order in which the specifiers appear, so we may presume any combination of specifiers has the same meaning regardless of order.

    The only mention of “order” in this regard appears in 6.7.2 2, which says “… the type specifiers may occur in any order, possibly intermixed with the other declaration specifiers.” So you can write long static int const long for static const long long int, just as you can say “square red big house” instead of “big square red house”—there is no rule against it, but it will seem funny to people and may throw them off.

    Note that the * that indicates a pointer, as well as ( and ) for either grouping or argument lists and [ and ] for subscripts are not declaration specifiers and may not be freely reordered with declaration specifiers. (They are in fact part of a declarator, which is a separate part of a declaration from the declaration-specifiers.)

    However, the standard describes using storage-class specifiers after other specifiers or qualifiers as obsolescent, in 6.11.5:

    The placement of a storage-class specifier other than at the beginning of the declaration specifiers in a declaration is an obsolescent feature.

    “Obsolescent” means the feature may be considered for withdrawal in future revisions of the standard (per Introduction paragraph 2). Thus, compilers that issue a warning for using const static are suggesting a change that helps prepare the source code for a future version of C.