External storage classes.
The keyword “extern” is used to declare the variables of external storage class.
The variable declared without keyword but declared above the function (outside) also belongs to the external storage class.
Properties
Storage | Memory |
Default initial value | Zero |
Scope | Global. |
Life | As long as the program’s execution doesn’t come to an end. |
Difference Between Global and Local Variables
Global variable | Local variable |
Global variable is a variable that is declared outside the definition of functions. | A variable declared inside the definition of a function is known as a local variable. |
Value of global variable is present in all the functions of the program | The value of a local variable is present only within the function in which it is declared. |
Value of a global variable can be manipulated in any of the functions and the modified value will be present in all the functions. | Value of the local variable cannot be manipulated within another function as it is not present there. |
The default value of a global variable is zero (0). | The default value of a local variable is garbage(unpredictable). |
Example:1
#include<stdio.h> int a=10; void abc() { printf("%d\n",a); a=a+10; } int main() { a=a+5; printf("%d\n",a); a=a+20; abc(); printf("%d\n",a); abc(); a=a+20; abc(); printf("%d\n",a); return(0); }
/* Output */ 15 35 45 45 75 85
Example:2
#include<stdio.h> int a=15; void abc() { printf("%d\n",a); a=a+5; } int main() { a=a+7; printf("%d\n",a); a=a+25; abc(); printf("%d\n",a); abc(); a=a+2; abc(); printf("%d\n",a); abc(); return(0); }
/* Output */ 22 47 52 52 59 64 64
Example:3
#include<stdio.h> int a=3; void abc() { printf("%d\n",a); a=a+4; } int main() { a=a+3; printf("%d\n",a); a=a+5; abc(); printf("%d\n",a); abc(); a=a+12; abc(); printf("%d\n",a); abc(); return(0); }
/* Output */ 6 11 15 15 31 35 35
Example:4
Note:
when local and global variables have the same name.
#include<stdio.h> int a=10; //global variable void abc() { printf("%d\n",a); a=a+10; } int main() { int a=5; //local variable a=a+5; printf("%d\n",a); a=a+20; abc(); printf("%d\n",a); abc(); a=a+20; abc(); printf("%d\n",a); return(0); }
/* Output */ 10 10 30 20 30 50
Example:5
#include<stdio.h> int a=15; void abc() { printf("%d\n",a); a=a+5; } int main() { int a=10; a=a+3; printf("%d\n",a); a=a+15; abc(); printf("%d\n",a); abc(); a=a+10; abc(); printf("%d\n",a); return(0); }
/* Output */ 13 15 28 20 25 38