Extern variables: belong to the External storage class and are stored in the main memory. extern is used when we have to refer a function or variable that is implemented in other file in the same project. The scope of the extern variables is Global.
Example: #include <stdio.h> extern int x; int main() { printf("value of x %d", x); return 0; } int x = 3;
Here, the program written in file above has the main function and reference to variable x. The file below has the declaration of variable x. The compiler should know the datatype of x and this is done by extern definition.
Global variables: are variables which are declared above the main( ) function. These variables are accessible throughout the program. They can be accessed by all the functions in the program. Their default value is zero.
Example: #include <stdio.h> int x = 0;/*Variable x is a global variable. It can be accessed throughout the program */void increment(void) { x = x + 1; printf("\n value of x: %d", x);} int main(){printf("\n value of x: %d", x); increment(); return 0; }