C Programming

Static Variables in C

C is a very flexible language when it comes to allocating different variables in a function or outside the function. A static variable is one of those variables that are declared “statically” in a program. The starting value of the static variables is zero. Static variables continue to function while the program runs.

Syntax of Static Variables

Following has been the C language basic syntax for declaring a “static variable”.

When defining the static variable, we may also set its initial state. Following is the method for initializing a static variable value in the programming language called C.

Everywhere that its scope is present, a constant variable value can be re-initialized. The re-initialization of a static variable’s value is demonstrated in the program below. In this program, we first include the header file “stdio” with the extension “.h” that is used to display the input and the output.

#include <stdio.h>

int main()
{

    static int x = 20;

    x++;

    printf("The value of x is: %d \n",x);

    x = 0;

    printf("After reinitialization of value x is: %d \n",x);
}

This is a basic and simple example of static variables where we have declared a variable “x” of data type “int” with the static keyword, which means the variable “x” will hold its value through a function call. We have initialized the value “20” to the variable “x,” and then the incremental operation has been applied to the variable to increment the value of “x” by 1. And then print the value of “x”. As you see, we have used “\n” in the printf() statement, which means to break a line or print the output in the new line. After this, we have re-initialized the value to the same variable “x,” which is “0”. Then we print the string that was written in double quotation marks.

Here is the output of the above illustration where the value of “x” is “21” after the implementation of the increment operation, but we have another value of the “x” that is “0” because we have re-initialized the variable “x” in the same function.

Major Usage for Static Variables

In this section, we will discuss the static variables and the global variables. Any declared variable that is used outside of the main() function is global. A static variable is one that was “statically” declared, which indicates that its lifecycle or extension is the duration of the whole execution of the program. Since they aren’t tied to a specific function, any accessible method can be used to access and modify global variables.

Using the static keyword within a function call: If we want to keep the same value for a variable on every call, we may specify it as a static variable within the function. Whenever the static keyword has been used inside of a method, the re-initialization of a parameter on consecutive function calls is prevented (for instance, when one static variable is created and initialized within a method). Consider a function that has a static variable called “x” with a beginning value of 20. The increment operator is used by the function to raise the value of “x” with each method call. The process is described below.

#include <stdio.h>

void function()
{

    static int x = 20;

    x++;

    printf("%d\n",x);
}


int main()
{
    function();
    function();
    function();
    function();
    function();
}

The variable “x” will start with a value of 20 when this method is first used, and it will increase to a value of 21 after that. The method will therefore print 21, 22, 23, 24, and 25 because we called the function 5 times in the main() function. Now, the variable “x” value won’t be reset to 20 if we run this method repeatedly. With incrementation, this should gain value from its preceding span, that is, 21, and become 22, and so on. Therefore, the method will print 22 this time and so on until the last function call.

This is the output of the above-compiled illustration:

Declaring a global variable using a static variable: Dynamic parameters can also be declared globally. This implies that a single parameter has the benefits of both a constant and a global variable. A static and a global variable’s scopes differ from one another. A constant parameter only has a blocking span, whereas a global variable can be accessed from any point in the program. The program that demonstrates the way it is defined is as follows.

#include<stdio.h>

static int y = 10;

void function() {

    static int x = 20;

    x++;

    printf("The static variable is: %d", x);
}


int main() {

    y++;

    printf("The global variable is: %d \n", y);

    function();
}

In this example, we have included necessary information about an input-output through the header file name “stdio.h,” where “.h” is the extension of the header file. Then, we declare a static variable outside the main() function that is the “y” of the data type integer. The value “10” is initialized to the static variable “y”. Then we have a method named “function()” that will also be declared globally outside the main() function. This function() has a static variable “x” of data type “int” with the value of “20,” and then we have implemented the increment operation to the variable “x”. After that, we have a printf() statement to print the string as it is written in the printf() function.

We must invoke this function after globally declaring the variable and function(). We have reached the main() section of the C program to invoke this method. In the main() function, we have again implemented an increment operation but now on the variable “y” and print the string of the global variable. Then we have called the function named “function()” that was declared globally.

Here is the output of the global variable illustration. First, the compiler will execute the global variable “y” because it was declared first in the program; after the implementation of the increment operation, variable “y” will become “11,” and then the function is called, which is “function()” and the static variable is printed.

Conclusion

We have learned how to re-initialize the static variable in the same function through a simple example. Then we also discussed how to declare the variable globally and how to write the function outside the main() body, and then how to callback the function through different examples. I hope that this tutorial will be beneficial to you as you learn about static variables in the C programming language.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content