C - Data Type extern keyword

Introduction

The extern keyword doesn't declare a global variable.

It informs the compiler that somewhere, a global variable is to be found.

Here's the extern keyword's format:

extern type name 

type is a variable type, the same type as the global variable being referenced.

name is the global variable's name.

The extern statement is coded at the top of the source code.

Code for main.c and a Global Variable

#include <stdio.h> 
#include <stdlib.h> 

void second(void); 

int count; 

int main() 
{ 
   for(count=0;count<5;count++) 
       second(); 
   return 0; 
} 

Code for second.c Using the Global Variable

#include <stdio.h> 

extern int count; 

void second(void) 
{ 
   printf("%d\n",count+1); 
} 

Related Topic