Education

Static Variable in C- Check GATE Notes For CSE

A static variable possesses the property of preserving its actual value even after it is out of its scope! Thus, static variables in C preserve their previous value in their previous scope. These values are not initialized in the new scope again. 

In this article, we will look deeper into the concept of static variables in the C language. If you wish to learn more on this topic, read more on Static Variable in C here. 

Syntax: 

static data_type var_name = var_value; 

Here are some interesting facts about the static variables: 

1. The static int variable remains in the memory while a program is running. An auto or normal variable is destroyed whenever a function call where a given variable was declared is finally over. 

For example, one can use static int if they wish to count the total number of times any function is called. An auto variable, on the other hand, can NOT be used for this very purpose. 

For example, the program given below prints “1 2” 

#include<stdio.h> 

int fun() 

  static int count = 0; 

  count++; 

  return count; 

int main() 

  printf(“%d “, fun()); 

  printf(“%d “, fun()); 

  return 0; 

}

The output would be: 

1 2 

But the program that is given below prints “1 1” 

#include<stdio.h>

int fun()

{

  int count = 0;

  count++;

  return count;

}

int main()

{

  printf(“%d “, fun());

  printf(“%d “, fun());

  return 0;

}

The output would be: 

1 1 

2. The static variables are allocated memory in the data segment, not the stack segment. 

3. The static variables are initialized (like global variables) as 0 if not explicitly initialized. For example, in the program given below, the value of a is printed as 0, while the value of b is something garbage. 

#include <stdio.h>

int main()

{

    static int a;

    int b;

    printf(“%d \n %d”, a, b);

}

The output would be: 

[some_garbage_value] 

4. The static variables can only be initialized in C using constant literal. For example, the following program fails in the compilation process. 

#include<stdio.h>

int initializer(void)

{

    return 50;

}

int main()

{

    static int a = initializer();

    printf(” value of a = %d”, a);

    getchar();

    return 0;

}

The output would be: 

In function ‘main’: 

9:5: error: initializer element is not constant

     static int a = initializer();

     ^

Note that this very condition doesn’t hold in the C++ language. So if you save a given program as a C++ program, it would actually compile and run just fine. 

5. Static global variables and functions are also possible in C++/C. The purpose here is to limit the scope of a variable or function to a file. 

Also Read Best Ever Programming Assignment Help – Online

6. The static variables should not be declared inside a structure. The reason is that the C compiler requires the entire structure elements to be placed together. Meaning, the allocation of memory for structure members should be contiguous. 

Now, it is possible to declare a structure inside any function (stack segment) or even dynamically allocate the memory (heap segment). Also, it can even be global (BSS or data segment). Whatever might be, all the structure members must reside in the very same memory segment. It is because the value for any structure element is fetched when we count the offset of that element from the structure’s beginning address. 

Separating one member alone to a data segment defeats the purpose of a static variable. Thus, it is possible to have an entire structure as a static one. 

We hope this article was helpful for you to understand static variables in C in detail. If you wish to learn more on similar topics, you can check more GATE Notes For CSE in detail here. Keep preparing for your exams with a proper schedule and pass GATE 2023 with flying colours! Adios. 

manu bhadouria

I'm Manmohan Bhadouria, a digital marketer with a strong SEO background. I love writing blogs and stories, and exploring new places and adventures is my passion. I have a soft spot for all kinds of animals.

5 Ways To Lose Respect As An Seo