There are four storage classes in C programming language, which are as follows −
- auto
- extern
- static
- register
Global variables / External variables
The keyword is extern. These variables are declared outside the block.
Scope − Scope of a global variable is available throughout the program.
Default value is zero.
Algorithm
The algorithm is given below −
START Step 1: Declare and initialized extern variable Step 2: Declare and initialized int variable a=3 Step 3: Print a Step 4: Call function step 5 Step 5: Called function Print a (takes the value of extern variable)
Example
Following is the C program for extern storage class −
extern int a =5; /* this ‘a’ is available entire program */
main ( ){
int a = 3; /* this ‘a' is valid only in main */
printf ("%d",a);
fun ( );
}
fun ( ){
printf ("%d", a);
}Output
The output is stated below −
3 1
Consider another program for extern storage class −
Example
External.h
extern int a=14;
extern int b=8;
externstorage.c file
#include<stdio.h>
#include "External.h"
int main(){
int sub = a-b;
printf("%d -%d = %d ", a, b, sub);
return 0;
}Output
The output is stated below −
a-b=6