Static storage classes:
- Keyword: static
- Memory: Inside RAM
- Scope: Static variables can be either local or global. If the variable is local, can access within the block. If it is global, we can access throughout the program.
- Default value: 0
#include<stdio.h> static int x=10; void test(); int main() { static int y; printf(“y val : %d \n”, y); test(); return 0; } void test() { printf(“x val : %d \n”, x); } |
Local variables memory will be deleted as soon as function execution completes.
#include<stdio.h> void test(); int main() { test(); test(); test(); test(); return 0; } void test() { auto int x=5; x=x+2; printf(“x val : %d \n”, x); } |
Static variable memory will be deleted only when program execution completes.
#include<stdio.h> void test(); int main() { test(); test(); test(); test(); return 0; } void test(){ static int x=5; x=x+2; printf(“x val : %d \n”, x); } |