Static Variables in C

By default, a C variable is classified as an auto storage type. A static variable is useful when you want to preserve a certain value between calls to different functions. Static variables are also used to store data that should be shared between multiple functions.

Static Variables

The static variables belong to the static storage class, they are initialized only once and preserve the values till the end of the program, The static keyword is used to declare the static variables.

Features of Static Variables

The following are some of the features of static variables in C programming language −

  • The compiler allocates space to a static variable in the computer’s main memory.
  • Unlike auto, a static variable is initialized to zero and not garbage.
  • A static variable is not re-initialized on every function call, if it is declared inside a function.
  • A static variable has local scope.

Declare a Static Variable

To declare a static variable in C language, use the static keyword and assign the initial value. Following is the syntax to declare a static variable:

static datatype var = value;

Here,

  • datatype represents the type of variable like int, char, float, etc.
  • var is the name of variable given by user.
  • value is any value given to initialize the variable. By default, it is zero.

Examples of Static Variables in C

Example: Using Static Variable

Here is an example of how you can use a static variable in C language −

#include <stdio.h>intmain(){autoint a =-28;staticint b =8;printf("The value of auto variable: %d\n", a);printf("The value of static variable b: %d\n",b);if(a !=0)printf("The sum of static variable and auto variable: %d\n",(b+a));return0;}

Output

When you run this code, it will produce the following output −

The value of auto variable: -28
The value of static variable b: 8
The sum of static variable and auto variable: -20

Example: Create Counter Function W/O Using Static Variable

In this example, x is an auto variable by default and initialized to 0 every time when the counter() function is called. On each subsequent call, it gets re-initialized.

#include <stdio.h>intcounter();intmain(){counter();counter();counter();return0;}intcounter(){int x;printf("Value of x as it enters the function: %d\n", x);
   x++;printf("Incremented value of x: %d\n", x);}

Output

Run the code and check its output −

Value of x as it enters the function: 0
Incremented value of x: 1
Value of x as it enters the function: 0
Incremented value of x: 1
Value of x as it enters the function: 0
Incremented value of x: 1

However, when the variable x in the counter() function is declared as static, it is initialized to “0” when the counter() function is called for the first time. On each subsequent call, it is not re-initialized. Instead, it retains the earlier value.

Example: Create Counter Using Static Variable

Change the declaration of “x” to “static int x = 0;” and run the program again −

#include <stdio.h>intcounter();intmain(){counter();counter();counter();return0;}intcounter(){staticint x =0;printf("Value of x as it enters the function: %d\n", x);
   x++;printf("Incremented value of x: %d\n", x);}

Output

Now, when you run this code, it will produce the following output −

Value of x as it enters the function: 0
Incremented value of x: 1
Value of x as it enters the function: 1
Incremented value of x: 2
Value of x as it enters the function: 2
Incremented value of x: 3

Passing Static Variable to Function

You can pass a static variable to a function. However, a formal parameter cannot be declared as static, as C uses the function parameters as local auto variables inside a function.

Example

In this code, we pass a static variable to a function. However, the change in its value is not reflected in the calling function.

#include <stdio.h>intmyfunction(int x);intmain(){staticint x =5;myfunction(x);printf("in main - x:%d\n", x);return0;}intmyfunction(int x){
   x++;printf("Incremented value of x: %d\n", x);}

Output

Run the coce and check its output −

Incremented value of x: 6
in main - x:5

Similarities Between Static and Global Variables

A static variable has certain similarities with a global variable. Both of them, if not explicitly initialized, both are initialized to “0” (for numeric types) or “null pointers” (for pointers).

The scope of a static variable is restricted to the function or the block in which it is declared. This is unlike a global variable, which is accessible throughout the program. Also, a static variable can be imported in another code file, as we do by using the extern keyword.

Example

You can declare a global variable as static too. Take a look at the following example −

#include <stdio.h>intmyfunction();staticint x =5;intmain(){myfunction(x);printf("Inside the main function, x: %d\n", x);return0;}intmyfunction(){
   x++;printf("Incremented value of x: %d\n", x);}

Output

When you run this code, it will produce the following output −

Incremented value of x: 6
Inside the main function, x: 6

It is better to use static variables to be accessible only within a file. On the other hand, use global (with extern) variables to be accessible from anywhere in a program (if declared extern in other files).


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *