The ones who are crazy enough to think they can change the world are the ones who do.
- Steve Jobs

C Storage Classes

Why Storage Class

A storage class defines the scope, visibility and life-time of variables.

Types of storge classes

There are four types of storage classes in c programming. They are

Keyword Storage Classes
auto automatic storage class
static static storage class
register register storage class
extern external storage class
C Functions

Storage class Defines Scope

We already defined that a scope of a variable is defined by its storage classes.

What is meant by scope?

A scope is defined as the area in which the declared variable is available.In c programing we can encounter five different types of scopes. They are

  • program scope
  • file scope
  • function scope
  • block scope
  • prototype scope

Program To Define Scope In Storage Classes

Let's workout a program to explain all 5 different scope in storage class.

C program - Storage Class

storageclass.c
#include <stdio.h>
void display(int num);
// here 'num' has prototype scope
static void fun();
int main()
{
int num=2;
void display(int d){
// here 'display' has program scope
printf("\n%d", d);
}
display(1);
fun();
bark:{
//here 'bark' has function scope
int i = 0;
printf("\nBow Bow");
if(i = 1){
return 0;
}
i++;
}
goto bark;
return 0;
}
static void fun(){
//here 'fun' has file scope
int i = 0;
//here 'i' has block scope
printf("\n%d", i);
i++;
}
  • 1
  • 0
  • Bow Bow

Note:

Program scope

display has a program scope because all non-static function have program scope.

File Scope

fun has a file scope because all static function can only accessed within a file.

Function Scope

bark is a label which has a function scope because there can be only one bark label within a function hence it has function scope.

Block Scope

i has a block scope because every function can have a variable with name i

Prototype Scope

num has a prototype scope because one cannot declare a variable j within the same function.

Storage class Defines Visibility

We already defined that visibility of a variable is defined by its storage class.

What is meant by Visibility?

Visibility is the way of hidding or showing a variable inside or outside a scope. Visibility is otherwise said to be accessibility.

Program To Define Visibility In Storage Classes

Let's workout a simple program to explain visibility of a variable.

storageclassvisibile.c
#include <stdio.h>
int main()
{
int i = 9;
//visible inside all function.
void display(){
//visible inside this function only.
int j = 10;
printf("%d",i);
}
display();
printf("%d",j);
return 0;
}
  • Error message will occur as a variable j is accessible only within that function.

Storage class Defines Life-time

We already defined that life-time of a variable is defined by its storage class.

What is meant by life-time?

Life-time is defined as a period of time in which the variable lives. There are three types of life-times in storage classes. They are

  • static life-time
  • automatic life-time
  • dynamic life-time

Program To Define Life-time In Storage Classes

Let's workout a simple program to explain all 3 life-time in storage class.

storageclasslifetime.c
#include <stdio.h>
int main()
{
int *numbers = (int*)malloc(4* sizeof(int));
// *number act as a automatic and dynamic as well
static int i;
// i act as a static variable
numbers[0] = 0;
numbers[1] = 1;
numbers[2] = 2;
numbers[3] = 3;
free(numbers);
printf("\nStored integers are ");
for(i = 0; i < 4; i++)
printf("\numbers[%d] = %d ", i, numbers[i]);
return 0;
}
  • Stored integers are
  • numbers[0] = 0
  • numbers[1] = 0
  • numbers[2] = 0
  • numbers[3] = 0

Note:

Static life-time

Here i has static life-time because its life-time is within a program.

Automatic life-time

*numbers has automatic life-time because its life-time starts when *number variable is allocated by malloc() inbuilt memory function.

Dynamic life-time

*numbers has dynamic life-time too because free() memory function can be used any time to clear data in allocated memory.

Report Us

We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.

Report