C Programming

Static Variables in C

Static variables do have the ability to maintain their meaning even though they’ve been removed from their scope! As a result, static variables keep their former value in the setting mentioned above and do not need to be initialized once in the new environment. Static variables are always set up once. The compiler holds the variable until the completion of the code. Within and outside of the method, static variables may be specified. Static variables have a standard fixed value of 0. The static variables remain active until the code is over. Ordinary variables are restricted to the range in which they are specified, whereas static variables have a scope that extends within the code.


Here is the syntax of the declaration of a static variable in the C language.

Static variable-type variable-name = variable-value;

The replica of a static variable is generated once it is specified. The key reason we are being used instead of a local variable seems to be that the value allocated in the range where it may be found is preserved. Throughout the fresh scope of a code, the developer makes sure not to initialize the parameter again. It can be comprehended in the entire code. It tells the compiler to restrict the width of a variable or method in the code, and it stays in place for the duration of the code. This feature prevents accidental accessibility to static variables by encapsulating or hiding them from the rest of the code. Let’s have some examples to elaborate on the working of static variables. As we have been working on C language, make sure you have a “GCC” compiler mounted on your Linux system to compile the C code.

Example 01:

Open the command shell via “Ctrl+Alt+T” and create a C type file via the “nano” editor using the below query. The file’s name is “test.c,” and it will directly be opened in GNU Nano Editor.

$ nano test.c

You can see the below snippet code. Write the same code in your GNU file. The code contains a single required header, the main function, and another function named “value.” We have initialized an integer type variable “num” in the function “value” having the value 0. Then the value of “num” will be incremented and returned to the main function. In the main method, we have stated three print statements to call the “value” function and print the result of a variable “num,” which has been incremented. Press the “Ctrl+S” key to save the code and quit the file using “Ctrl+X.”

Now compile the code using the “gcc” command as below. If no error occurs, then it means the code is correct.

$ gcc test.c

After the compilation of a test.c file, we will be executing this file via the “a.out” below query in the console. You will see that the output will only increment the value of variable “num” once, even after calling the function thrice. This is because the value of variable “num” has not been stated as static, due to which whenever the function “value” has been called, it regenerates the original value, which is 0. That is why it only increments 0 to 1 all the three types upon calling.

$ ./a.out

Let’s update the code. Open the test.c C language file again using “nano” query.

$ nano test.c

We have just to specify the variable “num” is static. For that, use the keyword “static” at the start of the variable “num” and before the variable type, which is an integer in our case. Every time the main function calls the method “value,” the static variable “num” will be incremented, and the value will get static. This means the newly updated value will be used in the next call, and so on, the process will be repeated.

Let’s compile the C file first via the “gcc” compilation query in the console terminal.

$ gcc test.c

Upon executing the file using the “a.out” instruction, you can see that the output is showing the incremented value every time upon calling the method “value” due to usage of the “static” keyword before the variable “num.”

$ ./a.out

Example 02:

Let’s see another example of static variable type. Open the same C file to write a code in it.

$ nano test.c

You will be using the below-shown code in the GNU editor. We have a single header library included and a main method in the code. This time, you will be using two integer-type variables, “x” and “y,” respectively, in the C script’s main function. One of them is the auto type, and the second one is of static type with the values “-75” and “22”. Then we have used two print lines to output the values of both the variables separately in the terminal screen. Then we have to practice an “if” statement to perform a condition. In this condition, we will check if the value of the variable “x” does not equal 0. If the value is not 0, then it will execute another print statement. This print statement will output the total sum of both the variables “x” and “y” in it. After that, the main function will be closed, and you can save and quit the C type file via shortcuts “Ctrl+S” and “Ctrl+X,” respectively.

Let’s compile the updated C code again via the “gcc” query along with the name of a C file “test.c” specified in the query as below.

$ gcc test.c

Let’s begin to run the file “test.c” via the old “a.out” instruction as stated. The output on your terminal screen will show you the exact value of both the variables in the first two lines. After that, it will show the sum of both the variables in the third line.


We hope this article will be easy to understand and helpful for you to understand the idea of static variables in the C language via the Linux operating system.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.