SlideShare a Scribd company logo
Storage Class:
'Storage' refers to the scope of a variable and memory allocated by compiler to store
that variable. Scope of a variable is the boundary within which a variable can be used.
Storage class defines the scope and lifetime of a variable.
From the point view of C compiler, a variable name identifies physical location from
a computer where variable is stored. There are two memory locations in a computer system
where variables are stored as : Memory and CPU Registers.
Functions of storage class :
To detemine the location of a variable where it is stored ?
Set initial value of a variable or if not specified then setting it to default value.
Defining scope of a variable.
To determine the life of a variable.
Types of Storage Classes :
Storage classes are categorised in 4 (four) types as,
 Automatic Storage Class
 Register Storage Class
 Static Storage Class
 External Storage Class
Automatic Storage Class :
oKeyword : auto
oStorage Location : Main memory
oInitial Value : Garbage Value
oLife : Control remains in a block where it is defined.
oScope : Local to the block in which variable is declared.
Syntax :
auto [data_type] [variable_name];
Example :
auto int a;
Program :
/* Program to demonstrate automatic storage class.
#include <stdio.h>
#include <conio.h>
void main()
{
auto int i=10; Output
clrscr(); 20
{ 10
auto int i=20;
printf("nt %d",i);
}
printf("nnt %d",i);
getch();
}
Example 2
int main()
{ int m=1000;
function2();
printf(“%dn”,m);
}
function1()
{
int m = 10;
printf(“%dn”,m);
}
function2()
{ int m = 100;
function1();
printf(“%dn”,m);
}
Output
10
100
1000
Register
o Keyword : register
o Storage Location : CPU Register
o Initial Value : Garbage
o Life : Local to the block in which variable is declared.
o Scope : Local to the block.
Syntax :
register [data_type] [variable_name];
Example :
register int a;
When the calculations are done in CPU, then the value of variables are transferred
from main memory to CPU. Calculations are done and the final result is sent back to main
memory. This leads to slowing down of processes.
Register variables occur in CPU and value of that register variable is stored in a
register within that CPU. Thus, it increases the resultant speed of operations. There is no
waste of time, getting variables from memory and sending it to back again.
It is not applicable for arrays, structures or pointers.
It cannot not used with static or external storage class.
Unary and address of (&) cannot be used with these variables as explicitly or implicitly.
Program :
/* Program to demonstrate register storage class.
#include <stdio.h>
#include <conio.h>
void main()
{
register int i=10;
clrscr();
{
register int i=20;
printf("nt %d",i);
}
printf("nnt %d",i);
getch();
}
Output
20
10
Static Storage Class :
o Keyword : static
o Storage Location : Main memory
o Initial Value : Zero and can be initialize once only.
o Life : depends on function calls and the whole application or program.
o Scope : Local to the block.
Syntax :
static [data_type] [variable_name];
Example :
static int a;
There are two types of static variables as :
a) Local Static Variable
b) Global Static Variable
Static storage class can be used only if we want the value of a variable to persist between
different function calls.
Program :
/* Program to demonstrate static storage class.
#include <stdio.h>
#include <conio.h>
void main()
{
int i;
void incre(void);
clrscr();
for (i=0; i<3; i++)
incre();
getch();
}
void incre(void)
{
int avar=1;
static int svar=1;
avar++;
svar++;
printf("nn Automatic variable value : %d",avar);
printf("t Static variable value : %d",svar);
}
Output :
Automatic variable value : 2 Static Variable Value :2
Automatic variable value : 2 Static Variable Value :3
Automatic variable value : 2 Static Variable Value :4
Internal static variable
• Are those which are declared inside a function.
• Scope of Internal static variables extend upto the end of the program in which they
are defined.
• Internal static variables are almost same as auto variable except they remain in
existence (alive) throughout the remainder of the program.
• Internal static variables can be used to retain values between function calls.
External static variables
• An external static variable is declared outside of all functions and is available to all
the functions in the program.
• An external static variable seems similar simple external variable but their difference
is that static external variable is available only within the file where it is defined while
simple external variable can be accessed by other files.
Static function
Static declaration can also be used to control the scope of a function.
• If you want a particular function to be accessible only to the functions in the file in
which it is defined and not to any function in other files, declare the function to be
static. eg.
static int power(int x inty)
{
. . .
. . .
}
External Storage Class :
o Keyword : extern
o Storage Location : Main memory
o Initial Value : Zero
o Life : Until the program ends.
o Scope : Global to the program.
Syntax :
extern [data_type] [variable_name];
Example :
extern int a;
The variable access time is very fast as compared to other storage classes. But few registers
are available for user programs.
The variables of this class can be referred to as 'global or external variables.' They are
declared outside the functions and can be invoked at anywhere in a program.
• Don’t try to declare a global variable as register. Because the register will be occupied
during the lifetime of the program.
Program :
/* Program to demonstrate external storage class.
#include <stdio.h>
#include <conio.h>
extern int i=10;
void main()
{
int i=20;
void show(void);
clrscr();
printf("nt %d",i); Output
show(); 20
getch(); 10
}
void show(void)
{
printf("nnt %d",i);
}

More Related Content

PPT
STORAGE CLASSES
PPTX
11 lec 11 storage class
PPTX
Storage Class in C Progrmming
PPTX
Storage Class Specifiers in C++
PPTX
Storage classes in C
PPTX
Storage class in c
PPTX
Storage class
PPT
Storage classes
STORAGE CLASSES
11 lec 11 storage class
Storage Class in C Progrmming
Storage Class Specifiers in C++
Storage classes in C
Storage class in c
Storage class
Storage classes

What's hot (20)

PPT
storage class
PPTX
Storage classes in C
PPTX
Storage classes in c++
PDF
Storage class
DOC
What is storage class
PPT
Storage classes
PPTX
Storage classes
PPTX
Storage class in C Language
PPTX
Storage Classes and Functions
PPTX
Storage classes in c language
PPT
Storage classes
PPTX
Storage classes
PPT
Functions in c
PPTX
Storage class
PDF
Storage classes arrays & functions in C Language
PDF
JavaScript Execution Context
PPT
Presentation mcrl2
PPTX
Loops in R
PDF
11 2. variable-scope rule,-storage_class
PDF
Algorithm and Programming (Branching Structure)
storage class
Storage classes in C
Storage classes in c++
Storage class
What is storage class
Storage classes
Storage classes
Storage class in C Language
Storage Classes and Functions
Storage classes in c language
Storage classes
Storage classes
Functions in c
Storage class
Storage classes arrays & functions in C Language
JavaScript Execution Context
Presentation mcrl2
Loops in R
11 2. variable-scope rule,-storage_class
Algorithm and Programming (Branching Structure)
Ad

Similar to Storage class (20)

PPTX
Storage_classes_and_Scope_rules.pptx
PDF
Latest C Interview Questions and Answers
PDF
Advanced C Programming Notes
PPTX
Storage classes in c language
PDF
Unit iii
PPT
Storage classes
DOC
Storage classess of C progamming
PPT
Lecture 13 - Storage Classes
PPTX
visiblity and scope.pptx
DOC
5.program structure
PPTX
STORAGE CLASS.pptx
PDF
C Programming Storage classes, Recursion
PDF
Data structure week 1
PDF
Data structure scope of variables
PPTX
C concepts and programming examples for beginners
DOC
C notes diploma-ee-3rd-sem
PPTX
C language updated
PPTX
C Programming and Data structure C Programming and Data structures
PPTX
cs3362 c programming and data structures
PPTX
Ch10 Program Organization
Storage_classes_and_Scope_rules.pptx
Latest C Interview Questions and Answers
Advanced C Programming Notes
Storage classes in c language
Unit iii
Storage classes
Storage classess of C progamming
Lecture 13 - Storage Classes
visiblity and scope.pptx
5.program structure
STORAGE CLASS.pptx
C Programming Storage classes, Recursion
Data structure week 1
Data structure scope of variables
C concepts and programming examples for beginners
C notes diploma-ee-3rd-sem
C language updated
C Programming and Data structure C Programming and Data structures
cs3362 c programming and data structures
Ch10 Program Organization
Ad

Recently uploaded (20)

PPTX
web development for engineering and engineering
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PPTX
Lecture Notes Electrical Wiring System Components
PPTX
Sustainable Sites - Green Building Construction
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
Construction Project Organization Group 2.pptx
PPT
Mechanical Engineering MATERIALS Selection
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
Welding lecture in detail for understanding
DOCX
573137875-Attendance-Management-System-original
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
OOP with Java - Java Introduction (Basics)
PDF
PPT on Performance Review to get promotions
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
web development for engineering and engineering
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
Lecture Notes Electrical Wiring System Components
Sustainable Sites - Green Building Construction
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Construction Project Organization Group 2.pptx
Mechanical Engineering MATERIALS Selection
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Welding lecture in detail for understanding
573137875-Attendance-Management-System-original
Foundation to blockchain - A guide to Blockchain Tech
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Operating System & Kernel Study Guide-1 - converted.pdf
OOP with Java - Java Introduction (Basics)
PPT on Performance Review to get promotions
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf

Storage class

  • 1. Storage Class: 'Storage' refers to the scope of a variable and memory allocated by compiler to store that variable. Scope of a variable is the boundary within which a variable can be used. Storage class defines the scope and lifetime of a variable. From the point view of C compiler, a variable name identifies physical location from a computer where variable is stored. There are two memory locations in a computer system where variables are stored as : Memory and CPU Registers. Functions of storage class : To detemine the location of a variable where it is stored ? Set initial value of a variable or if not specified then setting it to default value. Defining scope of a variable. To determine the life of a variable. Types of Storage Classes : Storage classes are categorised in 4 (four) types as,  Automatic Storage Class  Register Storage Class  Static Storage Class  External Storage Class Automatic Storage Class : oKeyword : auto oStorage Location : Main memory oInitial Value : Garbage Value oLife : Control remains in a block where it is defined. oScope : Local to the block in which variable is declared. Syntax : auto [data_type] [variable_name]; Example : auto int a; Program : /* Program to demonstrate automatic storage class. #include <stdio.h> #include <conio.h> void main() { auto int i=10; Output clrscr(); 20 { 10 auto int i=20; printf("nt %d",i); } printf("nnt %d",i); getch(); } Example 2 int main() { int m=1000; function2(); printf(“%dn”,m); }
  • 2. function1() { int m = 10; printf(“%dn”,m); } function2() { int m = 100; function1(); printf(“%dn”,m); } Output 10 100 1000 Register o Keyword : register o Storage Location : CPU Register o Initial Value : Garbage o Life : Local to the block in which variable is declared. o Scope : Local to the block. Syntax : register [data_type] [variable_name]; Example : register int a; When the calculations are done in CPU, then the value of variables are transferred from main memory to CPU. Calculations are done and the final result is sent back to main memory. This leads to slowing down of processes. Register variables occur in CPU and value of that register variable is stored in a register within that CPU. Thus, it increases the resultant speed of operations. There is no waste of time, getting variables from memory and sending it to back again. It is not applicable for arrays, structures or pointers. It cannot not used with static or external storage class. Unary and address of (&) cannot be used with these variables as explicitly or implicitly. Program : /* Program to demonstrate register storage class. #include <stdio.h> #include <conio.h> void main() { register int i=10; clrscr(); { register int i=20; printf("nt %d",i); } printf("nnt %d",i); getch(); } Output
  • 3. 20 10 Static Storage Class : o Keyword : static o Storage Location : Main memory o Initial Value : Zero and can be initialize once only. o Life : depends on function calls and the whole application or program. o Scope : Local to the block. Syntax : static [data_type] [variable_name]; Example : static int a; There are two types of static variables as : a) Local Static Variable b) Global Static Variable Static storage class can be used only if we want the value of a variable to persist between different function calls. Program : /* Program to demonstrate static storage class. #include <stdio.h> #include <conio.h> void main() { int i; void incre(void); clrscr(); for (i=0; i<3; i++) incre(); getch(); } void incre(void) { int avar=1; static int svar=1; avar++; svar++; printf("nn Automatic variable value : %d",avar); printf("t Static variable value : %d",svar); } Output : Automatic variable value : 2 Static Variable Value :2 Automatic variable value : 2 Static Variable Value :3 Automatic variable value : 2 Static Variable Value :4 Internal static variable • Are those which are declared inside a function. • Scope of Internal static variables extend upto the end of the program in which they are defined. • Internal static variables are almost same as auto variable except they remain in existence (alive) throughout the remainder of the program. • Internal static variables can be used to retain values between function calls.
  • 4. External static variables • An external static variable is declared outside of all functions and is available to all the functions in the program. • An external static variable seems similar simple external variable but their difference is that static external variable is available only within the file where it is defined while simple external variable can be accessed by other files. Static function Static declaration can also be used to control the scope of a function. • If you want a particular function to be accessible only to the functions in the file in which it is defined and not to any function in other files, declare the function to be static. eg. static int power(int x inty) { . . . . . . } External Storage Class : o Keyword : extern o Storage Location : Main memory o Initial Value : Zero o Life : Until the program ends. o Scope : Global to the program. Syntax : extern [data_type] [variable_name]; Example : extern int a; The variable access time is very fast as compared to other storage classes. But few registers are available for user programs. The variables of this class can be referred to as 'global or external variables.' They are declared outside the functions and can be invoked at anywhere in a program. • Don’t try to declare a global variable as register. Because the register will be occupied during the lifetime of the program. Program : /* Program to demonstrate external storage class. #include <stdio.h> #include <conio.h> extern int i=10; void main() { int i=20; void show(void); clrscr(); printf("nt %d",i); Output show(); 20 getch(); 10 } void show(void) { printf("nnt %d",i); }