There are four storage classes in C programming language, which are as follows −
- auto
- extern
- static
- register
Static variables
The keyword is static.
Scope
Scope of a static variable is that it retains its value throughout the program and in between function calls.
Static variables are initialised only once.
Default value is zero.
Example 1
Following is the C program for static storage class −
#include<stdio.h> main ( ){ inc ( ); inc ( ); inc ( ); } inc ( ){ static int i =1; printf ("%d", i); i++; }
Output
The output is stated below −
1 2 3
Example 2
Following is another C program for static storage class −
#include<stdio.h> main ( ){ inc ( ); inc ( ); inc ( ); } inc ( ){ auto int i=1; printf ("%d", i); i++; }
Output
The output is stated below −
1 1 1
Example 3
Following is the third example of the C program for static storage class −
#include <stdio.h> //function declaration void function(); int main(){ function(); function(); return 0; } //function definition void function(){ static int value= 1; //static variable declaration printf("\nvalue = %d ", value); value++; }
Output
The output is stated below −
value = 1 value =2