Storage Class In C - Programming Languages

Latest

Thursday 26 January 2017

Storage Class In C

Storage Class In C

storage classes.
Storage classes include following categories:
1. auto
2. extern
3. register
4. static.
auto
The auto keyword is used to place the specified variable into the stack area of memory. In most variable declaration,this is usually implicit one, e.g. int i; 
Automatic ( Auto ) storage class in C Programming

  1. This is default storage class
  2. All variables declared are of type Auto by default 
  3. In order to Explicit declaration of variable use ‘auto’ keyword
auto int num1 ;   // Explicit Declaration  
extern
The extern keyword is used to specify variable access the variable of the same name from some other file. In modular programs,this is very useful for sharing variables.
Features :
 StorageMemory
 ScopeLocal / Block Scope
 Life timeExists as long as Control remains in the block
 Default initial ValueGarbage
 
External ( extern ) storage class in C Programming

  1. Variables of this storage class are “Global variables”
  2. Global Variables are declared outside the function and are accessible to all functions in the program
  3. Generally , External variables are declared again in the function using keyword extern
  4. In order to Explicit declaration of variable use ‘extern’ keyword
extern int num1 ;   // Explicit Declaration  
 
Features :
 StorageMemory
 ScopeGlobal / File Scope
 Life time
  • Exists as long as variable is running
  • Retains value within the function
 Default initial ValueZero
register
The register keyword gives suggest to the compiler to place the particular variable in the fast register memory located directly on the CPU. Most compilers these days (like gcc) are so smart that suggesting registers could actually make your program more slower. 
1.register keyword is used to define local variable. 
2.Local variable are stored in register instead of RAM. 
3.As variable is stored in register, the Maximum size of variable = Maximum Size of Register 
4.unary operator [&] is not associated with it because Value is not stored in RAM instead it is stored in Register. 
5.This is generally used for faster access. 
6.Common use is “Counter

Syntax

{
register int count;
}
static
It is also used to declare the variables to be private to a certain file only when declared with global variables. static can also be used with functions, making those functions visible only to file itself.The static keyword is used for extending the lifetime of a particular variable. The variable remains even after the function call is long gone (the variable is placed in the alterable area of memory),if you declare a static variable inside a function,. The static keyword can be overloaded.
A static variable is declared by using keyword static. For example;
static int i;
The value of a static variable persists until the end of the program.

Example #2: Static Variable

#include <stdio.h>
void display();

int main()
{
    display();
    display();
}
void display()
{
    static int c = 0;
    printf("%d  ",c);
    c += 5;
}
Output
0  5
During the first function call, the value of c is equal to 0. Then, it's value is increased by 5.
During the second function call, variable c is not initialized to 0 again. It's because c is a static variable. So, 5 is displayed on the screen.
 
 

No comments:

Post a Comment