***Welcome to ashrafedu.blogspot.com * * * This website is maintained by ASHRAF***

Posts

Wednesday, November 18, 2020

Storage Classes in C

A storage class represents the visibility and a location of a variable. It tells from what part of code a variable can be accessed.

A storage class is used to describe the following things:

  • The variable scope.
  • The location where the variable will be stored.
  • The initialized value of a variable.
  • A lifetime of a variable.

A storage class is used to represent the information about a variable.

There are total four types of standard storage classes. The table below represents the storage classes in 'C'.

Storage class

Purpose

auto

It is a default storage class.

extern

It is a global variable.

static

It is a local variable which is capable of returning a value even when control is transferred to the function call.

register

It is a variable which is stored inside a Register.

1. Auto storage class

 The variables defined using auto storage class are called as local variables. Auto stands for automatic storage class. A variable is in auto storage class by default if it is not explicitly specified.

The scope of an auto variable is limited with the particular block only. Once the control goes out of the block, the access is destroyed.

A keyword auto is used to define an auto storage class. By default, an auto variable contains a garbage value.

Example, auto int age;

2. Extern storage class

Extern stands for external storage class. Extern storage class is used when there are global functions or variables which are shared between two or more files.

Keyword extern is used to declaring a global variable or function in another file to provide the reference of variable or function which have been already defined in the original file.

The variables defined using an extern keyword are called as global variables. These variables are accessible throughout the program.

Example, extern void display();

Ex:

First File: main.c

#include <stdio.h>

extern i;

main() {

   printf("value of the external integer is = %d\n", i);

   return 0;}

Second File: original.c

#include <stdio.h>

i=48;

3. Static storage class

The static variables are used within function/ file as local static variables. They can also be used as a global variable


  • Static local variable is a local variable that retains and stores its value between function calls or block and remains visible only to the function or block in which it is defined.

 

  • Static global variables are global variables visible only to the file in which it is declared. 

Global variables are accessible throughout the file whereas static variables are accessible only to the particular part of a code.

The lifespan of a static variable is in the entire program code. A variable which is declared or initialized using static keyword always contains zero as a default value.

Example: static int count = 10;

#include <stdio.h> /* function declaration */

void next(void);

static int counter = 7; /* global variable */

main() {

 while(counter<10) {

      next();

      counter++;   }

return 0;}

void next( void ) {    /* function definition */

   static int iteration = 13; /* local static variable */

   iteration ++;

   printf("iteration=%d and counter= %d\n", iteration, counter);}

4. Register storage class

Register storage class is used when  local variables are to be stored within functions or blocks in CPU registers instead of RAM to have quick access to these variables.

The keyword register is used to declare a register storage class. The variables declared using register storage class has lifespan throughout the program.

It is similar to the auto storage class. The variable is limited to the particular block. The only difference is that the variables declared using register storage class are stored inside CPU registers instead of a memory. Register has faster access than that of the main memory.

Example: register int age;

Principal features of each storage class which are commonly used in C programming are :


Storage Class

Declaration

Storage

Default Initial Value

Scope

Lifetime

auto

Inside a function/block

Memory

Unpredictable

Within the function/block

Within the function/block

register

Inside a function/block

CPU Registers

Garbage

Within the function/block

Within the function/block

extern

Outside all functions

Memory

Zero

Entire the file and other files where the variable is declared as extern

program runtime

Static (local)

Inside a function/block

Memory

Zero

Within the function/block

program runtime

Static (global)

Outside all functions

Memory

Zero

Global

program runtime


No comments:

Post a Comment

Top