Static Variable in C: How to Declare and Use It?

Static Variable in C: How to Declare and Use It?

6 mins read2.7K Views Comment
Updated on Mar 16, 2023 17:23 IST

Before understanding the static variable, I hope you are familiar with variables, variable declaration, variable naming rules, and the scope of a variable.

2023_02_Understanding-Break-Statement-in-C-7.jpg

A static variable in C programming is a variable that holds its value after initialization. It is different from local and global variables. Variables are the holders for a value and allocate memory for the value type. There are various variable types in C programming, and the static variable is one of the special kinds of local variables with significant properties.

Table Of Contents

What is a Static Variable?

Static variable in C programming belongs to the class of storage type variables. It uses the “static” keyword before its definition. The keyword helps the compiler to understand the nature of the variable. The significant property of a static variable is holding its last value even after the end of the function. When you call a function, the static variable will give you the updated value. For example, one C program increases the value of a static variable by one. Whenever you call the function, the static variable value will be increased by 1.

Explore free C programming courses

Key Highlights of Static Variable in C 

  • It holds the value of the variable.
  • The default value of the static variable is ZERO.
  • The scope of the static variable is LOCAL. It can be defined within and outside the main function.
  • Its lifetime is till the execution of the function.
  • It holds its memory even after program execution.
  • Memory to static variables is allotted in the form of a data segment.
  • It uses the “static” keyword for its definition.
  • Static Variables are the only C variable that can be initialized using constant literals.
  • You cannot declare a static variable inside the C structure. It is because the C compiler allocates a continuous memory segment to all the structure members.

How to Declare Static Variable in C?

Static variables in C programming are declared using the “static” keyword followed by the data type and the variable’s name.

Syntax to declare a static variable: static data_type variable_name;

For example, static int a;

When you provide value to the declared static variable, it is its initialization. The static variable syntax consists of two parts: declaration and initialization

Syntax of Static variable: static data_type variable_name = initial_value;

For example: static int variable = 24;

How to Use Static Variables in C?

You can use static variables inside and outside the main function. Its scope is the entire function, in which it is declared. The static variable can be initialized any number of times.

Program to print the static variable without initialization

 
#include
int main()
{
int a;
static int b; //Declaring a static variable without initialization
printf("Default value of auto variable is: %d\n", a);
printf("Default value of static variable is: %d", b);
return 0;
}
Copy code

OUTPUT

 
Default value of auto variable is: 0
Default value of static variable is: 0
Copy code

NOTE: In the above code, the static variable “b” is not initialized and when we print its value. It is 0 as its default value.

Program to initialize a static variable and change its value

 
#include
int main()
{
static int a = 56; // initialization of static variable
printf(" First Value of static variable is: %d\n", a);
a = 67; //Reinitialization of static variable
printf(" Second Value of static variable is: %d\n", a);
return 0;
}
Copy code

OUTPUT

 
First Value of static variable is: 56
Second Value of static variable is: 67
Copy code

Program to use static variable in a user-defined function 

 
#include
//User defined function
void function()
{
int x = 10;
static int y = 10; //Declaring static variable
x = x + 10;
y = y+ 10;
printf("The value of auto variable: %d \n", x);
printf("The value of static variable: %d \n", y);
}
int main()
{
printf("The first function call \n");
function();
printf("The second function call \n");
function();
printf("The third function call \n");
function();
return 0;
}
Copy code

OUTPUT

 
The first function call
The value of auto variable: 20
The value of static variable: 20
The second function call
The value of auto variable: 20
The value of static variable: 30
The third function call
The value of auto variable: 20
The value of static variable: 40
Copy code

NOTE: In the above program, the static variable holds its last value. Every time the function call, the value of the static variable is increased by 10. The auto variable value is unchanged with the function call. With multiple function calls, the value of the static variable will update.

Program to use the static variable outside the main function

 
#include
static int variable = 10; //Declaring static variable outside the main function
int a = 20;
int main()
{
printf("%d \n %d", variable, a);
return 0;
}
Copy code

OUTPUT

 
10, 20
Copy code

Difference Between Static Variable and Global (auto) Variable

The lifetime of the static variable is not only local, it can be used as a global variable. When a static variable is defined in a function, the variable is local to the function only. When the static variable is defined outside the main function, the scope of the variable is global. The auto variable is a global variable, and the default value is garbage. The lifetime of the global variable is till the execution of the code.

Conclusion

The static variable is useful in C, and it holds the value. The variables in C are important, and we cannot do anything without variables. They hold the value and the memory for the variable type. In this article, we implement multiple C programs to understand the static variable’s uses. The static variables in C have special features, and some limitations like it cannot be declared in the structure.

Contributed by: Sonal Meenu Singh

FAQs

Can we declare a static variable?

Static variables in C programming are declared using the u201cstaticu201d keyword. Its declaration part consists of the static keyword, data type, and variable name. An example of a static variable declaration is: static int num;

Why do we use the static keyword?

A static keyword before a variable tells the C compiler that the variable is static. It is because it holds special properties and data segment memory.

What is static variables?

Static variables are local variables in C programming that are defined in a function using a u201cstaticu201d keyword. They have the property of holding their values and are active whenever they are called.

What is the use of a static variable in the C example?

Static variable lifetime is till the execution of the code, and even after code execution, it does not free its memory space. The auto variable lifetime is till the body of the code, and it destroys as soon as its role ends.

Can we initialize the static variable in C?

We can initialize a static variable by providing its data type value. We can initialize it at the time of declaration or later as per program requirements.

Why use static in C?

You can use a static variable in C programming when you want your variable to hold its value and update with every function call. You can change the initial value of the static variable anytime in the program.

What is the main difference between a global and static local variable?

Global variable lifetime is till their execution, and they do not hold their values. They can be declared without any keywords. The default value of the global variable is garbage and can be used anywhere in the program. Syntax of a global variable is: data_type variable_name = initial_value; Static variables are local variables, they are declared using the static keyword. They hold their values and do not free their memory space. The default value of the static variable is zero. Its syntax is: static data_type variable_name = initial_value.

Are static variables global in C?

Static variables are also global variables in C programming. To make a static variable global, you can declare it outside the main function and can use it in the file only.

What is the disadvantage of static?

A static variable does not free its memory space, It always holds its memory space whether the variable uses it or not. Manually controlling the memory space for creation and discretion is difficult with static variables.

What is the syntax of static variables in C Programming?

The syntax of static variables in C is: static data_type variable_name = initial_value. You can change the initialization value of the static variable and can initialize it later in the program.

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio