scope - extern declaration and definition in C -
a global variable may 1 2 different storage classes in c, best knowledge, , declaration may given 2 different keywords, correspodingly
extern int foo; //default static int bar; static variables visible within module of declaration, , cannot exported. in case of extern declaration, variable in common namespace of modules linked, unless shadowed static variable.
whereas static variables have defined in module, extern variable may defined somewhere else. has defined if ever used.
my compiler (gcc) accepts
static int bar = 5; but casts complain at
extern int foo = 4; it seems expected extern variables never defined keyword 'extern'. leads following question:
what kind of storage class object 'foo' in example above have in module defined?
iirc, extern more of hint compiler not have allocate storage value. linker expected find value in compilation unit. extern used in header files indicate has defined storage associated name. definition of value not include extern keyword since compiler has allocate storage value in compilation unit includes definition.
see extern storage class specifier more details.
Comments
Post a Comment