About Me

Hey friends! My name is yuvraj basnet. I'm going to introduce myself.I have born and grown up in india(chandigarh) and I'm studying second year of B.C.A computer science field. My father serving in indian air force. My mother has been taking care of house hold. I am a friendly person, good thinker, I can work for longer hours without any break, only if I'm interested in that...My life style is very simple.

STORAGE CLASSES

In this tutorial you will learn most important term of c programming i.e, storages classes with examples.

I will also provide a link to easily understand the storage classes. 

                                            

                                           STORAGE CLASSES

A storage class defines the scope (visibility) and life-time of variables and/or functions within a C Program. They precede the type that they modify.
   
We have four different storage classes in a C program: 

1) auto storage classes.
 
2) register storage classes.
  
3) static storage classes. 
 
4) extern storage classes.

Image result for storage classes in c


1)AUTO STORAGE CLASSES-
The auto storage class is the default storage class for all local variables.

 {
 int mount; 
 auto int month;
 } 

 The example above defines two variables within the same storage class. ‘auto’ can only be used within functions, i.e., local variables.

2)REGISTER STORAGE CLASSES-
The register storage class is used to define local variables that should be stored in a register instead of RAM. This means that the variable has a maximum size equal to the register size (usually one word) and can't have the unary '&' operator applied to it (as it does not have a memory location).

 {
 register int miles;
 }

3)STATIC STORAGE CLASSES-
This storage class is used to declare static variables which are popularly used while writing programs in C language. Static variables have a property of preserving their value even after they are out of their scope! Hence, static variables preserve the value of their last use in their scope. So we can say that they are initialized only once and exist till the termination of the program. Thus, no new memory is allocated because they are not re-declared. Their scope is local to the function to which they were defined. Global static variables can be accessed anywhere in the program. By default, they are assigned the value 0 by the compiler.

void test()
{
    static int a
= 0;       //a static variable
    a
= a + 1;
   
printf("%d\t",a);
} 


4)EXTERN STORAGE CLASSES-
Extern storage class simply tells us that the variable is defined elsewhere and not within the same block where it is used. Basically, the value is assigned to it in a different block and this can be overwritten/changed in a different block as well. So an extern variable is nothing but a global variable initialized with a legal value where it is declared in order to be used elsewhere. It can be accessed within any function/block.

int main()
{
    extern int x
;   //informs the compiler that it is defined somewhere else
    x
= 10;     
   
printf("%d", x);    } CLICK HERE, To watch the video of described 4 storage classes of the c - auto, register, extern, static. All storage classes defines the storing place, scope and lifetime for the variable.




 

Post a Comment

0 Comments