The concept of extern variables in C

By | 15/09/2012

Most of the C projects are not single file projects. The code is spread over tens or hundreds of files. Now suppose there is a situation where you have declared and defined a variable in one file (say file1) and want to use the same variable in other file (say file2) by just telling the compiler that this variable is already defined somewhere else. What would you do? Well, this can be done through the concept of extern variables in c.

Extern variables in C

Well, the answer is to declare that variable as ‘extern’ in file2. The keyword extern tells the compiler that this is not a definition for this variable as it’s already defined in some other file. This way compiler will let you use the same variable in multiple files.

An example

Here is an example project code (header.h , main.c and extern.c ) that uses a variable ‘global’ as extern.

/* Header.h */

#ifndef STDIO_H 
#include<stdio.h>
#define STDIO_H 
#endif 
int func(void);

 

/* main.c */ 
#include"header.h" 

int global = 1; 

int main(void) 
{ 
    printf("\nInside main()\n"); 
    func(); 
    return 0; 
}

 

/* extern.c */ 
#include"header.h" 
extern int global; 

int func(void) 
{ 
    printf("\n value of extern variable is [%d]\n",global); 
    return 0; 
}

So we see that ‘header.h’ is the common header file. A variable ‘global’ is defined in ‘main.c’ while it is being used in ‘extern.c’ as an extern variable.

Here is the output when the above code is run :

$ gcc -Wall main.c extern.c -o extern 

$ ./extern Inside main() value of extern variable is [1]

So, from the output above its clear that the variable ‘global’ was successfully declared in ‘extern.c’ as an extern variable and was successfully accessed in the func() function in the same file.

Now, if we do not declare it as an extern in extern.c then ‘global’ becomes a new global variable for extern.c and the compiler will throw a multiple definitions error for this variable.

Here is the new extern.c :

#include"header.h" 
int global=0; // this variable is no more extern now 

int func(void) 
{ 
    printf("\n value of extern variable is [%d]\n",global); 
    return 0; 
}

Here is the output when the project was compiled again:

$ gcc -Wall main.c extern.c -o extern 
/tmp/cc6FH8Nu.o:(.bss+0x0): multiple definition of `global' 
/tmp/cc5fkH7f.o:(.data+0x0): first defined here collect2: ld returned 1 exit status

So we see that the compiler complained about the variable ‘global’ being defined at multiple places.

Also, if the variable ‘global’ is removed from extern.c, then the compiler will complain that the variable ‘global is not declared/defined anywhere.

Here is the new extern.c

#include"header.h" 
//int global=0; 

int func(void) 
{ 
    printf("\n value of extern variable is [%d]\n",global); 
    return 0; 
}

Here is the output when the project was compiled again:

$ gcc -Wall main.c extern.c -o extern 
extern.c: 
In function ‘func’: extern.c:7: error: ‘global’ undeclared (first use in this function) 
extern.c:7: error: (Each undeclared identifier is reported only once 
extern.c:7: error: for each function it appears in.)

We see that compiler complains about ‘global’ being undeclared in extern.c file.

So, to access a variable that is defined in main.c, it has to be declared as extern in extern.c file.

Please note that in C, functions are extern by default.

Leave a Reply

Your email address will not be published. Required fields are marked *