C Programming Lesson 5
C Programming Lesson 5
KIIT 2014
Objectives
After completing this lesson, you should be able to do the following:
• Define storage class
• Define types of storage class
– Automatic storage class
– Register storage class
– Static storage class
– External storage class
• Examples of storage class
KIIT 2014
Storage Class
• Every variable and function in C programming has two
properties: type and storage class. Type refers to the data
type of variable whether it is character or integer or floating-
point value etc
• The storage class determines the part of memory where
storage is allocated for an object and how long the storage
allocation continues to exist.
• It also determines the scope which specifies the part of the
program over which a variable name is visible, i.e. the
variable is accessible by name.
KIIT 2014
Types of storage class
There are 4 types of storage class:
KIIT 2014
Automatic Variable
• Are declare at the start of a block.
• Memory is allocated automatically upon entry to a block and
freed automatically upon exit from the block.
• The scope of automatic variables is local to the block in which
they are declared, including any blocks nested within that
block.
• For these reasons, they are also called local variables. No
block outside the defining block may have direct access to
automatic variables, i.e. by name.
• Automatic variables may be specified upon declaration to be
of storage class auto.
KIIT 2014
Automatic Variable
• The variable declared as auto is stored in the memory.
• Default value of that variable is garbage value..
Example:
#include<conio.h>
#include<stdio.h>
void main(){
auto int a;
printf(“%d”,a)
}
1285
KIIT 2014
Register Variable
• Register variables are a special case of automatic variables.
• Automatic variables are allocated storage in the memory of
the computer; however, for most computers, accessing data in
memory is considerably slower than processing in the CPU.
• These computers often have small amounts of storage within
the CPU itself where data can be stored and accessed quickly.
These storage cells are called registers.
• For these reasons, they are also called local variables.
KIIT 2014
Register Variable
• Variables which are used repeatedly or whose access times
are critical, may be declared to be of storage class register.
• Main difference between auto and register is that variable
declared as auto is stored in memory whereas variable
declared as register is stored in CPU register
#include<conio.h>
#include<stdio.h>
void main(){
register int a;
printf(“%d”,a)
}
KIIT 2014
External Variable
• In some applications it may be useful to have data which is
accessible from within any block and/or which remains in
existence for the entire execution of the program.
• Such variables are called global variables, and the C language
provides storage classes which can meet these requirements;
namely, the external and static classes.
• External variables may be declared outside any function block
in a source code file the same way any other variable is
declared; by specifying its type and name. No storage class
specifier is used - the position of the declaration within the
file indicates external storage class.
KIIT 2014
External Variable
• Memory for such variables is allocated when the program
begins execution, and remains allocated until the program
terminates. For most C implementations, every byte of
memory allocated for an external variable is initialized to zero.
• The scope of external variables is global, i.e. the entire source
code in the file following the declarations.
#include<conio.h>
#include<stdio.h>
int a;
void main(){
extern int b;
printf(“%d %d”,a,b)
}
int b=10;
KIIT 2014
Static Variable
• External variables have global scope across the entire program
(provided extern declarations are used is files other than
where the variable is defined), and a lifetime over the the
entire program run.
• The storage class, static, similarly provides a lifetime over the
entire program, however; it provides a way to limit the scope
of such variables, Static storage class is declared with the
keyword static as the class specifier when the variable is
defined.
• These variables are automatically initialized to zero upon
memory allocation just as external variables are. Static storage
class can be specified for automatic as well as external
variables.
KIIT 2014
Static Variable
• Static automatic variables continue to exist even after the
block in which they are defined terminates. Thus, the value of
a static variable in a function is retained between repeated
function calls to the same function.
• The scope of static automatic variables is identical to that of
automatic variables, i.e. it is local to the block in which it is
defined; however, the storage allocated becomes permanent
for the duration of the program in the file following the
declarations.
KIIT 2014
Static Variable
#include <stdio.h>
void func(void);
static int count = 5;
main()
{
while(count--)
{
func();
} return 0;
}
void func( void )
{
static int i = 5;
i++;
printf("i is %d and count is %d\n", i,
count);
}
KIIT 2014
Static Variable
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
KIIT 2014
Static Variable
#include <stdio.h>
void Check();
int main()
{
Check();
Check();
Check();
}
void Check()
{
static int c=0;
printf("%d\t",c);
c+=5;
}
0 5 10
KIIT 2014
Summary
In this lesson, you should have learned how to:
• Define storage class
• Define types of storage class
– Automatic storage class
– Register storage class
– Static storage class
– External storage class
• Examples of storage class
KIIT 2014