In C programming, a static variable is a variable that retains its value between function calls. It is initialized only once and its value persists across function calls, unlike local variables which are re-initialized with each function call. Static variables are typically used to maintain a value that must be retained across multiple function calls, but that should not be accessible from outside the function.
The basic syntax for declaring a static variable in C is as follows:
static data_type variable_name;
Here, the data_type specifies the data type of the variable, and the variable_name is the name of the variable. The static keyword is used before the data type to indicate that the variable is a static variable.
Here is an example of a static variable being used in a function:
//Static Variable #include<stdio.h> void display(); int main() { display(); display(); display(); } void display() { static int x=1; x++; printf("\nx : %d",x); }To download raw file Click Here
x : 2 x : 3 x : 4
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions