I was reading about extern storage class from this website:
https://www.geeksforgeeks.org/understanding-extern-keyword-in-c/
and there is this example:
#include "somefile.h"
extern int var;
int main(void)
{
var = 10;
return 0;
}
Supposing that somefile.h has the definition of var
Since we cannot define the variable which is an extern storage class how is it working correctly?
Does it mean that if an extern variable is already defined somewhere in the script and then if I re-define it any further in my code it's going to work?
C Language has methodology :- first Declare it and define it as you know daily use in C. You declare a function and define it somewhere downside.
The Main things is declaring a variable doesn't allocate memory. Memory is only allocated once you define it and in this case var is declare as well defined in somefile.h Memory is allocated via that file only and in this example extern states that this variable is defined somewhere else and Memory is already initialized for it. [blog]: https://errbits.com/2021/12/16/storage-classes-in-c/ "Storage Classes in C"
And while compile time compiler verify that for same ,if var is already defined in somefile.h it will compile without error since it will check its symbol table to verify if it there.