What is the use of declaring a static variable as extern inside a function?
c
Solution
this is useful when you need to access a variable that resides within another translation unit, without exposing the external variable globally (for a few reasons, like name collision, or that the the variable shouldn't be directly accessed, so `static` was used to limit its scope, but that TU's header still needs access).
As an example, lets say we have a translation unit `foo.c`, it contains:
//foo.c
static int i = 0;
`i` shouldn't be changed or directly accessed outside `foo.c`, however, `foo.h` comes along requiring access to `i` for an inline function, but `i` shouldn't be exposed to any translation unit using `foo.h`, so we can use `extern` at functional level, to expose it only during the scope of `IncI`, the inline function requiring the use of `i`:
//foo.h
inline void IncI(int val)
{
extern int i;
i += val;
}
Your second example is 'disallowed' because the compiler thinks you are trying to bind two different variables to the same symbol name, ie: it creates the `static i` at local scope, but searches for the `extern int i` at global scope, but doesn't find it, because `static i` as at the function scope. a more clever compiler would just fix the linkage to the `static i`, whether or not this follows standards I wouldn't know.
Now that I have a C standards document to work from (shame on me I know...), we can see what the official stance is (in C99):
6.2.2 Linkages of identifiers
Section 3:
If the declaration of a file scope identifier for an object or a function contains the storageclass specifier static, the identifier has internal linkage.
Section 4:
For an identifier declared with the storage-class specifier extern in a scope in which a prior declaration of that identifier is visible, if the prior declaration specifies internal or external linkage, the linkage of the identifier at the later declaration is the same as the linkage specified at the prior declaration. If no prior declaration is visible, or if the prior declaration specifies no linkage, then the identifier has external linkage.
thus, because `static` will cause internal linkage, the `extern` will bring that linkage into the current scope. there is also a footnote stating that this may cause hiding of variables:
23) As specified in 6.2.1, the later declaration might hide the prior declaration.
Problem
``` #include <stdio.h> static i = 5; int main() { extern int i; printf("%d\n",i); return 0; } ``` Can someone give any use-case for declaring a static variable as extern inside a function block? NEW: Why is this not allowed? ``` int main() { static i = 5; extern int i; printf("%d\n",i); return 0; } ```