Saturday 31 August 2013

Storage Classes

Declaration of a variable in C language:

                          storageclass datatype variable_name;

Where storage class will tell the compiler about the lifetime and accessible area of a variable and more importantly  by default with what value it will initialize by compiler.


Automatic Storage Class:

 They are declared at the start of a block. Memory is allocated automatically upon entry to a block and freed automatically upon exit from the block. 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, i.e. by name. Of course, 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; by default, storage class within a block is auto. Automatic variables declared with initializers are initialized each time the block in which they are declared is entered.
Note: We do not need to take care of memory must b freed that will be freed automatically by compiler.

Scope: 

Local to block or function body in which it is declared

Lifetime: 

As long as program remains control within the block it is declared

Initialized Value:

 A garbage value

Register Storage Class:

Register variables are a special case of automatic variables. Automatic variables are allocated storage in the memory of the computer; however, for most computers, accessing data in memory is considerably slower than processing in the CPU. These computers often have small amounts of storage within the CPU itself where data can be stored and accessed quickly. These storage cells are called registers.
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 request to the compiler that particular automatic variables should be allocated to CPU registers, if possible. 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.

Scope: 

Local to block or function body in which it is declared

Lifetime: 

As long as program remains control within the block it is declared

Initialized Value:

 A garbage value

 Note: As per the memory hierarchy registers are smallest and fastest.

Static Storage Class:

If we want to retain the value of some variable between several function calls then we declare it with static storage class.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 initializer must be constant expressions, and initialization is done only once at compile time when memory is allocated for the static variable.

Scope: 

Local to block or function body in which it is declared

Lifetime: 

Throughout the program variable remains alive. Hence it can retain the value between several function calls

Initialized Value:

 Zero

Note: Static keyword is different in every language in C the basis is if we want to restrict the scope of external variable within the block.

External Storage Class:

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. No storage class specifier is used - the position of the declaration within the file indicates external storage class. Memory for such variables is allocated when the program begins execution, and remains allocated until the program terminates.
If a programmer wants to use the variable in several files then in every file it must declare with extern keyword which tells that the variable is external to this file.

Scope: 

In all functions of a program

Lifetime: 

Throughout the program variable remains alive. Hence it can retain the value between several function calls

Initialized Value:

 Zero

Note: Generally dlls in C built with the help of extern variables because through these variables we can provide linking between several files.

C programs:

Automatic  and Register Variables:


Static Variables:

                                /* File: stat.c*/
                                        main()
                                        {
                                              int i;
                                              for(i=0;;i<10;i++)
                                              {
                                                        sunny();
                                               }

                                               getch();
                                          }
                                           int sunny()
                                           { 
                                                    static int j;
                                                    printf("%d",j);
                                                     j++;
                                            }

External Variables:

 




No comments:

Post a Comment