Extern block scope variable?

Started by
18 comments, last by King Mir 10 years, 1 month ago

EDIT2: extern NEVER allocates any storage, it's just a way to tell the compiler and linker you know that something exists and has an address somewhere.

This is incorrect. Extern does allocate storage when used together with an initializer in global scope. It defines a global variable, just like any (non-static) variable defined in global scope.

EDIT: added a detail

Actually that throws a linker error:


Beginning.obj : error LNK2005: "int i" (?i@@3HA) already defined in otherFile.obj

Probably because you define "int i" in another file. "int i" and "extern int i=0" are the same in global scope, meaning you can only have one such definition in your whole program.

Advertisement

extern int i = 14;

Works, if defined in the file where it is used.

It is confusing though, an extern token should only indicate declaration not definition.

I guess the compiler assumes that because there is an assignment, the extern keyword should be obviated, instead of throwing a compiler error.

Intel Core 2 Quad CPU Q6600, 2.4 GHz. 3GB RAM. ATI Radeon HD 3400.

extern int i = 14;

Works, if defined in the file where it is used.

It is confusing though, an extern token should only indicate declaration not definition.

I guess the compiler assumes that because there is an assignment, the extern keyword should be obviated, instead of throwing a compiler error.

thats right this is confusing , extern means external in two ways

1) 'some thing is outside'

2) 'this thing is to be seen from outside' (this is default so you dont have to write it before each function)

same confusing is static which means 'this thing is internal only' or

this is not stack variable but 'static' '

this is probably done to not populate keywords in c but this was a bad choice and granted a heavy mass mind confusion ;/


1) 'some thing is outside'

Something is assumed outside (if by that you mean program duration)also when avoiding the extern keyword all together for global variable and function definitions. Yes, I tried the extern keyword for global function definitions and it did not affect the compilation in any visible way.

Intel Core 2 Quad CPU Q6600, 2.4 GHz. 3GB RAM. ATI Radeon HD 3400.

Going back to the original post, according to some documentation I am reading, at least for C, the extern keyword is used inside a block of code to document that any future referents (variable names) refer to the global variable as opposed to some block scope variable.

Intel Core 2 Quad CPU Q6600, 2.4 GHz. 3GB RAM. ATI Radeon HD 3400.


1) 'some thing is outside'

Something is assumed outside (if by that you mean program duration)also when avoiding the extern keyword all together for global variable and function definitions. Yes, I tried the extern keyword for global function definitions and it did not affect the compilation in any visible way.

not program duration but is in the different seperately compiled module

extern has two meanings instead of one you could use word public

and instead of other declare_external_symbol like


 
void f()
{
    void g();
 
    g();
}

is short for


 
extern void f()
{
    extern void g();
 
    g();
}

which means


 
public void f()
{
  declare_external_symbol void g(); 
  
  g();
}

thats all about it smile.png


extern int i = 14;

Works, if defined in the file where it is used.

It is confusing though, an extern token should only indicate declaration not definition.

I guess the compiler assumes that because there is an assignment, the extern keyword should be obviated, instead of throwing a compiler error.

It's not the compiler assuming anything, it's a rule in the standard. extern is not ignored. extern explicitly specifies the scope of the variable (which can be static, extern, auto, and register). That's why in global scope, you cant use extern and static within the same definition. Most of the time it's not used, just like most of the time we don't write "signed int".


1) 'some thing is outside'

Something is assumed outside (if by that you mean program duration)also when avoiding the extern keyword all together for global variable and function definitions. Yes, I tried the extern keyword for global function definitions and it did not affect the compilation in any visible way.

not program duration but is in the different seperately compiled module

extern has two meanings instead of one you could use word public

and instead of other declare_external_symbol like


 
void f()
{
    void g();
 
    g();
}

is short for


 
extern void f()
{
    extern void g();
 
    g();
}

which means


 
public void f()
{
  declare_external_symbol void g(); 
  
  g();
}

thats all about it smile.png

No, for both uses, extern means "external linkage". It's just that in one case it also means "this is a declaration", and in the other it doesn't.


extern int i = 14;

Works, if defined in the file where it is used.

It is confusing though, an extern token should only indicate declaration not definition.

I guess the compiler assumes that because there is an assignment, the extern keyword should be obviated, instead of throwing a compiler error.

It's not the compiler assuming anything, it's a rule in the standard. extern is not ignored. extern explicitly specifies the scope of the variable (which can be static, extern, auto, and register). That's why in global scope, you cant use extern and static within the same definition. Most of the time it's not used, just like most of the time we don't write "signed int".

Actually, that is not true at all. Fir understood it right. extern simply imports a variable or function defined in another translation unit(code file), in other words it is a linkage process, the scope will stay file based, and the duration program-wise(some books call it static, but I find it to be a horrible name.)

Intel Core 2 Quad CPU Q6600, 2.4 GHz. 3GB RAM. ATI Radeon HD 3400.


extern int i = 14;
Works, if defined in the file where it is used.
It is confusing though, an extern token should only indicate declaration not definition.
I guess the compiler assumes that because there is an assignment, the extern keyword should be obviated, instead of throwing a compiler error.

It's not the compiler assuming anything, it's a rule in the standard. extern is not ignored. extern explicitly specifies the scope of the variable (which can be static, extern, auto, and register). That's why in global scope, you cant use extern and static within the same definition. Most of the time it's not used, just like most of the time we don't write "signed int".

Actually, that is not true at all. Fir understood it right. extern simply imports a variable or function defined in another translation unit(code file), in other words it is a linkage process, the scope will stay file based, and the duration program-wise(some books call it static, but I find it to be a horrible name.)

The proper term is "storage class". Extern specifies a storage class. You're right that declaring a variable in a function with extern does not make that variable local. But it does mean that the variable must be defined elsewhere in the program with extern storage class.

Really there are four uses of extern in C:


extern int i; // declares a variable with external linkage.
 
extern int i=0; // defines a variable with external linkage, just like if extern wasn't there. A program can only have one definition of such a variable.
 
extern int foo(); //declares a function with external linkage. As above, extern can be and usually is omitted.

extern void foo(){} //defines a function with external linkage. As above function, except this is a definition. 

In the last three cases, extern can be replaced with static, to give the symbol internal linkage; extern and static are mutually exclusive. If you replace extern with static in the first case, it becomes a definition. Likewise extern cannot be used together with auto and register, the two storage classes of local variables. Consider:


void foo(){
  int i;//defines an "automatic" or stack variable.
  auto int j; //same as above. Not valid C++11. 
  register int k;//defines a register variable. Behaves the same as above, but the programmer intends the variable to be in a register. Same as above on widespread desktop compilers. 
  static int x; //defines a global variable, not accessible outside the function. 
  extern int y; //declares a global with external linkage.  
}

static int y;//Error. clashes with extern int y above.

In no case can the same variable or function have more than one storage class.

There is one other rule however:


static int i;
extern int i;//declares i with internal linkage, referring to the above definition.

This topic is closed to new replies.

Advertisement