Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a few doubts regarding the use of extern keyword with variables in C. I did go through the links related to this question. However, there are still a few things that I didn't gather very well

#include<stdio.h>
    main( )
    {
    extern int i;
    printf ( "\n%d ",i) ) ;
    }
    int i = 31 ;

In the above code, how does I get printed before its definition statement?
Now in the following code:

#include<stdio.h>
int x = 21 ;
main( )
{extern int i;
 i=20;
printf ( "\n%d ", i ) ;
}

Isn't the statement "i=20;" a definition statement? I get an error for this. Is it because i'm trying to change the variable that is defined in some other source file? If this is the case how is the statement "int i=31;" in the top most code snippet right to use?

Also, I read, "int i;" is a definition. I don't really follow how.

share|improve this question

4 Answers 4

In your first program, the print statement is printing the value of i based on the extern int i declaration. This is similar to calling a function based on its prototype declaration without seeing its definition. The compiler generates code to retrieve the value in the global variable named i. The symbol is resolved to the correct variable and address at link time.

In your second program, no definition for i is provided, only the extern int i declaration, and the attempt to set its value with i = 20. At link time this fails, since there is no definition, and so the attempt to resolve the reference to the global variable fails. Changing i = 20 to int i = 20 instead creates a local variable named i within the scope of the function main(), and is no longer referencing the globally declared extern int i.

When int i; is used at global scope, it is treated as a declaration and may be treated as a kind of definition. A global variable declared with an initializer, like:

int i = 20;

is always treated as a definition. Only one definition of this type is allowed, even if each is using the same initializer value. However,

int i;

is treated like a declaration. If more than one of these declarations appear, they are all treated as declarations of the same variable. At the same time, if no declaration with an initializer is present, this variable is implicitly defined with an initializer of 0, and the linker will be able to resolve references to it.

share|improve this answer

Declaration of a variable/function simply declares that the variable/function exists somewhere in the program but the memory is not allocated for them.

Coming to the definition, when we define a variable/function, apart from the role of declaration, it also allocates memory for that variable/function.

So in the 1st program, int i = 31 outside the main() defines the variable i. This variable is then merely declared as an extern variable inside the main(). Hence the program works.

In the 2nd program, without defining the variable i elsewhere, it is directly declared as an extern variable. Hence it gives an error.

share|improve this answer

In the second program you haven't defined the variable anywhere , not outside the main and instead you have defined it inside main so you are getting that error.

But in the first program you are defining the variable once and that is outside the main so you don't get that error.Program looks outside the main and find its definition.

int i; is a variable definition declaration both for AUTO variables (i.e inside main or any function ) but not for the external variables for extern variables you have to declare it like :

extern int i;

and then define outside main like this :

int i;

share|improve this answer

The idea behind extern is that you tell the compiler you're going to use a variable that was already defined outside the scope of where it is used.

It doesn't make much sense in your second case as there i is defined as a local variable. If you were to define it as a global, like you do in the first case, it would be fine.

If I can demonstrate the use of extern with a very simple (contrived) case:

main.c:

#include <stdio.h>

extern int a; /* we tell the compiler that a will come from outside the scope of where it is used */

int main()
{
    printf("%d\n", a);

    return 0;
}

source.c:

int a = 3; /* we define a here */

You then compile both .c files like this:

$ gcc main.c source.c

This outputs 3.

You might also want to have a read at this article.

share|improve this answer
    
I believe both should compile, but the 2nd one will fail to link (fires up clang to test it) –  Christoph Jul 10 '13 at 8:46
1  
ok, the first example failed to compile, but not because of the variable declarations - there's an extraneous closing parenthesis... –  Christoph Jul 10 '13 at 8:48
    
@Christoph the second one fails due to saying that i is extern but not actually providing it yet using it at the same time. Comment out all lines invloving i bar the extern one and it will compile fine. –  Nobilis Jul 10 '13 at 8:54
    
@Christoph just double checked and first case is valid, yes (after fixing the parens), updated my answer to only mention the second case. There i is a local, yet he is telling the linker it is a global. –  Nobilis Jul 10 '13 at 8:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.