Storage Classes in C Programming – Types, Scope & Example

0
Storage Classes in C Programming – Types, Scope & Example
Storage Classes in C Programming – Types, Scope & Example
📑 Table of Contents

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

StorageMemory
Default ValueAutomatic default value is garbage.
Value LifeThe control within the function defined.
Value ScopeLocal function in which auto variable is defined with in function scope.

Example of Auto Storage Class in C

#include<stdio.h>
#include<conio.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

StorageMemory
Default ValueStatic default value is 0.
Value LifeStatic variable value between different function call .
Value ScopeLocal function in which static variable is defined with in function scope.

Without Use Static Keyword Example

#include<stdio.h>
#include<conio.h>

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<stdio.h>
#include<conio.h>
void static_data()
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

StorageMemory
Default ValueRegister default value is Garbage.
Value LifeRegister variable control and remain function block .
Value ScopeLocal function in which register variable is defined with in function scope.


Example of Register Storage Class in C

#include<stdio.h>
#include<conio.h>

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

StorageMemory
Default ValueExternal default value is 0.
Value LifeExternal variable control end of program.
Value ScopeGlobal function in which external variable is global scope.

Example of Extern Storage Class in C

#include<stdio.h>
#include<conio.h>

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
Local Value inside function:100
After function call:100

Storage Classes or Scope in C

Storage ClassStorageStart ValueScope ValueValue Life
autostackgarbageWithin BlockEnd Block
externSegment Data0GlobalEnd of Program
staticSegment Data0Within BlockEnd of Program
registerCPU RegisterGarbageWithin BlockEnd Block

Conclusion of Storage Class in C

Conclusion: This is a storage class in C. It can be used scope of program and lifetime defines in storage classes.  I can explains storage classes is very simple and easy method  you can easily understand every example and definition. If any problems within storage classes than you can contact me by email or contact us. I can solution provide easy method. 


Tags

Post a Comment

0 Comments
Post a Comment (0)
To Top