Static Variable Declaration (C)

c, declaration, language-lawyer, static-variables

Solution

Yes the declarations in case `1` and `2` are identical. We can see this by going to the draft C99 standard section `6.7.5` Declarators which says (emphasis mine going forward):

Each declarator declares one identifier, and asserts that when an operand of the same form as the declarator appears in an expression, it designates a function or object with the scope, storage duration, and type indicated by the declaration specifiers.

We can see the grammar from section `6.7` Declarations is as follows:

declaration:
   declaration-specifiers init-declarator-listopt ;

the declaration-specifiers include storage duration:

declaration-specifiers:
   storage-class-specifier declaration-specifiersopt

so the storage duration specifier applies to all the declarators in the init-declarator-list which has the following grammar:

init-declarator-list:
   init-declarator
   init-declarator-list , init-declarator
init-declarator:
   declarator
   declarator = initializer

You may wonder about pointers, they are handled handled differently and we can see this from the grammar in `6.7.5` for declarators:

declarator:
    pointeropt direct-declarator
[...]
pointer:
    * type-qualifier-listopt
    * type-qualifier-listopt pointer

Problem

Are the following two `static` variable declarations equivalent? 1. ``` static int var1; static int var2; static int var3; ``` 2. ``` static int var1, var2, var3; ``` More specifically, in case 2, will all variables be `static`, or just `var1`?

Original source