English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
When using the static keyword, you cannot modify the variable or data member or function again. It is allocated during the lifetime of the program. Static functions can be called directly using the class name.
Static variables are initialized only once. The compiler retains the variable until the end of the program. Static variables can be defined inside or outside the function. They are local. The default value of static variables is zero. Static variables are valid before the program execution.
This is the syntax of static variables in C language,
static datatype variable_name;
Here,
datatype-The data type of the variable, such as int, char, float, etc.
variable_name-This is the variable name given by the user.
Value-to initialize any value of the variable. By default, it is zero.
This is an example of a static variable in C language,
#include <stdio.h> int main() { static int a; int b; printf("Default value of static variable : %d\n", a); printf("Default value of non--static variable : %d\n", b); return 0; }
Output Result
Default value of static variable : 0 Default value of non--static variable : 0
In the above program, two variables are declared, one is a static variable and the other is a non-static variable. The default values of the two variables are displayed as follows-
static int a; int b; printf("Default value of static variable : %d\n", a); printf("Default value of non--static variable : %d\n", b);