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.
No comments:
Post a Comment