“extern” keyword in C


External variables are also known as global variables. These variables are defined outside the function. These variables are available globally throughout the function execution. The value of global variables can be modified by the functions. “extern” keyword is used to declare and define the external variables.

Scope − They are not bound by any function. They are everywhere in the program i.e. global.

Default value − Default initialized value of global variables are Zero.

Lifetime − Till the end of the execution of the program.

Here are some important points about extern keyword in C language,

  • External variables can be declared number of times but defined only once.

  • “extern” keyword is used to extend the visibility of function or variable.

  • By default the functions are visible throughout the program, there is no need to declare or define extern functions. It just increase the redundancy.

  • Variables with “extern” keyword are only declared not defined.

  • Initialization of extern variable is considered as the definition of the extern variable.

Here is an example of extern variable in C language

Example

 Live Demo

#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
", a);    printf("The value of extern variables x and b : %d,%d
",x,b);    x = 15;    printf("The value of modified extern variable x : %d
",x);    return 0; }

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

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 24-Jun-2020

17K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements