C – Storage Class Specifiers - C-Tutorial

Latest

Monday, 10 October 2016

C – Storage Class Specifiers

C – Storage Class Specifiers


Ø  A storage class is used to declare variables. To fully defined a variable one needs to mention not only it's data type, But also it's storage class.    
Ø  Storage class specifiers in C language tells the compiler where to store a variable, what is the initial value of the variable, What is the scope of the variable and life time of the variable.

Storage class tells the compiler about the following information

1. Storage area: It specifies the location where the variable is stored.  i.e, Either in CPU memory unit (or) Register memory.

2. Default / Initial value:  If we are not going to provide value to a variable then system automatically provide values to the variable. There are two types of default/initial values. Those are
            1. Garbage value (Unpredictable value)
            2. Zero

3. Scope:  It specifies the scope of the variable. i.e, In which function the value of the variable would be available.

4. Life time of a variable:  It specifies the life of the variable. i.e, how long a variable exits

Types of Storage Class Specifiers in C:

     There are 4 storage class specifiers available in C language. They are,
1)      Automatic Storage class
2)      External Storage class
3)      Static Storage class
4)      Register Storage class

Syntax:
                      storage_specifier   data_type   variable _name;

example:                   static  int  x;

                      Here 'x' is the static storage class variable


1)      Automatic Storage class;
Ø    Automatic variables are declared with in a function with the keyword called "auto"

Ø          They are local to the function in which they are declared

Ø           Automatic variables are created when they function is called and destroyed automatically    when the function is completed

Ø          A variable inside a function without any storage class specification is by default an auto variable

#include<stdio.h>
void increment();
int main()
{
increment();
increment();
increment();
}
void increment()
{
auto int i = 0 ;
printf ( “%d “, i ) ;
i++;
}

2) Static Storage class:

Ø        static variables are declared outside of the function with the keyword called "static"

Ø         static variable holds the latest value of the variable in the function in which they are declared.

Ø        There are two types of static variables are available, Those are
            1. Local static variable [ variables declare inside of the function]
            2. Global static variable [ variables declare outside of the function ]

Ø          It specifies the following information to the compiler
            Storage area: CPU memory
            Default value:            Zero
            Scope:             1. Scope of local static variable is local to the function  in which                                                      the variable is declared.
                                    2. Scope of the global static variable is throughout the program.
            life time:         Through out the program. Variable definition might be any where                                       in the program.

  Static variables retain the value of the variable between different function calls.
//C static example
#include<stdio.h>
void increment();
int main()
{
increment();
increment();
increment();
}
void increment()
{
static int i = 0 ;
printf ( “%d “, i ) ;
i++;
}

3) Extern Storage Class:

Ø          extern variables are declared outside of the function with the keyword called "extern"

Ø          extern variables are global to all functions within the program

Ø          The variables declared outside a function without any storage class is by default  external variables. But when ever, we use the extern keyword then the variables must be initialized.

Ø           It specifies the following information to the compiler
            Storage area:    CPU memory
            Default value:   Zero
            Scope:             Global all functions with in a program
            life time:         Through out the program. Variable definition might be any where                                                   in the program.


#include<stdio.h>
extern int x = 10 ;
 main( )
{
clrscr();
printf(“The value of x is %d \n”,x);

getch();
}


4) Register Storage class:

Ø         Register variables are declared with in a function with the keyword called "register"

Ø         Register variables are local to the function in which they are declared

Ø         Register variables are created when they function is called and destroyed automatically when the function is completed

Ø          Syntax for register storage variable is as follows
   register data_type  variable_name;
   ex:       register int x;
            register float y;

Ø         It specifies the following information to the compiler
            Storage area:    Register memory
            Default value:   Garbage value
            Scope:             Local to the function which they are declared
            life time:         With in the function only

Ø          Register variables are also local variables, but stored in register memory. Whereas, auto variables are stored in main CPU memory.

Ø   Register variables will be accessed very faster than the normal variables since they are stored in register memory rather than main memory.

Ø          But, only limited variables can be used as register since register size is very low. (16 bits, 32 bits or 64 bits)

#include <stdio.h>
int main()
{
register int i;
int arr[5];
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
arr[4] = 50;
for (i=0;i<5;i++)
{
printf(“value of arr[%d] is %d \n”, i, arr[i]);
}
return 0;
}


S. No.
Storage Specifiers
Storage place
Initial / default value
Scope
Life time
1
auto
CPU Memory
Garbage value
local
Within the function only.
2
extern
CPU memory
Zero
Global
Till the end of the main program.
Variable definition might be anywhere in the C program
3
static
CPU memory
Zero
Local/
Global
Retains the value of the variable
between different function calls.
4
register
Register memory
Garbage value
local
Within the function

No comments:

Post a Comment