0% found this document useful (0 votes)
50 views

Storage Class Specifiers

1. auto, extern, static, and register are storage class specifiers that determine a variable's lifetime, visibility, default value, and storage location. 2. If no storage class is specified, variables declared within a function are auto, functions declared within a function are extern, and variables/functions declared outside a function are static. 3. The storage class specifiers determine whether a variable is stored in memory or a processor register, and whether it maintains its value between function calls.

Uploaded by

LEESHMA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Storage Class Specifiers

1. auto, extern, static, and register are storage class specifiers that determine a variable's lifetime, visibility, default value, and storage location. 2. If no storage class is specified, variables declared within a function are auto, functions declared within a function are extern, and variables/functions declared outside a function are static. 3. The storage class specifiers determine whether a variable is stored in memory or a processor register, and whether it maintains its value between function calls.

Uploaded by

LEESHMA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Storage class specifiers

automatic, extern, static storage, register storage.


C++ Storage Classes
• These are used to describe the characteristics of a variable/function.
• It determines the lifetime, visibility, default value, and storage location
which helps us to trace the existence of a particular variable during the
runtime of a program.
• Storage class specifiers are used to specify the storage class for a
variable.
Special Case: When no storage class specifier is declared or defined in a program
There is at least one storage class specifier is given in the variable declaration. But, in case,
there is no storage class specifier specified, the following rules are followed:

1. Variables declared within a function are considered as auto.

2. Functions declared within a function are considered as an extern.

3. Variables and functions declared outside a function are considered static, with external
linkage.
Syntax

• To specify the storage class for a variable, the following syntax is to be


followed:

storage_class var_data_type var_name;


1. auto
• This is the default storage class for all the variables declared inside a function or a block.

• 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);
}

Thus, the output generated here would be:


0
1. What would be the output of the following program?
#include<stdio.h>
int func(){
static int a=0;
int b=0;
a++;
b++;
printf(“a= %d and b= %d\n”, a, b);
}
int main() {
func();
func();
func();
return 0;
}
a= 1 and b= 1
a= 2 and b= 1
a= 3 and b= 1
2. What would be the output of the following program?
#include<stdio.h>
int main(){
int x=20;
auto int y=30;
printf(“%d %d”,x,y);
return 0;
}
20 30

You might also like