In the above example, when variable i is auto, each time increment() is called and it is re-initialized to one. When the function terminates, i vanishes and its new value of 2 is lost. The result: no matter how many times we call increment(), i is initialized to 1 every time.

On the other hand, if i is static, during the first call to increment(), i is incremented to 2. Because i is static, this value persists. The next time increment() is called, i is not re-initialized to 1; on the contrary its old value 2 is still available. This current value of i (i.e. 2) gets printed and then i = i + 1 adds 1 to i to get a value of 3.

External storage class

The extern variable is visible to all the programs. It is used if two or more files are sharing same variable or function.

This storage class uses extern keyword.

The features of a variable whose storage class has been defined as external are as follows:

Features

Description

Storage

Memory (RAM)

Default initial value

Zero

Scope

Global

Life Time

As long as the program’s execution doesn’t come to an end.

External variables differ from those we have already discussed in that their scope is global, not local. External variables are declared outside all functions, yet are available to all functions that care to use them.

Let’s see the simple example of external storage class –

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

#include<stdio.h>

inti;

intmain()

{

printf("\ni = %d",i);

increment();

increment();

decrement();

decrement();

return0;

}

increment()

{

i=i+1;

printf("\non incrementing i = %d",i);

}

decrement()

{

i=i-1;

printf("\non decrementing i = %d",i);

}

When you run the above program, the output will be following:

1

2

3

4

5

i = 0

on incrementing i = 1

on incrementing i = 2

on decrementing i = 1

on decrementing i = 0

From the above output, the value of i is available to the functions increment() and decrement() since i has been declared outside all functions.