Posted On: Feb 22, 2018
C defines another class of variables called static variables. Static variables are of two types:
#include <stdio.h> void PrintCount() { static int Count = 1; //Count is initialized only on the first call printf( "Count = %d\n", Count); Count = Count + 1; //The incremented value of Count is retained } int main() { PrintCount(); PrintCount(); PrintCount(); return 0; } OutPut: Count = 1 Count = 2 Count = 3
The output of the program is a sequence of numbers starting with 1, rather than a string of 1′s. The initialization of static variable Countis performed only at the first instance of the function call. In successive calls to the function, the variable count retains its previous value. However, these static variables are not accessible from other parts of the program.
Never Miss an Articles from us.
The scope resolution operator (::) is useful in C programming when both global and local variables have the same name,..
C language allows the use of the prefix register in primitive variable declarations. Such variables are called register..
A structure is a composite data type declaration that defines a physically grouped list of variables to be placed under..