The ones who are crazy enough to think they can change the world are the ones who do.
- Steve Jobs

C Static Storage Class

What Is Static Storage Class

  • A static variable is a variable that tells the compiler to retain the value until the program terminates.
  • They are created once when the function is called, even though the function gets repeated it retains the same value and exists until the program terminates.
  • The default value of static variable is zero(0).
  • The keyword for a variable to declared under static storage class is static
C Static Storage Class

Syntax - Static Storage Class

Syntax
static int st;

C program - Static Storage Class

Let's workout a program to demonstrate static storage class in C.

extern.c
#include <stdio.h>
//function declaration
void subfun();
int main()
{
subfun();
subfun();
subfun();
return 0;
}
//function definition
void subfun()
{
static int st = 1;  //static variable declaration
printf("\nst = %d ", st);
st++;
}
  • st = 1
  • st = 2
  • st = 3

Note:

The program illustrates that, when the function is called, the static variable inside a function is initialized to 1 and retains the incremented value for the upcoming function calls.

Reference

Keyword static
Storage Type Memory
Scope Block Scope
Life Time Static life time
Visibility Local/Global
Default Value 0
Default Storage Class No

Report Us

We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.

Report