How to declare variables using extern in the C language?

In C language, the keyword “extern” is used to declare that a global variable or function is defined in another file.

When using extern to declare a global variable in a file, it means that the variable is defined in another file. This allows the variable to be used in the current file without the need to redefine it.

Below is an example of using the extern declaration for variables:

In file 1, declare a global variable:

int globalVariable = 10;

Declare the global variable in file 2 using the extern keyword.

extern int globalVariable;

This way, the globalVariable variable can be used in file 2 without needing to redefine it.

It is important to note that the ‘extern’ keyword is only used to declare variables or functions, not to define them. The definition of the variable or function should be done in another file.

Additionally, the extern keyword can also be used to declare global functions. The method is similar to declaring global variables, just add the extern keyword before the function declaration.

bannerAds