C Programming

Extern in C

C has an added benefit over other scripting languages since it is a procedural programming language. We can also say that C is a compiler-based programming language that distinguishes between Python and Java, which are interpreter-based. As a result, coding compilation and execution are expedited. The data types in the C language are the declarations or definitions for variables. This defines the variety and volume of data about the variables. C is a strong choice language for beginners to learn programming with. The C programming language is used to write the Unix operating system and practically all Unix applications. Due to this reason, C is currently a frequently used professional language.

The operative systems of component programs, in a particular way, were initially developed using the C language. Because it produces the code that executes almost rapidly code written in assembly language, and C was adopted as a system development language. To run any C-language program, we must first compile it using a C compiler, which turns C into a computer-understandable language. Visual Studio is the best compiler for user code compilation in C language.

The topic we will discuss in our article is related to the “C” language, which is a simple keyword that is “extern”. The programmers and coders use this keyword to extend the visibility of any variable and function. Eventually, we don’t need function definition in the “C” language because the function will remain visible in the code by default.

Furthermore, it has a proclivity to disseminate variables and functions visibility over many source files. We can say that declaring a variable without the assassination of memory allocation. It is not required regularly to declare any function while coding a program. It tweaks the affiliation criteria so that the C compiler does not amend the symbol with extra information. In “C” language, we have another prefix, “static”, which could not be used along with the “extern” in the same declaration of a function definition. External is standardized as an “extern” in the C programming language.

Procedure

In this article, we will try to learn what an extern keyword in “C” is and why this keyword is used. We will see how and where it could be used along with the help of the description of our code by definition of a function with the implementation of different examples of C language.

Example # 01

As we have discussed earlier, the introduction of the “extern” keyword is in our article, and now we will look at the working of this keyword with our examples. In the first example, we have first taken our header file in our code for importing the “C” language. After that, we used integer data type with our main keyword “extern” for the declaration of variable “m” and assigned our variable with the value “20”. Now, we declared our second variable “n” with integer data type as we used integer values and assigned the “n” variable as the value of “10”. After defining both variables, we will put our main function on the code.

In the main function, we have declared the variable “k” of integer data type along with the “auto” keyword and assigned a value of “21” to the variable “k”. Now, “auto” is a keyword in “C” language that is used for the declaration of a local variable in the code. For globally assigning the variable “n”, we used the “extern” keyword with the integer data type.

Then we used the “printf ()” function to display our prompt message on the output screen. In the first print function after the output message, we assigned it the value of variable “k”. In the second print function, we stored variables “m” and “n” along with the message that we can see on our code display. “\n” is used for making the screen point cursor up to the new line after executing the present code line. Then “%d” is used as if it could have an integer value stored in their variable.

Here, we modified the value of variable “m” to “25”. Again, we used our print function to display the modified value that is newly stored in our variable “m”. So, we can see that with the same variable “m”, we have represented two values before and after modification as “20” and “25”.

#include <stdio.h>

extern int m = 20;

int n = 10;

int main()

{

auto int k = 21;

extern int n;

printf("AuTO VARIABLE VALUE :- %d\n", k);

printf("EXTERN VARIABLE m AND n VALUE :- %d,%d\n",m,n);

m = 25;

printf("EXTERN VARIABLE m MODIFIED VALUE :- %d\n",m);

return 0;

}


In the output, we have accessed all values stored in variables “k”, “m”, and “n”. with the usage of the “extern” keyword, we access that variable “m”, which represents two values that we can see in our output display.

Example # 02

Now, let us look at our second example in which we imported the header file of C, which is “<stdio.h>”. This header consists of a lot of coding quenched or composed into a single word. We declared or defined our variable “p” of an integer data type with the “extern” keyword, and we just assigned it with the value of “5000”, as we can see in our code display, as well. Now, let us come to the program’s main function, where we used the print function and assigned it to the value of our variable “p”. By this step, it will take the value stored in variable “p” and display it on the output screen. To end the code implementation, we used the “return” statement, which will return the flow of execution of our program.

#include <stdio.h>

extern int p=5000;

int main()

{

printf("%d", p);

return 0;

}

The output will display the value assigned to variable “p” that is “5000”, which we can use in the following output.

Conclusion

This article covers the concept of the “extern” keyword in the “C” Language. This enables us to understand how we use “extern” in our examples. We implemented two “C” language code examples that clearly expressed the function of the “extern” keyword. In the first example, we used “extern” along with two declarations of variables. While in the second example, we used a simple extern function for a simple definition of a variable and displayed it on the output.

About the author

Kalsoom Bibi

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