0% found this document useful (0 votes)
11 views6 pages

Storage Class

The document discusses the different storage classes in C programming language: auto, static, register, and extern. It provides details on the scope, location, initial value, and lifetime of variables for each storage class.

Uploaded by

Pavankumar Bhise
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)
11 views6 pages

Storage Class

The document discusses the different storage classes in C programming language: auto, static, register, and extern. It provides details on the scope, location, initial value, and lifetime of variables for each storage class.

Uploaded by

Pavankumar Bhise
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/ 6

Storage Classes In C

# Definition

i) A storage class represents the visibility & a location of a variable .

ii) It is used in c to describe

i) The variable Scope

ii) The Location where the variable is stored

iii) The initialized value

iv) A lifetime of a variable

Types
i) There are four types of storage classes in c & they are

i) auto ( automatic ) storage class

ii) static storage class

iii) register storage class

iv) extern storage class


i) auto storage class

i) The keyword auto is used to represent this type of class.


ii) Here it has :
i) The variable Scope : Is limited within the block only ( Local)

ii) The Location where the variable is stored is RAM

iii) The initialized value : Garbage value

iv) A lifetime of a variable is accessible to the block only where


it has been declared .

iii) Example

#include<stdio.h>
void main()
{
auto int a;

printf("\n value of a before inner blk is : %d\n", a);


{
auto int a=45;
printf("\n value of a in inner blk is : %d\n", a);
}

O/P :
value of a before inner blk is : -1080081572

value of a in inner blk is : 45


ii) static storage class

i) The keyword static is used to represent this type of class.


ii) Here it has :
i) The variable Scope : Is limited within the function &
block in which it is defined ( Local + Global) .

ii) The Location where the variable is stored is RAM

iii) The initialized value : Zero ( 0 )

iv) A lifetime of a variable is accessible till end of a program

iii) Exmple

1)
#include<stdio.h>

void main()
{
static int a;

printf("\n value of a before is : %d\n", a);

a++;

printf("\n value of a before inner blk is : %d\n", a);


{
a=45+a;

printf("\n value of a is : %d\n", a);


}

printf("\n value of a is : %d\n", a);


}
O/P :
value of a before is : 0
value of a before inner blk is : 1
value of a is : 46
2) With function
#include<stdio.h>
void display( );
void main()
{
printf(“\n%d\n”, display());
printf(“\n%d\n”, display());
}
void display( )
{
static int a=2;
a++;
return a;
}

O/P : 3
4
iii) register storage class

i) The keyword register is used to represent this type of class.


ii) Here it has :

i) The variable Scope : Is limited within the block only ( Local)

ii) The Location where the variable is stored in register memory

iii) The initialized value : Garbage value

iv) A lifetime of a variable is accessible till end of a program .

Iii) Example
#include<stdio.h>

void main()
{
register int a=45;
int *m ;
m =&a;
printf(“\ Address =%d\n value =%d\n”,m ,*m);
}

O/P : error: address of register variable ‘a’ requested


m =&a;
^

void main()
{
register int a=45;

a++;
printf(“\n %d\n”,a);
}
O/P : 46
iv) extern storage class

i) The keyword extern is used to represent this type of class.


ii) Here it has :

i) The variable Scope : Is outside of all functions (Global )

ii) The Location where the variable is stored in RAM

iii) The initialized value : Zero ( 0 ) value

iv) A lifetime of a variable is accessible till end of a program

iii) Example

create user header file var.h

extern int n1 =45;


extern int n2=18;

create c source program add.c

#include<stdio.h>

#include “var.h”

void main()
{
static int add;
add=n1+n2;
printf(“\n%d+%d=%d\n”,n2,n1,add);
}

Output :- > 18 + 45 = 63

You might also like