Storage Classes in C

Storage classes in a C Programming Language are classified into four main types according to the storage area and their behavior, in this c programming tutorial we use these storage classes in many programs:-

  1. Automatic storage class
  2. Static storage class
  3. Register
  4. Extern

Automatic storage class:

Automatic storage class variables in C Programming Language will be created and destroyed automatically. In the Automatic storage class, variables are stored in the stack area of the data segment or processor register.
Features:

Storage Memory
Scope Local / Block Scope
Lifetime Exists as long as Control remains in the block

Static storage class:

Static storage class variables in c language will only exist once and throughout the c program, this storage variable will be always active. Static storage class variables are stored in a static area of a data segment.
In C programming, there are four types of scopes available such as

  1. Body scope
  2. Function scope
  3. Program scope
  4. File scope

Static Storage Class:

The value of the static variable lingers until the execution of the whole c program. A static variable can be declared using a static keyword as shown below:

Syntax:

static int i;

Example:

#include <stdio.h>

void Check();   //prototype

void main()

{

Check();   //function calling

Check();  //function calling

Check();   //function calling

getch();

}

void Check(){

static int c=0;

printf(“c=%d\n”,c);

c+=10;

}

Static storage class in C programming

Register storage class:

The keyword ‘register’ in C programming language is used to define a local variable.
Syntax
register int count;

Example

#include<stdio.h>

void main()

{

int num1,num2;

register int sum;

printf(“\nEnter the Number 1 : “);

scanf(“%d”,&num1);

printf(“\nEnter the Number 2 : “);

scanf(“%d”,&num2);

sum = num1 + num2;

printf(“\nSum of Numbers : %d”,sum);

getch();

}

Register Storage class in C programming

Extern: Global variable

Example:

#include<stdio.h>

void f1();

extern int i=10; //global variable

void main()

{

clrscr();

printf(“inside main=%d”,i);

f1();

getch();

}

void f1()

{

printf(“\noutside main method=%d”,i);

}

Extern storage class in c programming