Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C extern keyword

C extern keyword

C Variables C Variables

In C, using extern the keyword to declare a variable makes it an external variable, indicating that the variable is defined elsewhere (usually in another file).

extern The keyword is used in C to declare external variables or functions so that they can be shared and used across multiple files.

extern It is mainly used to achieve modular programming and code separation.

extern Variable declarations and definitions are usually placed in different files.


extern Variables

The following demonstrates how to define an external variable in one file and declare and use it in another.

1. Define and initialize (in one file, file1.c)

Example

#include

// Define and initialize the external variable

int

d

=

3

;

int

f

=

5

;

void

printExternVariables

(

)

{

   

printf

(

"d: %d, f: %d\n"

,

d

,

f

)

;

}

** 2. Declare and use (in another file, file2.c)**

Example

#include

// Declare the external variable

extern

int

d

;

extern

int

f

;

// Declare the external function (must be declared before use)

extern

void

printExternVariables

(

)

;

int

main

(

)

{

   

// Use the external variable

   

printf

(

"d in main: %d\n"

,

d

)

;

   

printf

(

"f in main: %d\n"

,

f

)

;

   

   

// Call a function defined in another file

    printExternVariables

(

)

;

   

   

return

0

;

}

Compile and run file1.c and file2.c together (see below for the compile command). The output is:



d in main: 3

f in main: 5

d: 3, f: 5


extern Functions

Similar to variables, a function can be defined in one file and declared and used in another.

1. Define the function (in one file, file1.c)

Example

#include

void

printMessage

(

)

{

   

printf

(

"Hello from printMessage!\n"

)

;

}

2. Declare and use the function (in another file, file2.c)

Example

#include

// Declare the external function

extern

void

printMessage

(

)

;

int

main

(

)

{

   

// Call the external function

    printMessage

(

)

;

   

return

0

;

}

3. Compile multiple files

To use extern variables and functions, all relevant files must be compiled together. For example:



gcc file1.c file2.c -o program

./program

After compiling and running the above function example, the output is:



Hello from printMessage!


Notes

Use extern the following points should be noted:

Points to note Description
Definition and initialization An external variable must be defined and initialized in one file and only declared in other files.extern A declaration cannot include initialization.
Scope extern Variables and functions have global scope and can be accessed anywhere in the program.
Duplicate definitions Avoid defining the same variable or function in multiple files, as this causes linker errors.
Using header files Usually, extern put the declarations in a header file, and then include that header file in the source files that need them.

Use of header files

To simplify management, you can use a header file to declare extern variables and functions.

Code in variables.h:

#ifndef VARIABLES_H

#define VARIABLES_H

// Declare the external variable

extern

int

d

;

extern

int

f

;

#endif

Code in functions.h:

#ifndef FUNCTIONS_H

#define FUNCTIONS_H

// Declare the external function

void

printExternVariables

(

)

;

void

printMessage

(

)

;

#endif

Then include the header file in the source file:

Code in file1.c:

#include

#include "variables.h"

#include "functions.h"

// Define and initialize the external variable

int

d

=

3

;

int

f

=

5

;

void

printExternVariables

(

)

{

   

printf

(

"d: %d, f: %d\n"

,

d

,

f

)

;

}

void

printMessage

(

)

{

   

printf

(

"Hello from printMessage!\n"

)

;

}

Code in file2.c:

#include

#include "variables.h"

#include "functions.h"

int

main

(

)

{

   

// Use the external variable

   

printf

(

"d in main: %d\n"

,

d

)

;

   

printf

(

"f in main: %d\n"

,

f

)

;

   

   

// Call the external function

    printExternVariables

(

)

;

    printMessage

(

)

;

   

   

return

0

;

}

Also use gcc file1.c file2.c -o program to compile and run. The output is:



d in main: 3

f in main: 5

d: 3, f: 5

Hello from printMessage!

Managing extern declarations through header files in this way makes the code clearer and easier to maintain.


C Variables C Variables