why does this work within global scope:
static int a;
static int a=0;
but not within function's body:
void foo()
{
static int b;
static int b=0; //error: Duplicate declaration of global variable 'b'
...
using clion 2017.3.1, C99, gcc5.4
The first one, in global scope, is an example for a so called Tentative definition.
A tentative definition is an external declaration without an initializer, and either without a storage-class specifier or with the specifier static.
A tentative definition is a declaration that may or may not act as a definition. If an actual external definition is found earlier or later in the same translation unit, then the tentative definition just acts as a declaration.
In the second example, b
has block scope and no linkage, in other words: the declarations are not external. Hence, the tentative definition rule does not apply.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With