Followers 0

Extern block scope variable?

19 posts in this topic

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 [i]i[/i]variable(applying internal linkage.)

0

Share on other sites

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.

0

Share on other sites
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++.
1

Share on other sites

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

0

Share on other sites
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
1

Share on other sites

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;

0

Share on other sites

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.

0

Share on other sites

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

0

Share on other sites

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

0

Share on other sites

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

0

Share on other sites

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.

0

Share on other sites
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
0

Share on other sites
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

Share on other sites

1) 'some thing is outside'

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

0

Share on other sites

Going back to the original post, according to some documentation I am reading, at least for [b]C[/b], 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.

0

Share on other sites

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
0

Share on other sites
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".

0

Share on other sites

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.

0

Share on other sites

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

0

Share on other sites

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
0

Create an account

Register a new account