0% found this document useful (0 votes)
127 views6 pages

Storage Class:: Types of Storage Classes

Storage class defines the scope and lifetime of a variable and determines where it is stored in memory or CPU registers. The main storage classes are automatic, register, static, and external. Automatic variables are stored in memory and have block scope, while register variables are stored in CPU registers for faster access but cannot be arrays or structures. Static variables are stored in memory and retain their value between function calls, and external variables are global to a program and stored in memory.

Uploaded by

madhusudhakar41
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
127 views6 pages

Storage Class:: Types of Storage Classes

Storage class defines the scope and lifetime of a variable and determines where it is stored in memory or CPU registers. The main storage classes are automatic, register, static, and external. Automatic variables are stored in memory and have block scope, while register variables are stored in CPU registers for faster access but cannot be arrays or structures. Static variables are stored in memory and retain their value between function calls, and external variables are global to a program and stored in memory.

Uploaded by

madhusudhakar41
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Storage Class :

'Storage' refers to the scope of a variable and memory allocated by compiler to store that variable. Scope of a variable is the boundary within which a variable can be used. Storage class defines the the scope and lifetime of a variable. From the point view of C compiler, a variable name identifies physical location from a computer where variable is stored. There are two memory locations in a computer system where variables are stored as : Memory and CPU Registers. Functions of storage class : To detemine the location of a variable where it is stored ? Set initial value of a variable or if not specified then setting it to default value. Defining scope of a variable. To determine the life of a variable.

Types of Storage Classes :


Storage classes are categorized in 4 (four) types as,

Automatic Storage Class Register Storage Class Static Storage Class External Storage Class

Automatic Storage Class :


Keyword : auto Storage Location : Main memory Initial Value : Garbage Value Life : Control remains in a block where it is defined. Scope : Local to the block in which variable is declared. Syntax :
o o o o o

auto [data_type] [variable_name];

Example :

auto int a;

Program : /* Program to demonstrate automatic storage class. #include <stdio.h>

void main() { auto int i=10; { auto int i=20; printf("\n\t %d",i); } printf("\n\n\t %d",i); }

Register Storage Class :


Keyword : register Storage Location : CPU Register Initial Value : Garbage Life : Local to the block in which variable is declared. Scope : Local to the block. Syntax :
o o o o o

register [data_type] [variable_name];

Example :

register int a;

When the calculations are done in CPU, then the value of variables are transferred from main memory to CPU. Calculations are done and the final result is sent back to main memory. This leads to slowing down of processes. Register variables occur in CPU and value of that register variable is stored in a register within that CPU. Thus, it increases the resultant speed of operations. There is no waste of time, getting variables from memory and sending it to back again. It is not applicable for arrays, structures or pointers. It cannot not used with static or external storage class. Unary and address of (&) cannot be used with these variables as explicitly or implicitly.

Program : /* Program to demonstrate register storage class.

#include <stdio.h> void main() { register int i=10; { register int i=20; printf("\n\t %d",i); } printf("\n\n\t %d",i); }

Static Storage Class :


o o o o o

Keyword : static Storage Location : Main memory Initial Value : Zero and can be initialize once only. Life : depends on function calls and the whole application or program. Scope : Local to the block.

Syntax : static [data_type] [variable_name];

Example : static int a;

There are two types of static variables as : a) Local Static Variable b) Global Static Variable Static storage class can be used only if we want the value of a variable to persist between different function calls.

Program : /* Program to demonstrate static storage class.

#include <stdio.h> void main() { int i; for (i=0; i<3; i++) incre(); }

void incre() { int avar=1; static int svar=1; avar++;

svar++; printf("\n\n Automatic variable value : %d",avar); printf("\t Static variable value : %d",svar); }

Output :

Automatic variable value : 2

Static variable value : 2

Automatic variable value : 2

Static variable value : 3

Automatic variable value : 2

Static variable value : 4_

External Storage Class :


Keyword : extern Storage Location : Main memory Initial Value : Zero Life : Until the program ends. Scope : Global to the program. Syntax :
o o o o o

extern [data_type] [variable_name];

Example :

extern int a;

The variable access time is very fast as compared to other storage classes. But few registers are available for user programs.

The variables of this class can be referred to as 'global or external variables.' They are declared outside the functions and can be invoked at anywhere in a program.

You might also like