The feature of a variable defined to have a static storage class are as follows :
A static variable is declared by using keyword static.Syntax : static type var-name; or type static var-name;
Storage : Computer Memory
Scope : Local to the block in which variable is defined
Default Initial Value : Zero
Lifetime : Value of the variable persists between different function calls
Default Initial Value : Zero
Lifetime : Value of the variable persists between different function calls
The static storage class specifier can only be applied to the following names:
- Objects
- Functions
- Class members
- Anonymous unions
You cannot declare any of the following as static:
- Type declarations
- Function declarations within a block
- Function parameters
Example :
#include <stdio.h> int increment(void) { static int i = 0; i++; return i; } int main(void) { int j; for (j = 0; j < 5; j++) { printf("Value of f(): %d\n", increment()); } return 0; }
Output:
Value of increment(): 1 Value of increment(): 2 Value of increment(): 3 Value of increment(): 4 Value of increment(): 5
This is Because i is a static variable, it is not reinitialized to 0 on successive calls to increment().
0 comments:
Post a Comment
Hey this is a test comment