extern keyword

Not sure if this exists in C++. Saw it in the Clean Architecture book.

Resources

the extern keyword extends the visibility of the C variables and C functions. That’s probably the reason why it was named extern.

https://stackoverflow.com/questions/3684450/what-is-the-difference-between-static-and-extern-in-c

static vs. extern?

static means a variable will be globally known only in this file. extern means a global variable defined in another file will also be known in this file, and is also used for accessing functions defined in other files.

So basically

  • static has the file scope
  • extern has the entire program scope
extern int var; // declaration
int var; //  declaration and definition 
  
int main()  
{   
    ...
}

Therefore, this only declares it, not define it.

extern int var; 
int main(void) 
{ 
  var = 10; 
  return 0; 
} 
  • Will not compile because var’s value is overwritten even though it’s never declared

This works

extern int var = 0; 
int main(void) 
{ 
 var = 10; 
 return 0; 
}