What are the different storage classes in C language? Interpret them with programs.
A storage class is defined as the scope and life cycle of a variable or function that exists in a C program.
The storage classes in C language are as follows:
The scope of local variables is limited to the block in which they are declared.
These variables are declared inside the block.
Demonstration
#include<stdio.h> void main (){ auto int i=1;{ auto int i=2;{ auto int i=3; printf ("%d",i); } printf("%d", i); } printf("%d", i); }
3 2 1
These variables are declared outside the block so they are also Global variables are called
#Scope - The scope of a global variable is available throughout the program.
Live Demonstration
#include<stdio.h> extern int i =1; /* this ‘i’ is available throughout program */ main (){ int i = 3; /* this ‘i' available only in main */ printf ("%d", i); fun (); } fun (){ printf ("%d", i); }
31
Live demonstration
#include<stdio.h> main (){ inc (); inc (); inc (); } inc (){ static int i =1; printf ("%d", i); i++; }
1 2 3
The value of the register variable is stored in the CPU register rather than in memory , normal variables are stored in memory.
Register is a temporary storage unit in the CPU.
Demonstration
#include<stdio.h> main (){ register int i; for (i=1; i< =5; i++) printf ("%d",i); }
1 2 3 4 5
The above is the detailed content of Different storage classes in C language. For more information, please follow other related articles on the PHP Chinese website!