2

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?

6
  • Both are same, no difference between int const and const int and static tells about duration and scope of var Commented Feb 27, 2020 at 17:46
  • Does this answer your question? const int vs. int const as function parameter in C++ and C Commented Feb 27, 2020 at 17:49
  • 1
    static mentions the scope and lifetime of a variable. Duration is till the end of program and scope is within a transaction unit or a block where is it is defined. Similar to volatile const int where volatile is input to compiler to not to optimize but variable is not allowed to updated in current scope directly but indirect means of update through pointer is UB. Commented Feb 27, 2020 at 17:58
  • 1
    From a style perspective, the type always sits right next to the variable. If static is used, it needs to be first. So: static const int var; is the preferred form. But C doesn't care. Commented Feb 27, 2020 at 18:02
  • 1
    Note that C11 §6.11.5 Storage class specifiers says: _The placement of a storage-class specifier other than at the beginning of the declaration specifiers in a declaration is an obsolescent feature._The examples here all put the storage class specifier first, so it is mostly tangential. Commented Apr 6, 2020 at 13:59

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

So I could even do const int static var or int static const var and any of those constructs would be qualified equivalently by the compiler?
But with pointers, the location of const keyword does matter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.