×

Search anything:

Extern in C

Binary Tree book by OpenGenus

Open-Source Internship opportunity by OpenGenus for programmers. Apply now.

Reading time: 30 minutes | Coding time: 5 minutes

Extern is a keyword in C programming language which is used to declare a global variable that is a variable without any memory assigned to it. It is used to declare variables and functions in header files. Extern can be used access variables across C files.

Syntax:

extern <data_type> <variable_name>;
// or
extern <return_type> <function_name>(<parameter_list>);

Example:

extern int opengenus;
// or
extern int opengenus_fn(int, float );

To understand the significance better, we need to understand three terms:

  • Declaration

Declaration of a variable means that the compiler knows that the variable exists but no memory or data has been assigned to it. To stop at this step, we need extern keyword like:

extern int opengenus;

opengenus is a valid integer variable but no memory has been allocated to it.

  • Define

Defining a variable means assigning the variable the required memory. At this step, garbage value is placed at the memory location but the variable is ready to be used.

Example:

int opengenus;
  • Initializing

Initializing a variable means assigning a value in the memory location of the variable. Example:

int opengenus = 1;

This table summarizes the idea:

Code Declare Define Initialize
; No No No
extern int opengenus; Yes No No
int opengenus; Yes Yes No
int opengenus = 1; Yes Yes Yes

Using extern

The main rule is that extern should be used in header files only. Though there are ways around this but it is not advised for code safety.

Let us code a header file named "opengenus_header.h" as:

extern int opengenus;
extern void get_data(void);

opengenus_header.h has both variables and functions but in production, it is advised to keep variables and functions in separate header files.

This will be our common C code (og.c) used to declare and define common variables and functions.

#include "opengenus_header.h"
int opengenus = 1;

This is our main code (main.c) which is linked with our common C code (og.c) and uses the same global/ extern variables and functions.

#include "opengenus_header.h"
#include <stdio.h>

int main(void)
{
    printf("%d", opengenus);
}

Compile it as:

gcc main.c og.c

Execute it as:

./a.out

Output:

1

While compiling, if you do not link og.c with main.c, you will get a compile time error.

Erroneous compilation:

gcc main.c

Error:

/tmp/ccrE2C9Q.o: In function `main':
main.c:(.text+0x6): undefined reference to `opengenus'
collect2: error: ld returned 1 exit status

This is because the memory for opengenus variable is being allocated in file og.c and it has not been linked. So, in main.c, in this compilation step, we are using the variable without declaring it which gives an error.

If we compile og.c only, we will get an error as it has no main function.

gcc og.c

Compile time error:

/usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../lib64/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status

Hence, the overall and good coding strategies are:

  • All variables and functions in header files should be extern
  • Separate header files should be used for variables and functions
  • Use a C code file to declare the variables and functions and use this in end user code
  • Extern must be used instead of using global variables
OpenGenus Tech Review Team

OpenGenus Tech Review Team

The official account of OpenGenus's Technical Review Team. This team review all technical articles and incorporates peer feedback. The team consist of experts in the leading domains of Computing.

Read More

Improved & Reviewed by:


Extern in C
Share this