Thursday 10 October 2013

STORAGE CLASSES IN C PROGRAMMING -STATIC-AUTOMATIC-EXTERNAL- & REGISTER

STORAGE CLASSES

INTRODUCTION

§  The storage class determines the part of memory where storage is allocated for an object (particularly variables and functions) and how long the storage allocation continues to exist.
§  A scope specifies the part of the program which a variable name is visible, that is the accessibility of the variable by its name.  In C program, there are four storage classes: automatic, register, external, and static.
§  Keep in mind that in the hardware terms we have primary storage such as registers, cache, memory (Random Access Memory) and secondary storage such as magnetic and optical disk.

AUTOMATIC VARIABLE - auto

§  They are declared at the start of a program’s block such as in the curly braces ( { } ). 
§  The variable declared as auto is stored in the memory.
§  Default value of that variable is garbage value.
§  Variable is alive till the control remains within the block in which the variable id defined.
The scope of automatic variables is local to the block in which they are declared, including any blocks nested within that block. For these reasons, they are also called local variables.
§  No block outside the defining block may have direct access to automatic variables (by variable name) but, they may be accessed indirectly by other blocks and/or functions using pointers.
§  Automatic variables may be specified upon declaration to be of storage class auto.  However, it is not required to use the keyword auto because by default, storage class within a block is auto.
§  Automatic variables declared with initializers are initialized every time the block in which they are declared is entered or accessed.
§  Example:
#include<stdio.h>
#include<conio.h>
void main(){
          auto int a;
          printf(“%d”,a)
}
Output:
1285
As seen above, the output is garbage value.
REGISTER VARIABLE - register
  • Automatic variables are allocated storage in the main memory of the computer; however, for most computers, accessing data in memory is considerably slower than processing directly in the CPU.
  • Registers are memory located within the CPU itself where data can be stored and accessed quickly.  Normally, the compiler determines what data is to be stored in the registers of the CPU at what times.
  • However, the C language provides the storage class register so that the programmer can suggest to the compiler that particular automatic variables should be allocated to CPU registers, if possible and it is not an obligation for the CPU to do this.
  • Thus, register variables provide a certain control over efficiency of program execution.
  • Variables which are used repeatedly or whose access times are critical may be declared to be of storage class register.
  • Variables can be declared as a register as follows:
register  int  var;
Default value of that variable is garbage value. Scope of that variable is local to the block in which the variable is defined. Variable is alive till the control remains within the block in which the variable id defined.It is not necessary that variable declared as register would be stored in CPU registers. The number of CPU registers is limited. If the CPU register is busy doing some other task then variable might act as automatic variable.
Example:
#include<stdio.h>
#include<conio.h>
Void main(){
          register int a;
          printf(“%d”,a)
}
Output:
4587
As seen above, the output is garbage value.

STATIC STORAGE CLASS:
The keyword used for Static storage class is 'static'.The variable declared as static is stored in the memory. Default value of that variable is zero.Scope of that variable is local to the block in which the variable is defined. Life of variable persists between different function calls.
  • Static storage class can be specified for automatic as well as external variables such as:
static extern varx;
  • Static automatic variables continue to exist even after the block in which they are defined terminates. Thus, the value of a static variable in a function is retained between repeated function calls to the same function.
  • The scope of static automatic variables is identical to that of automatic variables, i.e. it is local to the block in which it is defined; however, the storage allocated becomes permanent for the duration of the program.
  • Static variables may be initialized in their declarations; however, the initializers must be constant expressions, and initialization is done only once at compile time when memory is allocated for the static variable.
/* static storage class program example */
#include <stdio.h>
#define MAXNUM 3

void sum_up(void);

int main()
{
    int count;
    
    printf("\n*****static storage*****\n");
    printf("Key in 3 numbers to be summed ");
    for(count = 0; count < MAXNUM; count++)
        sum_up();
    printf("\n*****COMPLETED*****\n");
    return 0;
}

void sum_up(void)
{
    /* at compile time, sum is initialized to 0 */
    static int sum = 0;
    int num;
   
    printf("\nEnter a number: ");
    scanf("%d", &num);
    sum += num;
    printf("\nThe current total is: %d\n", sum);
}

Output:


§  While the static variable, sum, would be automatically initialized to zero, it is better to do so explicitly.
§  In any case, the initialization is performed only once at the time of memory allocation by the compiler.  The variable sum retains its value during program execution.
§  Each time the sum_up() function is called, sum is incremented by the next integer read.  To see the different you can remove the static keyword, re-compile and re-run the program.

EXTERNAL VARIABLE - extern

§  All variables we have seen so far have had limited scope (the block in which they are declared) and limited lifetimes (as for automatic variables).
§  However, in some applications it may be useful to have data which is accessible from within any block and/or which remains in existence for the entire execution of the program.  Such variables are called global variables, and the C language provides storage classes which can meet these requirements; namely, the external (extern) and static (static) classes.
§  Default value of that variable is zero.
§  Declaration for external variable is as follows:
extern int var;
§  External variables may be declared outside any function block in a source code file the same way any other variable is declared; by specifying its type and name (extern keyword may be omitted).
Typically if declared and defined at the beginning of a source file, the extern keyword can be omitted.  If the program is in several source files, and a variable is defined in let say file1.c and used in file2.c and file3.c then the extern keyword must be used in file2.c and file3.c(In case of large program, containing more than one file, if the global variable is declared in file 1 and that variable is used in file 2 then, compiler will show error. To solve this problem, keyword extern is used in file 2 to indicate that, the variable specified is global variable and declared in another file.
§  But, usual practice is to collect extern declarations of variables and functions in a separate header file (.h file) then included by using #include directive.
§  Memory for such variables is allocated when the program begins execution, and remains allocated until the program terminates.  For most C implementations, every byte of memory allocated for an external variable is initialized to zero.
§  The scope of external variables is global, i.e. the entire source code in the file following the declarations. All functions following the declaration may access the external variable by using its name.  However, if a local variable having the same name is declared within a function, references to the name will access the local variable cell.

/*program for global variable*/

#include<stdio.h>
int i=50;
void show();
int main()
{
  int i=100;
  printf("\n%d",i);
  show();
  return 0;
}
void show()
{
  printf("\n%d",i);
}

OUTPUT:-

100
50


/*Another example of external or global variable*/

#include<stdio.h>
int i=5;
int main()
{
  extern int j;
  printf("\ni=%d \nj=%d",i,j);
  int j=10;
  return 0;
}

OUTPUT:-

i=5
j=10


Explanation: Because both variables i and j are enjoy as global variable.


Features
Automatic Storage Class
Register Storage Class
Static Storage Class
External Storage Class
Keyword
auto
register
static
extern
Initial Value
Garbage
Garbage
Zero
Zero
Storage
Memory
CPU register
Memory
Memory
Scope
scope limited,local to block
scope limited,local to block
scope limited,local to block
Global
Life
limited life of block,where defined
limited life of block,where defined
value of variable persist between different function calls
Global,till the program execution
Memory location
Stack
Register memory
Segment
Segment
Example

void main()
{
  auto int i;
  printf("%d",i);
}
OUTPUT
124
void main()
{
  register int i;
  for(i=1; i<=5 ; i++);
    printf("%d ",i);
}
OUTPUT
1 2 3 4 5

void add();
void main()
{
  add();
  add();
}
void add()
{
  static int i=1;
  printf("\n%d",i);
  i=i+1;
}
OUTPUT
1
2

void main()
{
  extern int i;
  printf("%d",i);
  int i=5
}
OUTPUT
5

No comments:

Post a Comment