Static Storage Class in C (Keyword)

Static Storage Class in C

Static variables can be used within function or file. Unlike global variables, static variables are not variables outside their fucntions or file, but they maintain their values between calls. The static specifier has different effects upon local and global variables.

Static Storage Class in C (Keyword)

Local Implementation of Static Storage Class

  • When a static Specifier is applied to function or block the compiler create permanent storage for it.
  • Static Local variable remains visible only to the function or block in which it is defined.
  • A Static Local variables is a local variable that retains its value between functions calls.

Code

Run
#include 
void demo()
{
    static int i = 1;
    {
        static int i;
        printf("%d ",i);
        i++;
    }
    printf("%d\n",i);
    i++;
}

int main()
{
   demo();
   demo();
}

Output

0 1
1 2

Global Implementation of Static Storage Class

  • When static specifier is applied to a global variable or a function then compiler makes that variable or function known only to the file in which it is defined.
  • A static global variable is a global variable with internal linkage.
  • That means even the variable is global but other files are not having any knowledge of it.
  • Other file cannot access or alter it’s content directly.

Code

Run
#include 
void func_1();
int a, b = 10;
int main()
{
    func_1();
    func_1();
    return 0;
}

void func_1()
{
    int a = 1;
    static int b = 100;
   printf("a = %d\n", a);
    printf("b = %d\n\n", b);
    a++;
    b++;
}

Output

a = 1
b = 100

a = 1
b = 10

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

4 comments on “Static Storage Class in C (Keyword)”