Static Variable in C

A static variable possesses the property of preserving its actual value even after it is out of its scope. Thus, the static variables are able to preserve their previous value according to their previous scope, and one doesn’t need to initialize them again in the case of a new scope.

In this article, we will take a closer look at Static Variable in C according to the GATE Syllabus for CSE (Computer Science Engineering). Read ahead to know more.

Table of Contents

Syntax and Use of the Static Variable in C

We only need to initialize the static variables once. Thus, the compiler will persist with the given variable till we reach the end of our given program. One can define a static variable both- outside or inside the function. These are local to the block, and their default value is always zero. The static variables stay alive till the program gets executed in the end.

The syntax of the static variables in C is:

static datatype variable_name = value;

In this case,

value − It refers to the value that we use to initialize the variable. It is zero, by default.

variable_name − It refers to the name that any user gives to a variable.

datatype − It refers to the datatype of the variable here, such as char, float, int, etc.

Example of Static Variable in C

#include <stdio.h>

int main() {

auto int x = -30;

static int y = 16;

printf(“Value of the given auto variable is : %d\n”, x);

printf(“Value of the given static variable y is : %d\n”,y);

if(x!=0)

printf(“Sum of the auto variable and static variable is : %d\n”,(x+y));

return 0;

}

Output

The output generated here would be:

Value of the auto variable is : -30

Value of the static variable y is : 16

Sum of the auto variable and static variable is : -18

Use of the Static Variable keyword

We use the static variable keyword in the following given situations:

Static Local Variable – When we declare a local variable with a static keyword, it is called a static local variable. The static local variable’s memory stays valid throughout any program. But the scope of the visibility of these variables is similar to that of the automatic local variables. But when the static local variable gets modified by the function during the first function call, the modified value here will also be available for the next function.

Static Global Variable – When we declare a global variable with a static keyword, then it is called a static global variable. This variable gets declared at the top of any program. Added to this, its visibility stays throughout any program.

Static Member Variables – When we declare the member variables with a static keyword in a class, we call them static member variables. All the instances present in a class can easily access this type of variables, and we don’t require a specific instance to do that.

Some Important Points to Remember

1. The static int variable stays in a given memory when the program is still running, while an auto variable or a normal variable gets destroyed when the function call in which the variable got declared is over.

For instance, one can utilize the static int for counting the total number of times a function gets called, but they can’t use an auto variable for the very same purpose.

2. The memory allocation of static variables is not in the stack segment, but in the data segment.

3. The static variables are initialized as 0 (just like global variables) if it is not initialized explicitly.

For instance, the value of p in the below program will get printed as 0, while for q, the value is something garbage.

#include <stdio.h>

int main()

{

static int p;

int q;

printf(“%d \n %d”, p, q);

}

Output

The output generated will be:

0

some_garbage_value

4. The static variables in the C language can be initialized only by using constant literals. Note that the very same condition is not applicable in the C++ language. Thus, if you save this program as a C++ one, the compilation and running of the program would go just fine.

5. The static global functions, as well as variables, are also possible with the C and C++ languages. The reason why we use these is for limiting the scope of any function or variable to a file.

6. We must never declare the static variables inside the structure. The primary reason here is that the C compiler requires placing the entire structure elements together. It means that the memory allocation for the structure members must be contagious. It is possible to allocate the memory dynamically (i.e., heap segment), declare the structure inside a function (i.e., stack segment), or it can even be global (data segment or BSS). Whatever the case here, all the structure members must reside inside the same segment of memory. It is because we fetch the value for the structure element by counting the element’s offset from the initial address of a structure. The separation of just one member alone to the data segment defeats the very purpose of the static variable and then it becomes possible for us to have the entire structure be static.

Practice Problems on Static Variable in C

1. Is the process of initialization a prerequisite for the static local variables?

A. It depends on the standard

B. It depends on the compiler

C. Yes

D. No

Answer – D. No

2. What would be the output/result obtained from this code?

#include

void main()

{

static int a;

if (a++ < 2)

main();

}

A. Run time error

B. Infinite calls to main

C. main is called twice

D. Varies

Answer – C. main is called twice

3. What would be the output/result obtained from this code?

#include

void main()

{

static double a;

int a;

printf(“a is %d”, a);

}

A. Compile time error

B. Nothing

C. 0

D. Junk value

Answer – A. Compile time error

4. What would be the output/result obtained from this code?

#include

static int a;

void main()

{

int a;

printf(“a is %d”, a);

}

A. Nothing

B. Run time error

C. Junk value

D. 0

Answer – C. Junk value

5. What would be the output/result obtained from this code?

int main()

#include

void main()

{

static int a;

printf(“a is %d”, a);

}

A. 1

B. 0

C. Run time error

D. Junk value

Answer – B. 0


FAQs

What is the purpose of static variables in C?

The static variable is a variable that is capable of retaining its value between multiple numbers of function calls in a program.

Can we modify a static variable in C in another function after we send it as a parameter?

No, it doesn’t work that way. The static variable remains so because it retains its actual value in any such case.

Is static int func (int); //parameter as static accepted in the C language?

Yes, it is acceptable.

Keep learning and stay tuned to get the latest updates on GATE Exam along with Eligibility Criteria, GATE Syllabus for CSE (Computer Science Engineering), GATE CSE Notes, GATE CSE Question Paper, and more.

Also Explore,

Leave a Comment

Your Mobile number and Email id will not be published.

*

*