C Programming

What is the Usage of Extern in C?

The term “extern” has been used to define the global variables within the C language. The word “extern” indicates that it is abbreviated from the word “external” which means outside. Hence, these variables are globally accessible throughout the C code. These variables are always initialized and defined outside of the main method. This guide is for the users who want to explore the working of the “extern” variable using the C language. So, let’s go ahead.

Example 01:

After the launch of the terminal by “Ctrl+Alt+T”, you need to create a C file. The “touch” command has been widely used for the creation of any file. So, we have created a file “test.c” with the “touch” instruction as per the below output.

$ touch test.c

Open the file to add some C code using the “nano” instruction. The word “nano” is used for the GNU editor used to update and edit files.

$ nano test.c

Within the file, we have written the code shown in the snap image. Included the input-output stream header file. After that, declare an extern variable “z”. We haven’t assigned any value to variable “z”. There is no function or main method in this code. The print statement has been used to display the value of the external variable “z”.

Save this code file by “Ctrl+S” and come back to the terminal via “Ctrl+X”. Compilation of this C file “test.c” has been done by a “gcc” compiler supported by Ubuntu 20.04 Linux system. The compilation error indicates that the external variable must be defined with some value.

$ gcc test.c

Example 02:

Let’s open the same file again to update it a little. This time, we will be using the “main” method in our code. We have declared the variable “z” outside of the main method.

The compilation of the file “test.c” throws an exception that the variable “z” has been undefined within the main method.  This is because variable “z” has not been assigned a value; hence, no memory allocation.

$ gcc test.c

Please open the file again and update it as shown in the snap. We have declared the external variable “z” outside of the main method and then changed its value to “13” within the main method. The print statement has been used to display the value.

Upon the compilation of the “test.c” file, we have got the same previous error along with 1 more error. We haven’t assigned a value to the external variable “z”; hence no memory allocated to “z”.  Also, we wanted to change the value of variable “z” to “13” while it has no memory assigned to it for the saving of a value yet.

$ gcc test.c

Example 03:

Let’s update the file once again a bit. This time, we have declared and defined an external variable “z” outside of the main method with the value “13”. Within the main method, a print statement has been used to print and display the external variable “z” value.

The compilation of a file “test.c” has been successful, as the output indicates.

$ gcc test.c

While the execution of a file successfully displays the success message and the value of variable “z”. This is because the variable “z” has been defined and declared at the same time.

$ ./a.out

As the default value of the external variable is always 0, you can use that to define it. While defining it 0, you can easily change its value within any method as below.

The compilation will be successful.

$ gcc test.c

The print statement will also work properly after this update.

$ ./a.out

You can also assign value to the variable outside of a function and then define it within the method for the same results.

Compilation and execution have been shown in the image. The extern value has been demonstrated in the output.

$ gcc test.c

$ ./a.out

Conclusion:

This article contains extern keywords in the C language by utilizing some simple and easy examples. The implementation contains the declaration of an extern variable within and out of the main method and will help you at its best.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content