Static Variables in C
Static Variables in C
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.
Here,
datatype represents the type of variable like int, char, float, etc.
var is the name of variable given by user.
https://www.tutorialspoint.com/cprogramming/c_static_variables.htm 1/6
6/16/24, 12:40 PM Static Variables in C
#include <stdio.h>
int main(){
if(a != 0)
printf("The sum of static variable and auto variable: %d\n",(b+a));
return 0;
}
Output
When you run this code, it will produce the following output −
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>
int counter();
https://www.tutorialspoint.com/cprogramming/c_static_variables.htm 2/6
6/16/24, 12:40 PM Static Variables in C
int main(){
counter();
counter();
counter();
return 0;
}
int counter(){
int x;
printf("Value of x as it enters the function: %d\n", x);
x++;
printf("Incremented value of x: %d\n", x);
}
Output
Change the declaration of "x" to "static int x = 0;" and run the program again −
#include <stdio.h>
int counter();
int main(){
counter();
counter();
counter();
return 0;
https://www.tutorialspoint.com/cprogramming/c_static_variables.htm 3/6
6/16/24, 12:40 PM Static Variables in C
int counter(){
static int 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 −
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>
int main(){
static int x = 5;
myfunction(x);
printf("in main - x:%d\n", x);
https://www.tutorialspoint.com/cprogramming/c_static_variables.htm 4/6
6/16/24, 12:40 PM Static Variables in C
return 0;
}
Output
Incremented value of x: 6
in main - x:5
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>
int myfunction();
static int x = 5;
int main(){
myfunction(x);
printf("Inside the main function, x: %d\n", x);
return 0;
https://www.tutorialspoint.com/cprogramming/c_static_variables.htm 5/6
6/16/24, 12:40 PM Static Variables in C
int myfunction(){
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).
https://www.tutorialspoint.com/cprogramming/c_static_variables.htm 6/6