Professional Documents
Culture Documents
External variables are also known as global variables. These variables are defined outside the
function and are available globally throughout the function execution. The “extern” keyword is
used to declare and define the external variables.
The keyword [ extern “C” ] is used to declare functions in C++ which is implemented and
compiled in C language. It uses C libraries in C++ language.
Here,
Example
Live Demo
#include <stdio.h>
extern int x = 32;
int b = 8;
int main() {
extern int b;
printf("The value of extern variables x and b : %d,%d\n",x,b);
x = 15;
printf("The value of modified extern variable x : %d\n",x);
return 0;
}
Output
The value of extern variables x and b : 32,8
The value of modified extern variable x : 15
In the above program, two variables x and b are declared as global variables.
In the main() function, variable is referred as extern and values are printed.
https://www.tutorialspoint.com/when-to-use-extern-in-c-cplusplus 1/2
10/5/2020 When to use extern in C/C++
extern int b;
printf("The value of extern variables x and b : %d,%d\n",x,b);
x = 15;
printf("The value of modified extern variable x : %d\n",x);
https://www.tutorialspoint.com/when-to-use-extern-in-c-cplusplus 2/2