Professional Documents
Culture Documents
com
C Storage Class
4-5 minutes
In this tutorial, you will learn about scope and lifetime of local and
global variables. Also, you will learn about static and register
variables.
1. automatic
2. external
3. static
4. register
Local Variable
#include <stdio.h>
int main(void) {
When you run the above program, you will get an error
undeclared identifier i. It's because i is declared inside the
for loop block. Outside of the block, it's undeclared.
int main() {
int n1; // n1 is a local variable to main()
}
void func() {
int n2; // n2 is a local variable to func()
}
Global Variable
#include <stdio.h>
void display();
int main()
{
++n;
display();
return 0;
}
void display()
{
++n;
printf("n = %d", n);
}
Output
n = 7
Register Variable
Static Variable
static int i;
The value of a static variable persists until the end of the program.
#include <stdio.h>
void display();
int main()
{
display();
display();
}
void display()
{
static int c = 1;
c += 5;
printf("%d ",c);
}
Output
6 11