问题
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?
回答1:
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.)
来源:https://stackoverflow.com/questions/60438923/what-is-the-difference-between-static-const-int-and-static-int-const