- A variable is declared when the compiler is informed that a variable exists (and this is its type); it does not allocate the storage for the variable at that point.
- A variable is defined when the compiler allocates the storage for the variable.
Declaration can be done any number of times but definition only once.
As extern is used with a variable, it’s only declared not defined.
extern int i; // Here, memory is not allocated for this variable.
int i = 10; //Here, memory is allocated for this integer variable (it's definition with initialization).
i = 5; // As declaration is already done, here "i" is defined again which is invalid.
It will throw error here.
For more information on "extern" keyword:
1. https://www.geeksforgeeks.org/understanding-extern-keyword-in-c/
2. https://stackoverflow.com/questions/1433204/how-do-i-use-extern-to-share-variables-between-source-files