Home > Backend Development > C++ > In C language, the 'extern' keyword

In C language, the 'extern' keyword

王林
Release: 2023-08-26 19:33:06
forward
721 people have browsed it

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

Example

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;
}
Copy after login

Output

The value of auto variable : 28
The value of extern variables x and b : 32,8
The value of modified extern variable x : 15
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template