Home >Backend Development >C++ >In C language, the 'extern' keyword
External variables are also called global variables. These variables are defined outside the function. These variables are available globally throughout the execution of the function. The value of global variables can be modified by functions. Use the "extern" keyword to declare and define external variables.
Scope - They are not restricted to any function. They are found everywhere in the program, i.e. globally.
Default value - The default initialization value of global variables is zero.
Life cycle - Until the end of program execution.
The following are some important points about the extern keyword in C language:
External variables can be declared multiple times, but they can be defined only once.
Use the "extern" keyword to extend the visibility of a function or variable.
By default, functions are visible throughout the program and no extern function needs to be declared or defined. This only adds to the redundancy.
Variables with the "extern" keyword are only declared rather than defined.
Initializing an extern variable is considered the definition of the extern variable.
The following is an example of extern variables in C language
Demonstration
#include <stdio.h> extern int x = 32; int b = 8; int main() { auto int a = 28; extern int b; printf("The value of auto variable : %d</p><p>", a); printf("The value of extern variables x and b : %d,%d</p><p>",x,b); x = 15; printf("The value of modified extern variable x : %d</p><p>",x); return 0; }
The value of auto variable : 28 The value of extern variables x and b : 32,8 The value of modified extern variable x : 15
The above is the detailed content of In C language, the 'extern' keyword. For more information, please follow other related articles on the PHP Chinese website!