Storage classes in c

In c there are four types of storage class. They are:
1. auto
2. register
3. static
4. extern


Storage class is modifier or qualifier of data types which decides:

1.  In which area of memory a particular variable will be stored?  
2. What is scope of variable?
3. What is visibility of variable?

Visibility of a variable in c:

    Visibility means accessibility. Up to witch part or area of a program, we can access a variable, that area or part is known as visibility of that variable. For example: In the following figure yellow color represents visibility of variable a.


Scope of a variable in c:

    Meaning of scope is to check either variable is alive or dead. Alive means data of a variable has not destroyed from memory. Up to which part or area of the program a variable is alive, that area or part is known as scope of a variable. In the above figure scope of variable a represented outer red box i.e. whole program.
Note: If any variable is not visible it may have scope i.e. it is alive or may not have scope. But if any variable has not scope i.e. it is dead then variable must not to be visible.

There are four type of scope in c:

1. Block scope.
2. Function scope.
3. File scope.
3. Program scope.

Block scope:

    In c block is represented area between opening curly bracket i.e. {and closing curly bracket i.e.}. Example of blocks in c.


In the c code there are three blocks shown in yellow color. In the above code one more block which is main function block. If a variable or function has scope only within the scope where it has declared then scope of variable or function is called as block scope and if a variable or function is visible is only within a block where it has declared then that variable or function is called as block visible.  

Function block:

A block of function body has special name, function block. From storage class point of view there is not any difference between function block and any other blocks in c. Because there is not any modifiers of storage class group which has function block. Importance of function block can understand when we deal with goto statement. Label of goto statement has function block scope. Label of particular goto statement is not visible in another function. For example:

#include<stdio.h>
void display();
int main(){
    printf("In MAIN");   
    goto xyz;
    return 0;
}
void display(){
    xyx:;
    printf("In DISPLay");
}

Output: Compilation error

File scope:

    If any variable or function has scope only within a file where it has declared then scope of variable or function is known file scope. If any variable or function is visible only within a file where it has declared then visibility of that variable or function is called file visible.

Program scope:

    If any variable or function has scope whole of the program, program may contain one or more files then scope of variable or function is known program scope. If any variable or function which is visible in the whole program, program may contain one or more file then variable or function, that variables or functions are called as program visible.

Scope and visibility of storage class:

13 comments:

roshan said...

thanks, this is very good ....

roshan said...

i got every ans about storage classes, thanks

arshavj said...

thank you very much....

natts said...

nice articles.......thank u very much.......

venki said...

awesome website for c language

Anonymous said...

Could have explain much better. but I think what you have explained as visibility is itself scope and explanation of the scope is called is life or variable. Isn't it?

Anonymous said...

thank u very much :)

Anonymous said...

Great Work man..

addanki sivaramakrishna said...

thank u sir. i got lot of information about storage class.

Anonymous said...

Really awesome website......And the website have the nice article

Unknown said...

thanks lot.....

Unknown said...

According to the article label of goto function is not visible in another program but if the label is not visible in anoter function then how we can jump in another function?

Unknown said...

good but i want some more examples ,which contains a programs and use of these variables in that programs.