0% found this document useful (0 votes)
6 views9 pages

Storage Classes in C

The document explains storage classes in C programming, detailing their scope, default initial values, and lifetimes. It covers four main types: automatic, static, register, and extern variables, providing examples for each. Each storage class has specific characteristics that determine how variables are stored and accessed during program execution.

Uploaded by

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

Storage Classes in C

The document explains storage classes in C programming, detailing their scope, default initial values, and lifetimes. It covers four main types: automatic, static, register, and extern variables, providing examples for each. Each storage class has specific characteristics that determine how variables are stored and accessed during program execution.

Uploaded by

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

Storage Classes

Storage classes are used to describe the features of a variable. These features
basically include the scope, visibility and life-time which help us to trace the
existence of a particular variable during the runtime of a program.

In C language, each variable has a storage class which decides the following
things:

● scope i.e where the value of the variable would be available inside a
program.
● default initial value i.e if we do not explicitly initialize that variable, what
will be its default initial value.
● lifetime of that variable i.e for how long will that variable exist.

The following storage classes are most often used in C programming,

1. Automatic variables
2. External variables
3. Static variables
4. Register variables

Automatic variables: auto

Scope: Variables defined with auto storage class are local to the function block
inside which they are defined.

Default Initial Value: Any random value i.e garbage value.

Lifetime: Till the end of the function/method block where the variable is defined.

A variable declared inside a function without any storage class specification, is by


default an automatic variable. They are created when a function is called and are
destroyed automatically when the function's execution is completed. Automatic
variables can also be called local variables because they are local to a function. By
default they are assigned garbage value by the compiler.

#include<stdio.h>

void main()
{
int detail;
// or
auto int detail; //Both are same
}

We take another program which shows the scope level “visibility level” for auto
variables in each block code which are independently to each other:

#include <stdio.h>
int main( )
{
auto int j = 1;
{
auto int j= 2;
{
auto int j = 3;
printf ( " %d ", j);

}
printf ( "\t %d ",j);
}
printf( "%d\n", j);

}
OUTPUT:
321

Example:

#include <stdio.h>
int main()
{
int a = 10,i;
printf("%d ",++a);
{
int a = 20;
for (i=0;i<3;i++)
{
printf("%d ",a); // 20 will be printed 3 times since it is the local value of a
}
}
printf("%d ",a); // 11 will be printed since the scope of a = 20 is ended.
}

Output:

11 20 20 20 11

Static variables

Scope: Local to the block in which the variable is defined

Default initial value: 0(Zero).


Lifetime: Till the whole program doesn't finish its execution.

Variables, may it be global or local, are stored using a static specifier in static
storage class when the variable needs to be declared once, and the value needs to
be retained. When a variable is declared as static, the value will be saved or
retained between the function calls. Permanent storage is created, and it is
declared only once. When a local variable is declared as static, permanent storage
is created for it, and the value is retained every time it is used.

The static variables are used within the function/ file as local static variables. They
can also be used as a global variable

● Static local variable is a local variable that retains and stores its value
between function calls or blocks and remains visible only to the function or
block in which it is defined.
● Static global variables are global variables visible only to the file in which
it is declared.

They are assigned 0 (zero) as default value by the compiler.

Example: static int count = 10;

Example:

#include <stdio.h>
void display();

int main()
{
display();
display();
}
void display()
{
static int c = 1;
c += 5;
printf("%d ",c);
}

Output

6 11

During the first function call, the value of c is initialized to 1. Its value has
increased by 5. Now, the value of c is 6, which is printed on the screen.

During the second function call, c is not initialized to 1 again. It's because c is a
static variable. The value c is increased by 5. Now, its value will be 11, which is
printed on the screen.

Example 2:
#include<stdio.h>

void test(); //Function declaration

int main()
{
test();
test();
test();
}

void test()
{
static int a = 0; //a static variable
a = a + 1;
printf("%d\t",a);
}

Example 3:

#include <stdio.h>

/* function declaration */
void func();

static int count = 5; /* global variable */

int main()
{

while(count>0)
{
count--;
func();
}

return 0;
}

/* function definition */
void func( )
{

static int i = 5; /* local static variable */


i++;

printf("i is %d and count is %d\n", i, count);


}
When the above code is compiled and executed, it produces the following
result −
i is 6 and count is 4
i is 7 and count is 3
i is 8 and count is 2
i is 9 and count is 1
i is 10 and count is 0

Register Storage class

Scope: Local to the function in which it is declared.

Default initial value: Any random value i.e garbage value

Lifetime: Till the end of function/method block, in which the variable is defined.

This storage class declares register variables which 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 register is not available, these are then stored in the memory only.
Usually 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.

Example: register int age;

Example:
#include <stdio.h> /* function declaration */
void main()
{
register int weight;
int *ptr=&weight ;
/*it produces an error when the compilation occurs ,
//we cannot get a memory location when dealing with CPU register*/
}

OUTPUT:
error: address of register variable 'weight' requested
NOTE: We can never get the address of such variables.

Example:

#include <stdio.h>
int main()
{
register int i, j;
int rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i)
{
for (j = 1; j <= i; ++j)
{
printf("* ");
}
printf("\n");
}

return 0;
}

Extern Storage class

External or Global variable


Scope: Global i.e everywhere in the program. These variables are not bound by
any function, they are available everywhere.

Default initial value: 0(zero).

Lifetime: Till the program doesn't finish its execution, you can access global
variables.

Extern stands for external storage class. Extern storage class is used when
we have global functions or variables which are shared between two or more
files.
Keyword extern is used to declare a global variable or function in another file
to provide the reference of a variable or function which has been already
defined in the original file.

You might also like