• Create Account

## Extern block scope variable?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

19 replies to this topic

### #1gasto  Members

Posted 20 February 2014 - 04:48 PM

int i = 4;
int main(int argc, char* argv[])
{
extern int i;
cout << i;
return 0;
}


In Visual Studio 2008 outputs

4

without warnings.

What is going on? Weren't block scope variables supposed to have no linkage?

I suspect the compiler is ignoring the fact that it is in a block altogether and simply redeclaring the global ivariable(applying internal linkage.)

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

Posted 20 February 2014 - 05:02 PM

extern int i;

isn't a stack variable it's just telling the compiler you know that an int named i has a definition elsewhere. It doesn't even need to be defined in the same translation unit, the linker will find it if it's unique (and has external linkage).

You may not even get an error if i isn't really an int though.

EDIT: It's not good practice to extern variables at block scope, since if you do it more than once you have to change all occurrences if you change the type. You should only extern things in a single header file.

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.

Edited by Paradigm Shifter, 20 February 2014 - 05:13 PM.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

### #3frob  Moderators

Posted 20 February 2014 - 05:18 PM

Also, and rather spectatularly as you tagged it as "c language"

That is **NOT** C.

Check your compiler options, double-check that you are compiling as C rather than C++.

Check out my book, Game Development with Unity, aimed at beginners who want to build fun games fast.

Also check out my personal website at bryanwagstaff.com, where I occasionally write about assorted stuff.

Posted 20 February 2014 - 05:24 PM

It could be C if cout is in scope and of integral type, cout << i; but it wouldn't have any useful side effects though, so would be optimised away ;)

Edited by Paradigm Shifter, 20 February 2014 - 05:24 PM.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

### #5/ fir   Members

Posted 20 February 2014 - 05:45 PM

int i = 4;
int main(int argc, char* argv[])
{
extern int i;
cout << i;
return 0;
}


What is going on? Weren't block scope variables supposed to have no linkage?

"int i =4" is not a block scope but whole file scope(it is even more than file scope because it is by default external)  * and "extern int i;" is a symbol declaration (not needed at all here)

* a normal 'global' aso someone could name it, ane "extern int i" is

this time not needed declaration ("part of a header" just info to the linker, nothing binary) - it is usable if you remove i to another .obj module then it is used by linker to allow you tu use that from this place

Edited by fir, 21 February 2014 - 04:34 AM.

Posted 20 February 2014 - 05:49 PM

That is correct but it will still work if i is not in file scope but has external linkage. Not recommended though, because of the don't repeat yourself idiom.

This would work too (also not good practice):

int main(int argc, char* argv[])
{
extern int i;
cout << i;
return 0;
}

int i = 4;


Edited by Paradigm Shifter, 20 February 2014 - 05:50 PM.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

### #7King Mir  Members

Posted 20 February 2014 - 07:18 PM

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.

### #8gasto  Members

Posted 21 February 2014 - 07:20 AM

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.

Actually that throws a linker error:

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


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

### #9gasto  Members

Posted 21 February 2014 - 07:31 AM

Basically what I've come to learn about this compiler is that it allows multiple declarations but not multiple definitions.

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

### #10/ fir   Members

Posted 21 February 2014 - 08:40 AM

Basically what I've come to learn about this compiler is that it allows multiple declarations but not multiple definitions.

ofcourse, you could populete a declaration 1000 times with no trouble

but populating a definition will raise a symbole conflict

### #11King Mir  Members

Posted 21 February 2014 - 09:07 AM

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.

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.

### #12gasto  Members

Posted 21 February 2014 - 10:48 AM

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.

Edited by gasto, 21 February 2014 - 10:52 AM.

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

### #13/ fir   Members

Posted 21 February 2014 - 11:02 AM

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 ;/

### #14gasto  Members

Posted 21 February 2014 - 11:39 AM

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.

### #15gasto  Members

Posted 21 February 2014 - 11:44 AM

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.

### #16/ fir   Members

Posted 21 February 2014 - 11:50 AM

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();
}



Edited by fir, 21 February 2014 - 11:55 AM.

### #17King Mir  Members

Posted 21 February 2014 - 12:17 PM

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".

### #18King Mir  Members

Posted 21 February 2014 - 03:15 PM

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();
}



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.

### #19gasto  Members

Posted 22 February 2014 - 11:14 AM

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.

### #20King Mir  Members

Posted 22 February 2014 - 01:48 PM

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.

Edited by King Mir, 22 February 2014 - 01:59 PM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.