English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

When to Use C / C ++Reference Variables in

External variables are also called global variables. These variables are defined outside the function and are globally available during function execution. The "extern" keyword is used to declare and define external variables.

The keyword [extern "C"] is used to declare C implemented and compiled in C ++Function. It uses C ++Language C library.

Here is the syntax of extern.

extern datatype variable_name; // variable declaration using extern
extern datatype func_name(); // function declaration using extern

Here,

datatype-The data type of the variable, such as int, char, float, etc.

variable_name-This is the variable name given by the user.

func_name-Functionname.

Here is an example of extern:

Example

#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 Result

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.

extern int x = 32;
int b = 8;

In thismain()In the function, the variable is called extern and the value is printed.

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);
SQL Tutorial