What is the use of extern in C?

devquora
devquora

Posted On: Feb 22, 2018

 

Global Static variables are global to the file in which they are defined. They are used when the same global variable is referenced in each of the files and these variables must be independent of each other across the files. The use of global variables is not recommended, as function independence is one of the basic idea of modular programming. Global variables should be used only when it is inevitable (i.e., when all the functions need a particular variable).

When program spans across different files and we want to have a global variable accessible to all functions in these files, the keyword extern should be used before the data type name in the declarations in all these files where it is accessed except one. The linker requires that only one of these files have the definition of the identifier. Global variables definitions can occur in one file only.

Consider the following program example to understand better the use of keyword extern,

/* file sub_file.c Defines the function sub_func */
extern int globalVar;
 
void inc_global()
{
  globalVar++;
}

The code of the second file, mainfile.c is given below,

/*file mainfile.c Defines the function main and uses the function
inc_global. Compile with cc mainfile.c sub_file.c */
int globalVar;
 
void main()
{
 /*Assign something to globalVar*/
 globalVar = 10;
 
printf( " globalVar before calling = %d\n", globalVar);
inc_global();
printf( " globalVar after calling = %d\n", globalVar);
}

Observe the integer globalVarhas been declared in the sub_file.c as extern globalVar;

Upon encountering the above statement, the compiler knows that an integerglobalVarexists. It goes ahead to produce the object file. In the file mainfile.c, the actual definition of globalVar occurs with the statement.
int globalVar;

Note that no special keywords are necessary to say that globalVarspans across files. This is because a declaration such as the one shown above tells the compiler that the integerglobalVaris exported and can be accessed in different files.

The linker is then invoked to link the two object together. When the compiler produces the object file of sub_file.c, it does not know the exact location of the variable globalVar. So, it includes the information in the locations in the object file where the global variable is accessed. In the object file of mainfile.c, the compiler will store this information (that a global integer called globalVarexists, that can be used across files).
When the linker links these two object files together, it knows the location ofglobalVarfrom the object file of mainfile.c. It uses this information to complete the object file of sub_file.c, where the compiler had encoded the information where globalVaris accessed in sub_file.c

    Related Questions

    Please Login or Register to leave a response.

    Related Questions

    C Programming Interview Questions

    What are static variables in C ?

    C defines another class of variables called static variables. Static variables are of two types:Static variables tha..

    C Programming Interview Questions

    What is a scope resolution operator in C ?

    The scope resolution operator (::) is useful in C programming when both global and local variables have the same name,..

    C Programming Interview Questions

    What is register variable in C language ?

    C language allows the use of the prefix register in primitive variable declarations. Such variables are called register..