4

This seems very trivial but a somewhat rigorous explanation for the following behavior will help my understanding of extern a lot.So I'll appreciate your answers.

In the following sample program,I've declared an extern variable x inside a function (main()).Now if I define the variable at file scope right after main() and assign 8 to it, then the program works fine and 8 is printed.But if I define the variable x inside main() after the printf(),expecting the extern declaration to link to it, then it fails and gives the following error:

test.c||In function 'main':|
test.c|7|error: declaration of 'x' with no linkage follows extern declaration|
test.c|5|note: previous declaration of 'x' was here|
||=== Build finished: 1 errors, 0 warnings ===|


#include<stdio.h>

int main()
{
extern int x;
printf("%d",x);
int x=8;   //This causes error
} 
//int x=8;   //This definition works fine when activated

I see only one fault in the code,that the statement int x=8 means we are declaring x again as a variable with auto storage class.Rest I don't understand.Can you tell me the following:

1) Why are we allowed to declare an extern variable inside a function,without any warning or error?If valid,what exactly does it mean?

2) Since we declared x as extern inside the function and it showed no error,why then this declaration doesn't link to the definition of the variable inside the function,but looks outside,when the variable is defined outside? Is conflicting storage-class declaration auto-vs-extern the reason for this?

7
  • Put the definition before the first statement, i.e. before printf. Commented May 9, 2013 at 10:19
  • @PaulR Exactly the same error even if i do that. Commented May 9, 2013 at 10:21
  • @PaulR I compiled that way too and it fails just as the earlier time. Commented May 9, 2013 at 10:22
  • @PaulR I am really curious why you said that...... Commented May 9, 2013 at 10:34
  • Prior to C99 variable definitions had to precede the first statement in a function - my guess was that you were compiling this as C89, but evidently there is a further problem. Commented May 9, 2013 at 10:53

4 Answers 4

6

extern variable declaration is a promise to the compiler that there would be a definition of a global variable some place else. Local variables do not qualify as fulfillments of the promise to the compiler, because they are invisible to linkers. In a sense, extern declarations are similar to forward declarations of functions: you say to the compiler "I know this function is there, so let me use it now, and let linker take care of locating the actual implementation".

Sign up to request clarification or add additional context in comments.

2 Comments

promise to the compiler that there would be a definition of a global variable some place else. Local variables do not qualify as fulfillments of the promise--That nails it.What way does the standard describe this point?I would appreciate if you would paste that.
@Rüppell'sVulture The standard defines it in terms of linkage: external, internal, and no linkage. Locals have no linkage; extern and globals have external linkage, and statics have internal linkage. That's section 6.2.2 of C99 standard.
1

just remember the concept that when we declare a variable as extern inside a function we can only define it outside of that function.

Comments

0
Why are we allowed to declare an extern variable inside a function,without any warning or error?If valid,what exactly does it mean?

Ans:- we can use extern at functional level, to expose it only during the scope of that function.

Since we declared x as extern inside the function and it showed no error,why then this declaration doesn't link to the definition of the variable inside the function,but looks outside,when the variable is defined outside? Is conflicting storage-class declaration auto-vs-extern the reason for this?

Ans:Variables declared at block scope (i.e. local variables) have no linkage, unless they are explicitly decalred as extern.

Comments

-1

Nobody uses extern in this way. extern is usually used on big projects, lots of .c , .h files and some variables needs to be shared. On those circumstances, compilation often fails to resolve variable declaration (perhaps, it's on some .h file which yet to compile), then "extern" is used to tell compilar to leave it for now and proceed compilation, this thing will be dealt at linking phase.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.