0% found this document useful (0 votes)
31 views36 pages

Storage Classes - Datatype Variable - Name - Int A Int B 10

The document discusses storage classes in C. There are four storage classes - automatic, register, static, and external. The storage class determines where a variable is stored (registers or memory), its initial value, scope, and lifetime. Automatic variables are stored in memory, have an undefined initial value, local scope to the block they are defined in, and exist until the end of the block. Static variables are stored in memory, have an initial value of 0, local scope but retain their value between function calls. External variables are stored in memory, have an initial value of 0, global scope, and exist for the entire program execution.
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)
31 views36 pages

Storage Classes - Datatype Variable - Name - Int A Int B 10

The document discusses storage classes in C. There are four storage classes - automatic, register, static, and external. The storage class determines where a variable is stored (registers or memory), its initial value, scope, and lifetime. Automatic variables are stored in memory, have an undefined initial value, local scope to the block they are defined in, and exist until the end of the block. Static variables are stored in memory, have an initial value of 0, local scope but retain their value between function calls. External variables are stored in memory, have an initial value of 0, global scope, and exist for the entire program execution.
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/ 36

STORAGE CLASSES

• Till now how we defined a variable?


• Datatype variable _name;
• int a; int b=10
STORAGE CLASSES
• But to fully defined a variable we need to
mention its storage class also…
• Storage _class Datatype variable _name;
• auto int a;
• Register int b=10;
STORAGE CLASSES
 The storage class determines the part of the
memory where the variable would be stored.
 The storage class also determines the initial
value of the variable.
 and it used to define the scope and
lifetime of variable.
 There are two storage location in
computer : CPU Registers and
Memory
They tells us
1.Where the variable would be stored
(registers or memory)
2.Initial value (zero or garbage value)
3.scope-in which block the variable is
accessible (or the value of the variable
would be available)
4.Life time-how long the variable exist.
TYPES OF STORAGE CLASSES
• There are four types of storage classes in C:
Automatic storage class
 Register storage class
Static storage class
External storage class
Automatic Storage Class
Keyword : auto
Storage : memory
Default initial value : garbage value
Scope :local to the block in
which the variable
is defined.
Life time :Till end of the Block
What is block
main()
{
int a;
{
int b;
}
}
main()
{
auto int i,j;
printf(“%d%d”,i,j);
}
Output:
main()
{
auto int a=10;
{
auto int b=20;
printf(“%d”, b);
}
}
main()
{
auto int a=10;
{
auto int b=20;
}
printf(“%d”, b);
}
main()
{
auto int a=10;
{
auto int b=20;
printf(“%d”, b);
printf(“%d”, a);
}

}
main()
{
auto int a=10;
{
auto int a=20;
printf(“%d”, a);

}
main()
{
auto int a=10;
{
auto int a=20;
printf(“%d”, a);

}
printf(“%d”, a);
}
main()
{
auto int i=1;
{
{
{
printf(“%d”,i);
}
printf(“%d”,i);
}
printf(“%d”,i);
}
}
main()
{
auto int i=1;
{
auto int i=2;
{
auto int i=3;
printf(“%d”,i);
}
printf(“%d”,i);
}
printf(“%d”,i);

}
main()
{
auto int a=10;
{
auto int a;
printf(“%d”, a);

}
printf(“%d”, a);
}
main()
{
increment();
Increment();
Increment();
}
Increment()
{
auto int i=1;
Printf(“%d”,i);
i=i+1;
}
Register Storage Class
Keyword : register
Storage : CPU Register
Default initial value : garbage value
Scope :local to the block in
which the variable
is defined.
Life time :Till end of the Block
CPU REGISTER AND MEMORY

A value stored in a CPU register can


always be accessed faster then the one
that is stored in memory.
If a variable is used at many places it is
better to declare its storage class as
register. Ex: loop variables.
Static Storage Class
Keyword : static
Storage : memory
Default initial value : zero
Scope : local to the block in
which the variable
is defined.
Life time : value of the variable
persists between
different function calls.
main()
{
increment();
Increment();
Increment();
}
Increment()
{
static int i=1;
Printf(“%d”,i);
i=i+1;
}
Diff b/w auto and static
• Static variables don’t disappear when the
function is no longer active. their values
persist.
• If the control comes back to the same function
again the static variables have the same values
they had last time around.
External Storage Class
Keyword : static
Storage : memory
Default initial value : zero
Scope : global
Life time : as long as program’s
execution doesn’t
comes to an end
void increment(); increment()
void {
decrement(); i=i+1;
int i; printf("\n on
int main() incrementing i=%d",i);
{ }
printf("i=%d",i);
increment(); decrement()
increment(); {
decrement(); i=i-1;
decrement(); printf("\n on
return 0; incrementing i=%d",i);
}
increment()
void increment();
{
void decrement(); extern int i;
int i; i=i+1;
int main() printf("\n on
{ incrementing i=%d",i);
extern int i; }
printf("i=%d",i);
increment(); decrement()
increment(); {
decrement(); extern int i;
i=i-1;
decrement();
printf("\n on
return 0; decrementing i=%d",i);
int x=10;
main()
{
int x=20;
printf(“%d”,x);
Display();
}
Display()
{
printf(“%d”,x);
}
#include <stdio.h>

void staticDemo()
{
static int i; Output: 1 0
{ 2 1
static int i = 1;
printf("%d ", i);
i++;
}
printf("%d\n", i);
i++;
}

int main()
{
staticDemo();
staticDemo();
}
#include <stdio.h>
int main()
{
static int i=5;
if(--i){
main();
printf("%d ",i);
}
}
• Output< 0 0 0 0
#include <stdio.h>
int main()
{
static int i=5;
if (--i){
printf("%d ",i);
main();
}
}
• Output< 4 3 2 1
#include <stdio.h>
int main()
{
int x = 10;
static int y = x;

if(x == y)
printf("Equal");
else if(x > y)
printf("Greater");
else
printf("Less");
return 0;
}
• In C, static variables can only be initialized
using constant literals. This is allowed in C++
though.
include <stdio.h>
int main()
{
static int count=5;
printf("\n count=%d",count--);
if(count!=0)
main();
return 0;
}
• 54321
#include <stdio.h>
int g(int);
int main()
{
int i,j;
for(i=1;i<5;i++)
{ j=g(i);
printf("%d",j);
}
return 0;
}
int g(int x)
{ static int v=1;
int b=3;
v=v+x;
return(v+x+b);
}

You might also like