What is Storage Class in C
Storage Class in C: A storage classes in defines the scope of variable, Variable lifetime, default value and storage space in memory of a variable. It is tell us compiler where the variable is stored in memory. It also decides the scope of the variable where it can be used. Storage class controls the lifetime of a variable. Storage class also sets the default initial value of a variable. C language provides different storage classes are auto, register, static, and extern.
Types of Storage Classes in C Programming
- Automatic Storage Class.
- Static Storage Class.
- Register Storage Class.
- External Storage Class.
Automatic Storage Classes in C
Automatic Storage Class: A Automatic variables are declared inside a function in which they have to used. When the function is called automatic variables are created and destroyed when function is exited. Automatic variables can not be used outside that function in which it is declared it means we can say that it is private member. Automatic variables are also known as local variable. Automatic storage class can be used to auto keyword.
Scope of Automatic Storage Class
| Storage | Memory |
| Default Value | Automatic default value is garbage. |
| Value Life | The control within the function defined. |
| Value Scope | Local function in which auto variable is defined with in function scope. |
Example of Auto Storage Class in C
| #include<stdio.h> void main(){ clrscr(); auto int a=200; { auto int a=300; { auto int a=100; printf("Auto Storage Class:%d\n",a); } printf("Auto Storage Class:%d\n",a); } printf("Auto Storage Class:%d\n",a); getch(); } ============OUTPUT============ Auto Storage Class:100 Auto Storage Class:300 Auto Storage Class:200 |
Static Storage Class in C
Static Storage Class: A Static variables can be used anywhere in the program inside or outside of a function or block. The value of the static variable exists until the end of program. The static variable which is declared inside a function is called internal static variable and and it can not be used outside that function.
The static variable which is declared outside a function is called external static variable and and it can be used in all the function of that program.
Scope of Static Storage Class in C
| Storage | Memory |
| Default Value | Static default value is 0. |
| Value Life | Static variable value between different function call . |
| Value Scope | Local function in which static variable is defined with in function scope. |
Without Use Static Keyword Example
void static_data(){ int a=0; printf("Static Value is:%d\n",a); a++; } void main(){ clrscr(); static_data(); static_data(); static_data(); static_data(); getch(); } ============OUTPUT============ Static Value is:0 Static Value is:0 Static Value is:0 Static Value is:0 |
Example of Static Storage Class in C
#include<conio.h> static int a=0; printf("Static Value is:%d\n",a); a++; } void main(){ clrscr(); static_data(); static_data(); static_data(); static_data(); getch(); } ============OUTPUT============ Enter Integer Value:56 Integer Value is: 56 |
Register Storage Class in C Definition
Register Storage Class: A register variables is stored in one of the register of system instead of memory. Value stored in register can be accessed faster than one that is stored in memory.
Lifetime of Register Storage Class
| Storage | Memory |
| Default Value | Register default value is Garbage. |
| Value Life | Register variable control and remain function block . |
| Value Scope | Local function in which register variable is defined with in function scope. |
Example of Register Storage Class in C
void main(){ clrscr(); register int i; for(i=1;i<=5;i++){ printf("Register Variable:%d\n",i); } getch(); } ============OUTPUT============ Register Variable:1 Register Variable:2 Register Variable:3 Register Variable:4 Register Variable:5 |
External Storage Class in C
External Storage Class: A external variable that can be used anywhere in the program is called external variable.
External storage class does not create a variable but it informs the compiler of its existence. External storage used to extern keyword.
Lifetime of Extern Storage Class
| Storage | Memory |
| Default Value | External default value is 0. |
| Value Life | External variable control end of program. |
| Value Scope | Global function in which external variable is global scope. |
Example of Extern Storage Class in C
int a=200; void external_data(){ extern int a; a=100; printf("Local Value inside function:%d\n",a); } void main(){ clrscr(); printf("Before function call:%d\n",a); external_data(); printf("After function call:%d\n",a); getch(); } ============OUTPUT============ Before function call:200 After function call:100 |
Storage Classes or Scope in C
| Storage Class | Storage | Start Value | Scope Value | Value Life |
|---|---|---|---|---|
| auto | stack | garbage | Within Block | End Block |
| extern | Segment Data | 0 | Global | End of Program |
| static | Segment Data | 0 | Within Block | End of Program |
| register | CPU Register | Garbage | Within Block | End Block |

