C extern storage class



External ( extern ) storage class in C Programming

  1. Variables of this storage class are “Global variables”
  2. Global Variables are declared outside the function and are accessible to all functions in the program
  3. Generally , External variables are declared again in the function using keyword extern
  4. In order to Explicit declaration of variable use ‘extern’ keyword
extern int num1 ;   // Explicit Declaration  
Features :
 Storage Memory
 Scope Global / File Scope
 Life time
  • Exists as long as variable is running
  • Retains value within the function
 Default initial Value
Zero

Live Example :
int num =  75 ;  

void display();

void main()
{
extern int num ;
printf("nNum : %d",num);
display();
}

void display()
{
extern int num ;
printf("nNum : %d",num);
}

Output :
Num : 75
Num : 75

Note :

  1. Declaration within the function indicates that the function uses external variable
  2. Functions belonging to same source code , does not require declaration (no need to write extern)
  3. If variable is defined outside the source code , then declaration using extern keyword is required

Bookmark & Share