For as many documents as possible, the global variables may well be defined as external. An include declaration can be used to take in all the extern expressions in any programming document. Let’s have some examples of extern variables and functions to understand its working while using the Ubuntu 20.04 Linux operating system. Login from your Linux system and open the terminal shell via “Ctrl+Alt+T.”
Extern Variable in C:
Right after opening the terminal, you have to create a C script to work on. As this example is for working the extern variable within the C language, we will be working on two files separately. The “extern” has been used to reference a variable that exists in another file. So firstly, we have created the main C file using the “nano” keyword in the command.
Now it’s time to create the main function code. Firstly, include a header library and extern variable “a” in the code. We haven’t assigned a value to the variable “a” yet. The main function has been used to print the value of the extern variable “a,” which will be declared in another file. The extern variable will be able to get that value from a file. The main function ends here. Save this C document with the “Ctrl+S” and leave it while using “Ctrl+X.”
We have to create a new C type file, “test.c,” using the “nano” keyword in the command.
Now the document has been opened, write the simple below code in it. A header file has been included, and the integer type variable “a” has been specified with the value “24”. This variable value will be accessible to the main.c file using the “extern” variable in that file. Save and quit the document.
Compile both the documents at the same time via the “GCC” compiler. Try out the below “gcc” query having names of the files stated in it.
After the compilation process, it’s turned for both files to be executed now. So, consume the “a.out” below-stated query in the console window to do so. The output window has been showing the result from the main.c file while using the value of a variable “a” from another file, “test.c”. This is because of the extern variable, which is referencing the variable value of a file.
Let’s take another example for the extern variable. Open the “main.c” file. This time we are working on a single file.
Try the below code in this file. This code contains one extern variable defined outside of the main function, which is “x.” Another variable, “y,” is used while also defined as an extern within the main method. This is to show that the extern works the same for inside and outside of the function. The main function has one auto integer type variable “z” with the value “76,” and the variable “y” has been specified as “extern” here. We have used two print statements. The first one is printing the value of the auto variable “z,” and the second one is showing the value of both extern variables, “x” and “y.” After that, we have modified the value of the extern variable “x,” and the print statement will show the modified value in the terminal.
The compilation has been done using “gcc” with the name of a file “main.c”. If you find the below output, don’t panic because it’s not an error. You can easily go with the execution.
Execute the “main.c” file with the old “a.out” command having dot and backslash at its start. The output is on your screen. The first line is showing the local variable value. Another line is showing the value of both extern variables, and the last one is showing the updated value of an extern variable “x.”
Extern Function in C:
The extern function refers to a function defined in another file and works the same as the extern variable but with a great working capability. So open the main.c file to update it.
The main file has one unsigned volatile integer, “stp,” having 0 value in it. An extern function “func” has been specified, which will be defined in another file. The main function has the “while” statement to call the “func” extern method. Then “stp” variable value has been specified as “1,” and the code ends.
Open another file, “test.c”.
Here variable “stp” is specified as an extern variable, and the method “func” has been implemented. It checks the variable “stp” via the “if” statement and prints some message.
Compile both files at the same time.
Upon execution, it shows the output message specified in the “test.c” file specified in function “func.”
Ensure the external variables as external by the start of the document, before any method descriptions, to make it available to all methods inside the in-progress document. It would be best if you put all of the global variables that have been defined inside one document, including all the extern clauses, in a header document. We hope this guide will be easy to use and implement at your end to understand the “extern” theory.