4

i am confused in the use of extern keyword in C. when it is used with a variable, then it means the declaration of variable. I declare the variable tmp outside the main() function and define it in a separate block in main but when i print the value in subsequent block i got an error "UNRESOLVED EXTERNAL LINK". I am confused please give me detailed explanation.


#include <stdio.h>
extern int tmp ;
int main()
{
    {
        int tmp = 50;
    }
    {
        printf("%d",tmp);
    }
    return 0;
}

alk
  • 69,737
  • 10
  • 105
  • 255
Rohit Sehgal
  • 41
  • 1
  • 2
  • 6
    This should help: [What are extern variables in C?](http://stackoverflow.com/questions/1433204/what-are-extern-variables-in-c/1433387#1433387) – Gilles 'SO- stop being evil' Sep 05 '13 at 07:24
  • When I pointed out http://stackoverflow.com/questions/1433204/what-are-extern-variables-in-c/1433387#1433387, I meant it as useful background reading. I don't think it adequately answers this specific question, which is why I disagree with considering the earlier question a duplicate. – Gilles 'SO- stop being evil' Sep 05 '13 at 07:47

5 Answers5

7

No; extern int tmp; means "somewhere else there is a definition of the variable tmp"; this is a declaration — you can reference tmp but it is not defined. Further, when you write extern int tmp; outside a function, it means that the variable will be defined outside a function — it is a global variable which may be defined elsewhere in the current source file or in another source file. (The rules for extern int tmp; written inside a function are moderately complex; let's not go there now!)

Your local variable int tmp = 50; in the function is unrelated to the global variable tmp declared outside. The local variable hides the global variable inside the braces. (The local variable is also unused.) The printf() statement, though, references the global variable; the local variable is not in scope for the printf().

Because you do not define the global variable (for example, by adding int tmp = -2; at the bottom of the file), your program fails to link and will continue to do so until you either define the variable in this source file or link in another source file where the variable is defined.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • np. A good detailed answer :). I slightly disagree i.e. a global variable with extern cannot be defined else where in the same source file. It will be in the same translation unit, but not the same source file. Let me know if you believe otherwise. – fkl Sep 05 '13 at 07:36
  • 1
    You mean that you think that you can't do: `extern int i; int i = 97;` as consecutive lines in a source file, outside the scope of a function? Try it — you can...in either order. One's a declaration; the other's a definition; they can coexist. You can repeat the declaration, but you can't repeat the definition. – Jonathan Leffler Sep 05 '13 at 07:38
  • Hmmm... that is interesting. I am pretty sure i at least failed doing it once in my career. May be standard compilers for desktop OS such as gcc allows it as you mentioned and my scenario might have been an embedded one such as gcc for arm or ppc or mips since i have worked with them a lot. But i will make sure if this is recommended as a portable standard practice. Thanks for pointing that out – fkl Sep 05 '13 at 07:43
  • See the [What are `extern` variables in C](http://stackoverflow.com/questions/1433204/what-are-extern-variables-in-c/1433387#1433387) question for a very (very, very) long answer. However, it is good practice to have a header that declares global variables, and to include that header in the source file that defines the global variables to ensure that the declaration and definition agree. The other files that reference the global variables can then use the declaration confidently. This cross-checking is crucial — and requires `extern int global_var;` and `int global_var = 12345;` to co-exist. – Jonathan Leffler Sep 05 '13 at 07:46
3

This line :

 extern int tmp ;

says look for the tmp variable definition elsewhere , which means look for the variable definition in other translation unit in the entire program.

when you define int tmp in main it is local to that function, i.e it doesn't have any external linkage.

Disclaimer- There are seriously many posts on SO regarding this like the one with link provided in the comments above . No, matter how much I add to this it will end up being a repetition. however , you have a good answer below by Jonathan leffler too.

0decimal0
  • 3,884
  • 2
  • 24
  • 39
  • 1
    "elsewhere" can be elaborated into outside the file scope on even better a different "translation unit". +1 though being first and correct answer – fkl Sep 05 '13 at 07:31
  • @fayyazkl yeah adding some details just a moment !! thanks !! by the way :) – 0decimal0 Sep 05 '13 at 07:32
1

Extern is redeclaration , so it doesn't crate variable, but only tells compiler that real declaration is somewhere else. You can use it in one source file to refer to variable declaration in another file, or in the same file to express that you use previously declared global variable.

So when you declare global variable

int a=5;

and use in function in the same source file, you can add extern int a; in the body of a function to clearly tell that it uses global variable but declaration is not here.

type func(arguments){
extern int a;
.
.
.

And when int a=5 is in another source file you place

 extern int a; 

in source file you actually want to use global variable a declared in previous source file.

zubergu
  • 3,646
  • 3
  • 25
  • 38
0

This is about linkage. When you declare a variable extern you give it external linking, saying it's defined with global linkage somewhere else.

In your function you're defining a variable called tmp, but it doesn't have global linkage, it's a local variable. You'd have to define it outside of any function to give it global linkage.

There's also static linkage, which means a variable is global but only to the current compilation unit (source file).

Per Johansson
  • 6,697
  • 27
  • 34
0

Using the extern keyword you only declare the symbol tmp. Which means the symbols is defined somewhere else and will be resolved at link time.
So if you do not provide a compiled object defining the symbol, the linker gives you some kind of "unresolved symbol" error.

See the following question for more details on Declaration or Definition in C

Community
  • 1
  • 1
greydet
  • 5,509
  • 3
  • 31
  • 51