What is a static storage class in C language?


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

 Live Demo

#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

 Live Demo

#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

 Live Demo

#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("
value = %d ", value);    value++; }

Output

The output is stated below −

value = 1
value =2

Updated on: 15-Mar-2021

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements