Use of 'extern' keyword while defining the variable?

It does not change the meaning. Extern only makes sense when you declare a variable. Defining a variable with extern is the same because all global variables that are not marked static are symbols visible to the linker by default.

Up vote 0 down vote favorite share g+ share fb share tw.

After seeing this answer I have this doubt. In my project, I have seen some extern variables declared and defined like below: file1. H extern int a; file1.

C extern int a=10; But in the link I mentioned it says that in the c file it should be defined like: int a = 10; Does adding extern key word during the definition too has any purpose/meaning. Or does it matter by the way? C variables extern link|improve this question edited Dec 10 '11 at 22:15raikrahul849 asked Sep 30 '11 at 6:53peter5,56021970 73% accept rate.

1 The question that you reference is a C question. Did you mean to tag this as C++ as well? – Charles Bailey Sep 30 '11 at 7:55.

It does not change the meaning. Extern only makes sense when you declare a variable. Defining a variable with extern is the same because all global variables that are not marked static are symbols visible to the linker by default.

Note that if you didn't want to initialise the variable, that is, not having the part = 10, the compiler will assume that extern int a is always a declaration and not a definition. In the same sense, having int a globally is always a definition and not just a declaration.

Int a (at file scope) is a tentative definition in C, a definition in C++ and always a declaration. I think you might have meant: "...always a definition and not just a declaration". – Charles Bailey Sep 30 '11 at 7:41 1 The answer is actually incorrect.

All non-const variables at namespace scope that are not marked static are visible to the linker (have external linkage). Const variables have internal linkage (not visible to the linker) unless marked extern somewhere. – James Kanze Sep 30 '11 at 7:48 @JamesKanze: Is this true in C?

I thought it was only C++. – Charles Bailey Sep 30 '11 at 7:54 @CharlesBailey Only C++. I missed the fact that this was tagged for both languages---this is one point where C and C++ differ.

– James Kanze Sep 30 '11 at 8:33.

It depends. In this case, it makes no difference, but it can. There are two issues here: definition vs. just declaration, and linkage.

And C++ doesn't handle them in an orthogonal manner. In C++, the declaration of a variable or a reference is a definition if and only if neither the extern keyword nor an initialization are present. (Note that the rules are different for functions, types and templates.

) So: extern int a; // not a definition int a; // definition extern int a = 42; // definition int a = 42; // definition The rules say you must have exactly one definition, so you put a definition in a source file, and the declaration in a header. With regards to linkage, a symbol declared as a variable or a reference has external linkage if it is declared at namespace scope, is not declared static, and is either not const (nor constexpr in C++11) or has been declared extern. The fact that const can give a variable internal linkage occasionally means that the extern is necessary: int const a = 42; // internal linkage extern int const a = 42; // external linkage Note that the extern doesn't have to be on the same declaration: extern int const a; // declaration, in header... int const a = 42; // external linkage, because of // previous extern Still, I've occasionally needed the extern; typically because I want to use a local constant to instantiate a template.

(This is only an issue if the template parameter takes a pointer or a reference. You can instantiate a template with an int parameter with an int const a = 42;, because the template is instantiated with the value 42, and not the variable a. ).

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions