You are on page 1of 11

Chapter 5: Function

Storage Class
• Storage classes are used to determine the
scope and lifetime of a variable.
• In computer, two types of storage are
available
– Memory
– CPU Registers
• Four types of storage classes:
– Auto
– Register
– Static
– Extern
Auto Storage Class
• Auto is the default storage class for local variable
• For example:
void abc()
{
int a;
auto int b;
}
• Here b is local variable to abc() .
• Auto variables are created when the function is
called and destroy automatically when function finish
its execution.
• Features:
– Storage: Memory
– Scope: local to the function or block in which it is
defined
– Life: Till the control remains within a function.
– Default Value: Garbage Value
Register Storage Class
• Default storage class for local variable.
• It is stored in a CPU register instead of memory.
• Maximum size for storage is equal to register size.
• Generally used for variables which required quick
access
• For example: count++
• Features:
– Storage: CPU Register
– Scope: local to the function or block in which it is
defined
– Life: Till the control remains within a function.
– Default Value: Garbage Value
Static Storage class
• Default storage class for global variables
• For example:
#include<stdio.h>
static int a;
int b;
void main()
{
printf(“a=%d \n b= %d”,a,b);
}
• Static can also be defined as a local variables
• Features:
– Storage: Memory
– Scope: local or global
– Life: Persist between different function calls
– Default Value: zero
Extern Storage Class
• Default storage class for global variables and
can be access by any functions
• Declared outside functions
• For example:
int a=0;
extern int b;
void main()
{
extern int c;
int d;
• Features:
– Storage: Memory
– Scope: Global
– Life: Throughout the program
– Default Value: zero

You might also like