-1

So i was looking through various questions to prepare for my interview that's up tomorrow and came across extern keyword, i understand that extern keyword specifies to allocate memory for a variable which is part of another program(dont know where to use) but the real doubt is,

#include<iostream>
using std::cout;


int main()
{
    extern int a;
    cout<<a;
    
    return 0;
}

int a=20;


output:
 20

i wonder how this works? even if hoisting works in c++ , i know a bit of JS hoisting like even if the declaration of a as int a is done first and assignment is done later , the output is ought to be a value a garbage value....

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
anyman
  • 9
  • `extern int a;` is *"just"* a declaration. – Jarod42 Oct 04 '21 at 08:53
  • 1
    C++ does not have hoisting. What you're seeing is that global variables such as `a` are initialized before `main` is called. – Quentin Oct 04 '21 at 08:53
  • so when do we need to use extern in our program ? – anyman Oct 04 '21 at 08:54
  • 3
    In this context, the `extern` keyword tells the compiler that the variable exists with [*external linkage*](https://en.cppreference.com/w/cpp/language/storage_duration#external_linkage) somewhere in a [*translation unit*](https://en.wikipedia.org/wiki/Translation_unit_(programming)). – Some programmer dude Oct 04 '21 at 08:55

1 Answers1

0

For this block scope declaration

extern int a;

the compiler searches a previous declaration of the name a in the global namespace that to determine whether it has external or internal linkage. If the compiler does not find the previous declaration it thinks that the variable a refers to a variable defined in the global namespace with external linkage.

And indeed there is a definition of the variable in the global namespace

int a=20;

So the two declarations refer to the same object defined in the global namespace.

From the C++ 17 Standard (6.5 Program and linkage)

2 A name is said to have linkage when it might denote the same object, reference, function, type, template, namespace or value as a name introduced by a declaration in another scope:

and

6 The name of a function declared in block scope and the name of a variable declared by a block scope extern declaration have linkage. If there is a visible declaration of an entity with linkage having the same name and type, ignoring entities declared outside the innermost enclosing namespace scope, the block scope declaration declares that same entity and receives the linkage of the previous declaration. If there is more than one such matching entity, the program is ill-formed. Otherwise, if no matching entity is found, the block scope entity receives external linkage. If, within a translation unit, the same entity is declared with both internal and external linkage, the program is ill-formed.

That is a declaration in a block scope with the storage class specifier extern does not define a new object. It refers to an object with the same name defined in the enclosing namespace with an internal or external linkage.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • so in this particular program extern int a thinks that int a is a part of it ? what if in a program i have like , int a and extern int a being declared inside that same block ? for extern int a it must declare in global namespace and int a will also exist ? with different levels of scope ? – anyman Oct 04 '21 at 09:03
  • @anyman it means that the both declarations refer to the same object with external linkage. – Vlad from Moscow Oct 04 '21 at 09:05
  • boss ,can you give me a good blog to read about this lets say if i have a block within that block, first i create a variable named int a; and then next to that declare extern int a but why does this flag an error for redeclaration ? because extern int a will search for a in global namespace, wait, does that global namespace mean the current program or anyother namespace thats being included using "using namespace xyz" ? if a variable called a exists in xyz then both int a and then extern int a will exist :? – anyman Oct 04 '21 at 09:07
  • @anyman It is because the same variable is declared as having no linkage and as having external linkage. – Vlad from Moscow Oct 04 '21 at 09:08
  • so how do i create a linkage, can you give me an example to understand it better – anyman Oct 04 '21 at 09:09
  • @anyman Your program in the question is such an example. – Vlad from Moscow Oct 04 '21 at 09:10