Storage Class Specifiers
Storage Class Specifiers
3. Variables and functions declared outside a function are considered static, with external
linkage.
Syntax
• Hence, the keyword auto is rarely used while writing programs in C language.
• Auto variables can be only accessed within the block/function they have been declared and not
outside them (which defines their scope).
• Of course, these can be accessed within nested blocks within the parent block/function in which the
auto variable was declared.
• However, they can be accessed outside their scope as well using the
concept of pointers given here by pointing to the very exact memory
location where the variables reside. They are assigned a garbage value by
default whenever they are declared.
#include <stdio.h>
int main()
{
int p = 10,i;
printf(“%d “,++p);
{
int p = 20;
for (i=0;i<3;i++)
{
printf(“%d “,p); // 20 will be printed here 3 times because it is the
given local value of p
}
}
printf(“%d “,p); // 11 will be printed here since the scope of p = 20 has finally ended.
}
2.extern
• 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.
• Also, a normal global variable can be made extern as well by placing the ‘extern’ keyword before its
declaration/definition in any function/block. This basically signifies that we are not initializing a new
variable but instead, we are using/accessing the global variable only. The main purpose of using
extern variables is that they can be accessed between two different files which are
part of a large program.
3. static
• This storage class is used to declare static variables which are popularly used while writing
programs in C language. Static variables have the 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.
Features of static variables:
• Those variables that we define as static specifiers are capable of holding their assigned value that
exists between various function calls.
• The static local variables are only visible to the block or the function in which we have defined them.
• We can declare the very same static variable multiple times, but we can only assign it a single time.
• The initial value of a static integral variable, by default, is 0. Else, it is null.
• We use the static keyword in the case of a static variable.
• The visibility of any static global variable stays limited to that file in which we have declared it.
#include <stdio.h>
void func(void);
static int count = 10;
main() {
while(count–) {
func(); /* increment of function */
}
return 0;
}
void func( void ) {
static int x = 10; /* a local type of static variable */
x++;
printf(“x is %d and the count is %d\n”, x, count);
}
When the above code is compiled and executed, it produces the following result −
x is 11 and the count is 9
x is 12 and the count is 8
x is 13 and the count is 7
x is 14 and the count is 6
x is 15 and the count is 5
#include<stdio.h>
void sum()
{
static int x = 20;
static int y = 34;
printf(“%d %d \n”,x,y);
x++;
y++;
}
void main()
{
int a;
for(a = 0; a< 3; a++)
{
sum(); // Given static variables will hold their values between
the various function calls.
}
}
The output generated here would be:
20 34
21 35
22 36
4. register
• This storage class declares register variables that have the same functionality as that of the auto
variables.
• The only difference is that the compiler tries to store these variables in the register of the
microprocessor if a free register is available.
• This makes the use of register variables to be much faster than that of the variables stored in the
memory during the runtime of the program.
• If a free registration is not available, these are then stored in the memory only.
• Usually, a few variables which are to be accessed very frequently in a program are declared with the
register keyword which improves the running time of the program.
• An important and interesting point to be noted here is that we cannot obtain the address of a
register variable using pointers.
Features of register variables:
• Those variables that we define as the register have their memory allocation into the CPU registers. It
depends totally upon the size of the memory that remains in the CPU.
• Its access time is comparatively much faster than that of the automatic variables.
• One cannot dereference a register variable. In other words, one cannot make use of the ‘&’ operator in
the case of a register variable.
• The default initial value of any given register local value will always be 0.
• We use the register keyword for the variable that must be stored in a CPU register. However, whether a
variable must be stored in a register or not is always going to be the choice of the compiler.
• One can easily store the pointers in a register. It means that any register is capable of storing the given
variable’s address.
• We cannot store a variable into a register since we can’t really utilize more than one storage specifier for
the very same variable.
#include <stdio.h>
int main()
{
register int x; // A variable x has memory allocation in the CPU
register. Here, the initial value of x, by default, is 0.
printf(“%d”,x);
}