Tuesday 1 November 2011

storage classes


In C language each variable has a Storage class specifiers inform the compiler how to store the variable;
Storage Class which decides scope, visibility and lifetime of that variable.
1.    Automatic variables
2.    External variables
3.    Static variables
4.    Register variables


auto:
auto means Automatic Variable.
A variable which is declared in side the function is called automatic variable or local variable. Compiler assigns garbage value by default.
Scope of the variable is within the function. It can not be accessed out side of the function 

void main()
{
 auto int a;
 //or  int a; //both are same
}

External or Global variableA variable that is declared outside any function is a Global variable. Global variables remain available throughout the entire program. One important thing to remember about global variable is that their values can be changed by any function in the program.
int number;
void main()
{
 number=10;
}
fun1()
{
 number=20;
}
fun2()
{
 number=30;
}
Here the global variable number is available to all three functions.

extern:

The extern keyword is used before a variable to inform the compiler that this variable is declared somewhere else. The extern declaration does not allocate storage for variables.


//extern keyword
#include<stdio.h>
#include<conio.h>
extern int b=12;
void main()
{
 int b=4;//
 // extern int c;
 void ex();
 clrscr();
 printf("%d\n",b);
 ex();
 getch();
}
void ex()
{
 int b=50;
 int c;
 printf("%d",c);
 printf("%d\n",b);
}


static:

Static is initialized only once and remains into existence till the end of program. A static variable can either be internal or external depending upon the place of declaration. Scope of internal static variable remains inside the function in which it is defined. External static variables remain restricted to scope of file in each they are declared.
Example
main()
{
void ex();   //Function declaration
ex();
ex();
ex();
}
void ex()
{
 static int a = 0;        //Static variable
 a++;
 printf("%d\t",a);
}
output :
1    2    3


//static
#include<stdio.h>
#include<conio.h>
void main()
{
 int i;
 void fun();
 clrscr();
 for(i=0;i<=3;i++)
 {
   fun();
 }
 getch();
}
//fun() definition
void fun()
{
 static int a=0;
// int a=0;
 printf("%d\n",a);
 a++;
}

register:

Register variable inform the compiler to store the variable in register instead of memory. Register variable has faster access than normal variable. Frequently used variables are kept in register. Only few variables can be placed inside register.

NOTE : It is not possible to get the address of register variable
What are the advantages of auto variables?       
Is it acceptable to declare/define a variable in a C header?          
Constant volatile variable declaration is possible or not? if give any one example and reason.     
Differentiate between an internal static and external static variable?     
What is the benefit of using an enum rather than a #define constant?   
What is the difference between declaring a variable and defining a variable?     
What are the advantages and disadvantages of binary floating point representation?       
Difference between : - 1)Global variable and Local variable , 2)Static variable and Global variable ?   What are comment line arguments in C programing? and also give an example.               
Are the variables argc and argv are local to main()?         
How pointer variables are initialized ?    
Can static variables be declared in a header file?              
Can a variable be both const and volatile?           
1)What is static identifier? 2)Where are the auto variables stored?          
What is the benefit of using const for declaring constants?          
What is the benefit of using #define to declare a constant?        
Where does global, static, local, register variables, free memory and C Program instructions get stored?               
Can static variables be declared in a header file ?             
Can a variable be both const and volatile?           
Can a variable be both constant and volatile ?

You may like the following posts:
typedef

No comments:

Post a Comment