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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/image1-52.png" data-lazy- height="21" src="data:image/svg xml,” width=”315″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/image3-50.png" data-lazy- height="22" src="data:image/svg xml,” width=”318″>

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”.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/image2-49.png" data-lazy- height="91" src="data:image/svg xml,” width=”472″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/image5-40.png" data-lazy- height="201" src="data:image/svg xml,” width=”717″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/image4-46.png" data-lazy- height="129" src="data:image/svg xml,” width=”481″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/image7-33.png" data-lazy- height="73" src="data:image/svg xml,” width=”449″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/image6-36.png" data-lazy- height="152" src="data:image/svg xml,” width=”536″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/image9-25.png" data-lazy- height="91" src="data:image/svg xml,” width=”544″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/image8-32.png" data-lazy- height="132" src="data:image/svg xml,” width=”467″>

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

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/image12-17.png" data-lazy- height="74" src="data:image/svg xml,” width=”544″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/image10-22.png" data-lazy- height="37" src="data:image/svg xml,” width=”350″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/image11-19.png" data-lazy- height="149" src="data:image/svg xml,” width=”547″>

The compilation will be successful.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/image13-11.png" data-lazy- height="76" src="data:image/svg xml,” width=”552″>

The print statement will also work properly after this update.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/image14-10.png" data-lazy- height="38" src="data:image/svg xml,” width=”325″>

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

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/image15-8.png" data-lazy- height="151" src="data:image/svg xml,” width=”537″>

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

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/image16-6.png" data-lazy- height="57" src="data:image/svg xml,” width=”349″>

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

<img alt="" data-lazy-src="https://secure.gravatar.com/avatar/d014e3711df41253029f4d4199698df8?s=112&r=g" data-lazy- height="112" src="data:image/svg xml,” width=”112″>

Kalsoom Akhtar

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