Thursday 6 May 2021

Storage classes in C- All types of storage class variables

All variables in C must have a data types and storage class. A storage class tells to the compiler three things.

  1. Where the variable would be stored.
  2. What is the initial value of the variable if it is not initialized 
  3. What is the scope of the variable.

 


In C there are four types of storage class are available. Each of then are discussed briefly below. The data related to any program is stored either in computer memory or in CPU registers. Since CPU registers access is more faster than memory devices, the variables which are frequently used in your C program like loop control variable are stored in registers.

1. Automatic storage class

  • The keyword "auto" is used for this storage class.
  • All local variables have automatic storage class.
  • It is default class in C.

Storage: Memory

Default value: garbage value

Scope: local scope

Lifetime: Till the program control within the block / function

int main()  
    {  
    int a; //auto  
    char b;  
    float c;   
    printf("%d %c %f",a,b,c); // printing initial default value of automatic variables a, b, and c.   
    return 0;  
    }  

2. External storage class

 The "extern" keyword is used this storage class.

All global variables have external storage class.

An external variable can be declared many times but can be initialized at only once.

Storage: Memory

Default value: zero

Scope: global scope

Lifetime: during entire program execution.

extern int a;
void main()
{
  a=25;
printf(“%d”,a);   // give 25
printf(“%d”,F1()); // gives 25
printf(“%d”,F2());// gives 25
getch();
}
F1()
{
a=a+10;
return a;
}
F2()
{
a=a+5;
return a;
}


3. Static storage class

The keyword "static" is used for this storage class.

All local and global variables can be declared using static keyword.

The variables defined as static specifier can hold their value between the multiple function call.

Storage: Memory

Default value: zero

Scope: local scope / global scope.

Lifetime: Value of the variable remains hold between function calls.

static int a;
void main()
{
  a=25;
printf(“%d”,a);   // gives 25
printf(“%d”,F1()); // gives 35
printf(“%d”,F2());  // gives 40
getch();
}
F1()
{
a=a+10;
return a;
}
F2()
{
a=a+5;
return a;
}

4. Register storage class

The keyword "register" is used for this storage class.

All local variables have register storage class.

The variables which are frequently used in your C program like loop control variables can be declared using register storage class, Since register access is faster compared to memory access.

Storage:CPU registers

Default value: garbage value

Scope: local scope

Lifetime: Till the program control within the block / function

 

int main()  
    {  
    register int a; // variable a is allocated memory in the CPU register. The initial default value of a is 0.   
    printf("%d",a);  
    }  



0 comments :

Post a Comment

Note: only a member of this blog may post a comment.

Machine Learning

More

Advertisement

Java Tutorial

More

UGC NET CS TUTORIAL

MFCS
COA
PL-CG
DBMS
OPERATING SYSTEM
SOFTWARE ENG
DSA
TOC-CD
ARTIFICIAL INT

C Programming

More

Python Tutorial

More

Data Structures

More

computer Organization

More
Top