This blog is under construction

Sunday, 13 May 2012

Extern storage class in C

extern variables:
These variables are declared outside the function block and it is available to all the functions.  Extern variables are usually global variables.  If a program has the same name for global variable and auto variable, then the priority will be given to the local variable first.   If we want the variable to be visible to multiple source files, then the keyword extern is used in variable declaration.  Below is the general form of extern variable.

extern int num;

Basically, no space is allocated for extern variable during the time of declaration.  So, they cannot be initialized.

// pgm1.c
extern int num;
void print() {
     printf("Value of num is %d\n", num);


int num = 100

Here, num in pgm1.c is declared as extern and num in pgm2.c is declared as non-extern. So, memory will allocated for num in pgm2.c since it is non-extern.  And the num in pgm1.c refers to the memory allocated for num in pgm2.c.  Whenever we need to use num in any file, we need include extern declaration for num.

Simply,  extern variables are declared only once per program(eg. num in pgm2.c) and they can be referenced in many source files(eg. num in pgm1.c)

Example C program to illustrate the usage of non-extern global variable:
  #include <stdio.h>
  void add();
  int value = 100;

  int main() {
        auto int value = 10;
        printf("Value:%d\n", value);
        printf("Value after add() function call:%d\n", value);
        return 0;

  void add() {
        value = value + 10;
        printf("Value in add() function: %d\n", value);

  jp@jp-VirtualBox:~/cpgms/storage$ ./a.out
  Value in add() function: 110
  Value after add() function call:10

Example C program to illustrate the usage of extern variables:
  // pgm1.c
  #include <stdio.h>
  int num = 100;  // memory allocation takes place
  int main() {
        printf("main() - value of num  : %d\n", num);
        printf("main() - Address of num: 0x%x\n", &num);
        return 0;

  // pgm2.c
  extern int num;  // reference to num in pgm1.c
  void increment() {
        printf("increment() - Address of num      : 0x%x\n", &num);
        printf("increment() - num before increment: %d\n", num);
        num = num + 10;
        printf("increment() = num after increment : %d\n", num);

  // pgm3.c
  extern int num;  // reference to num in pgm1.c
  void printnum(char *str) {
        printf("printnum() - value of num  : %d\n", num);
        printf("printnum() - address of num: 0x%x\n", &num);

  jp@jp-VirtualBox:~/$ gcc pgm1.c pgm2.c pgm3.c
  jp@jp-VirtualBox:~/$ ./a.out
  main() - value of num  : 100
  main() - Address of num: 0x804a014
  increment() - Address of num      : 0x804a014
  increment() - num before increment: 100
  increment() = num after increment : 110
  printnum() - value of num  : 110
  printnum() - address of num: 0x804a014

From the above output, we could infer that the extern variable num in pgm2.c and pgm3.c are references to the memory allocated for the variable num in pgm1.c

No comments:

Post a Comment