Updated April 1, 2023
Introduction to C++ extern
The variables that are defined outside a function in C++ are called global variables, and such global variables can be accessed globally during the execution of the function and global variables are also called external variables. The keyword used to define and declare external variables is extern, and functions can also be declared globally using the keyword extern C in C++. But these functions are compiled and implemented in C language, and C libraries present in C++ language is used by these functions and datatype, name of the variable that is to be defined globally or name of the function that is to be defined globally are used along with these extern keywords while defining global variables or global functions.
Syntax of extern in C++:
extern datatype name_of_the_variable
extern datatype name_of_the_function
Where,
- datatype represents datatypes such as int, float, double etc.
- name_of_the_variable represents the variable name which is to be defined and declared globally.
- name_of_the_function represents the function name which is to be defined and declared globally.
Working of extern in C++
- The variables that are defined outside a function in C++ are called global variables, and such global variables can be accessed globally during the execution of the function.
- The global variables are also called external variables, and the keyword used to define and declare external variables is extern.
- Functions can also be declared globally using the keyword extern C in C++, but these functions are compiled and implemented in C language, and these functions use C libraries present in C++ language.
- The datatype, the name of the variable that is to be defined globally or the function that is to be defined globally, is used along with these extern keywords while defining global variables or global functions.
Examples of C++ extern
Given below are the examples of C++ extern:
Example #1
Code:
#include <stdio.h>
//defining a global variable called firstvariable using the keyword extern and storing an integer value inside it
extern int firstvariable = 100;
int main()
{
//displaying the value of the global variable before modifying it
printf("The value of the variable before modifying is : %d\n",firstvariable);
//modifying the global variable inside the function though it is declared and defined outside the function
firstvariable = 50;
//displaying the modified value of the global variable
printf("The value of the variable after modifiying is : %d\n", firstvariable);
return 0;
}
Output:
In the above program, a variable called firstvariable is declared and defined globally by making use of the keyword extern outside the function, and an integer value is stored inside the variable. Then the value of the variable defined globally using the extern keyword is displayed inside the function without any modifications. Then the value of the variable defined as a global variable is modified inside the function, and then when the value of the variable is printed, the modified value inside the function is displayed as the output on the screen.
Example #2
Code:
#include <stdio.h>
//defining a global variable called firstvariable using the keyword extern and storing an integer value inside it
extern int firstvariable = 40;
int main()
{
//displaying the value of the global variable before modifying it
printf("The value of the variable before modifying is : %d\n",firstvariable);
//modifying the global variable inside the function though it is declared and defined outside the function
firstvariable = 20;
//displaying the modified value of the global variable
printf("The value of the variable after modifiying is : %d\n", firstvariable);
return 0;
}
Output:
In the above program, a variable called firstvariable is declared and defined globally by making use of the keyword extern outside the function, and an integer value is stored inside the variable. Then the value of the variable defined globally using the extern keyword is displayed inside the function without any modifications. Then the value of the variable defined as a global variable is modified inside the function, and then when the value of the variable is printed, the modified value inside the function is displayed as the output on the screen.
Example #3
Code:
#include <stdio.h>
//defining a global variable called firstvariable using the keyword extern and storing an integer value inside it
extern int firstvariable = 10;
int main()
{
//displaying the value of the global variable before modifying it
printf("The value of the variable before modifying is : %d\n",firstvariable);
//modifying the global variable inside the function though it is declared and defined outside the function
firstvariable = 5;
//displaying the modified value of the global variable
printf("The value of the variable after modifiying is : %d\n", firstvariable);
return 0;
}
Output:
In the above program, a variable called firstvariable is declared and defined globally by making use of the keyword extern outside the function, and an integer value is stored inside the variable. Then the value of the variable defined globally using the extern keyword is displayed inside the function without any modifications. Then the value of the variable defined as a global variable is modified inside the function, and then when the value of the variable is printed, the modified value inside the function is displayed as the output on the screen.
Recommended Articles
This is a guide to C++ extern. Here we discuss the introduction, working of extern in C++ along with examples, respectively. You may also have a look at the following articles to learn more –