Storage Class in C Language :
here we are sharing the storage classes in c language. variables
can be put into some combination of scope, lifetime and visibility by using
some keywords and placing them appropriately and this is called a storage
class of the variable.
Broadly
speaking there are only two storage classes. Auto and static. Other storage classes
are extern and register.
1. Static Variables
Depending
In the context where the static declaration is used, it can give two different
meanings.
1.
If declared at global scope it means that the variable has file scope.
E.g.1
static
myprintf( ); // only file scope
declares
that myprintf function is having file scope and is not accessible to other
files.
E.g.
2.
int a; // global scope
static int b; // file scope
2.
If declared within a block it means that the variable is having a static lifetime
but with local scope. As a whole, the static keyword helps in limiting the
variables to file scope such that collision of names at the time of linkage is
avoided. In other words, static limits variables to internal linkage. A static
variable (with local or block scope) can be initialized with the address of any
external or static item, but not with the address of another auto item. Because
the address of an auto item is not a constant since auto items are created in
stacks and erased as the function exits.
2. Extern
‘extern’
is used for two main purposes; it can be used for:
_
for specifying global linkage and
_
forward declarations,
The first use of extern is to explicitly specify that the variable is a candidate
for external linkage.
extern
int fun(); specifies
that the function fun is defined somewhere else and that will be known only at link-time.
extern
int i; // declare i is defined
somewhere else
...
int
i = 10;
//
i resolves to this variable i.
//
This is resolved by the linker and the programmer may have
//
used it for forward reference.
3. Auto
Auto
keyword specifies that the variable is of an automatic lifetime. It is not instructive
to the compiler but for you to just remember that it is of automatic type. Practically
it serves no purpose (of course you know that the variables declared inside functions
are automatic and told explicitly that the variable is auto doesn’t help you
anyway).
4. Register
You
can specify a local variable as register if you think that variable may be accessed
frequently. The compiler will try to put that variable in a microprocessor register
if one is available. Else this keyword is ignored and treated as if it is
declared as auto. Declaring such frequently used variables to be placed in
registers may only lead to small performance improvement. Modern compilers will
easily find out the variables that will be frequently accessed and will place
them accordingly. This is a deprecated feature
(so
will be omitted in future versions of C) and so is not recommended to use. It
is illegal to apply ‘&’ operator to a register variable. Why because of both
the pointer that stores the address of the variable and the value in that the address has to be in the memory for applying & and * operators. Then only
these operations remain meaningful and valid. However, register variables may
not be inactive memory when the & operator applied and so C prohibits
applying this operator on register variables. Register variables can be used to
improve program efficiency. Looping variables are good candidates for such
register optimizations,
register
int i;
for(
i=0 ; i < INT_MAX; i++)
arrayAccess[i];
In
such cases, the efficiency of execution certainly increases. If a memory the register is not available for placing it in memory it may at-least place the
variable in cache memory that is still an improvement in execution speed. Anyway,
using register variables is found to speed up many programs by a factor
of
two.
Scope of a variable -:
v In C, a variable can be
declared anywhere in the program but before using them.
v The area of the program
within which a variable is accessible, known as its scope.
v
A variable can be accessed within the block where it is declared.
No comments:
Post a Comment