C – Static Storage Classes

Static storage classes:

  1. Keyword: static
  2. Memory: Inside RAM
  3. Scope: Static variables can be either local or global. If the variable is local, can access within the block. If it is global, we can access throughout the program.
  4. Default value: 0
#include<stdio.h>
static int x=10;
void test();
int main()
{
            static int y;
            printf(“y val : %d \n”, y);
            test();  
            return 0;
}
void test()
{
            printf(“x val : %d \n”, x);
}

Local variables memory will be deleted as soon as function execution completes.

#include<stdio.h>
void test();
int main()
{
            test();  
            test();
            test();
            test();
            return 0;
}
void test()
{
            auto int x=5;
            x=x+2;
            printf(“x val : %d \n”, x);
}

Static variable memory will be deleted only when program execution completes.

#include<stdio.h>
void test();
int main()
{
            test();  
            test();
            test();
            test();
            return 0;
}
void test(){
            static int x=5;
            x=x+2;
            printf(“x val : %d \n”, x);
}
Scroll to Top