Prepared by
S.Manjula
AP/CSE
Ramco Institute of Technology
Rajapalayam
• The name of a variable is associated with a
memory location within the computer where
the value assigned to the variable is stored in
the form of bits.
• During the execution of the program, these
variables may be stored in the registers of the
CPU or the primary memory of the computer.
• To indicate
– where the variables would be stored,
– how long they would exist,
– what would be their region of existence,
– what would be the default values
Four storage class
C provides four storage class specifiers that can be used along with
the data type specifiers in the declaration statement of a variable.
Automatic Storage Class
• All variables defined within a function or block by
default belong to automatic storage class if no storage
class is mentioned.
• Variables having automatic storage class are local to
the block which they are defined in, and get destroyed
on exit from the block.
automatic
Example:
#include <stdio.h>
int main( )
{
auto int j = 1;
auto int k;
{
auto int j= 2;
{
auto int j = 3;
printf ( "%d ", j);
}
printf ( "t%d ",j);
}
printf( "%dn", j);
printf( "%dn", k);
}
Output
3 2 1 garbage
Register Storage Class
• Variables belonging to register storage class are local to
the block which they are defined in, and get destroyed on
exit from the block.
• A register declaration is equivalent to
an auto declaration, but hints that the declared variable
will be accessed frequently; therefore they are placed in
CPU registers, not in memory.
register
# include<stdio.h>
main( )
{
register int a=200;
printf(“The value of a is %d”,a);
}
Output:
The value of a is 200
Static Storage Class
• Static variables can be used within function or file.
• Unlike global variables, static variables are not visible
outside their function or file, but they maintain their
values between calls.
• The static specifier has different effects upon local and
global variables.
static
#include<stdio.h>
void sum()
{
static int a = 10;
static int b = 24;
a++;
b++;
printf("%d %d n",a,b);
}
void sum1()
{
int a = 10;
int b = 24;
a++;
b++;
printf("%d %d n",a,b);
}
void main()
{
printf("Using Staticn");
sum();
sum();
printf("Without Using Staticn");
sum1();
sum1();
}
Output:
Using Static
11 25
12 26
Without Using Static
11 25
11 25
External Storage Class
• The principal use of extern is to specify that a
variable is declared with external linkage elsewhere
in the program.
• When extern specifier is used with a variable
declaration then no storage is allocated to that
variable and it is assumed that the variable has
already been defined elsewhere in the program.
• When we use extern specifier the variable cannot be
initialized because with extern specifier variable is
declared, not defined.
external
2-14
Question?
• A good question deserve a good grade…
Check the Progress
1.Where will the space be allocated for an
automatic storage class variable?
a) In CPU register
b) In memory as well as in CPU register
c) In memory
d) On disk.
2.What is the output of the program.?
void myshow();
int main()
{ myshow();
myshow();
myshow();
}
void myshow()
{
static int k = 20;
printf("%d ", k);
k++;
}
A) 20 20 20
B) 20 21 21
C) 20 21 22
D) Compiler error.
ANSWER:
1.c) In memory
2. c) 20 21 22

Storage class

  • 1.
  • 2.
    • The nameof a variable is associated with a memory location within the computer where the value assigned to the variable is stored in the form of bits. • During the execution of the program, these variables may be stored in the registers of the CPU or the primary memory of the computer. • To indicate – where the variables would be stored, – how long they would exist, – what would be their region of existence, – what would be the default values
  • 3.
    Four storage class Cprovides four storage class specifiers that can be used along with the data type specifiers in the declaration statement of a variable.
  • 6.
    Automatic Storage Class •All variables defined within a function or block by default belong to automatic storage class if no storage class is mentioned. • Variables having automatic storage class are local to the block which they are defined in, and get destroyed on exit from the block.
  • 7.
    automatic Example: #include <stdio.h> int main() { auto int j = 1; auto int k; { auto int j= 2; { auto int j = 3; printf ( "%d ", j); } printf ( "t%d ",j); } printf( "%dn", j); printf( "%dn", k); } Output 3 2 1 garbage
  • 8.
    Register Storage Class •Variables belonging to register storage class are local to the block which they are defined in, and get destroyed on exit from the block. • A register declaration is equivalent to an auto declaration, but hints that the declared variable will be accessed frequently; therefore they are placed in CPU registers, not in memory.
  • 9.
    register # include<stdio.h> main( ) { registerint a=200; printf(“The value of a is %d”,a); } Output: The value of a is 200
  • 10.
    Static Storage Class •Static variables can be used within function or file. • Unlike global variables, static variables are not visible outside their function or file, but they maintain their values between calls. • The static specifier has different effects upon local and global variables.
  • 11.
    static #include<stdio.h> void sum() { static inta = 10; static int b = 24; a++; b++; printf("%d %d n",a,b); } void sum1() { int a = 10; int b = 24; a++; b++; printf("%d %d n",a,b); } void main() { printf("Using Staticn"); sum(); sum(); printf("Without Using Staticn"); sum1(); sum1(); } Output: Using Static 11 25 12 26 Without Using Static 11 25 11 25
  • 12.
    External Storage Class •The principal use of extern is to specify that a variable is declared with external linkage elsewhere in the program. • When extern specifier is used with a variable declaration then no storage is allocated to that variable and it is assumed that the variable has already been defined elsewhere in the program. • When we use extern specifier the variable cannot be initialized because with extern specifier variable is declared, not defined.
  • 13.
  • 14.
    2-14 Question? • A goodquestion deserve a good grade…
  • 15.
    Check the Progress 1.Wherewill the space be allocated for an automatic storage class variable? a) In CPU register b) In memory as well as in CPU register c) In memory d) On disk. 2.What is the output of the program.? void myshow(); int main() { myshow(); myshow(); myshow(); } void myshow() { static int k = 20; printf("%d ", k); k++; } A) 20 20 20 B) 20 21 21 C) 20 21 22 D) Compiler error.
  • 16.