BESU, SUMMER-07
Topics
 Automatic variables
 External variables
 Static variables
 Register variables
 Scopes and longevity of above types of
variables.
1
BESU, SUMMER-07
1. Scope: the scope of a variable determines over what
part(s) of the program a variable is actually available
for use(active).
2. Longevity: it refers to the period during which a
variables retains a given value during execution of a
program(alive)
3. Local(internal) variables: are those which are declared
within a particular function.
4. Global(external) variables: are those which are
declared outside any function.
2
BESU, SUMMER-07
 Are declare inside a function in which they are to be
utilized.
 Are declared using a keyword auto.
eg. auto int number;
 Are created when the function is called and destroyed
automatically when the function is exited.
 This variable are therefore private(local) to the function
in which they are declared.
 Variables declared inside a function without storage
class specification is, by default, an automatic variable.
3
BESU, SUMMER-07
int main()
{ int m=1000;
function2();
printf(“%dn”,m);
}
function1()
{
int m = 10;
printf(“%dn”,m);
}
function2()
{ int m = 100;
function1();
printf(“%dn”,m);
}
4
Output
10
100
1000
BESU, SUMMER-07
 Any variable local to main will normally live throughout
the whole program, although it is active only in main.
 During recursion, the nested variables are unique auto
variables.
 Automatic variables can also be defined within blocks. In
that case they are meaningful only inside the blocks
where they are declared.
 If automatic variables are not initialized they will contain
garbage.
5
BESU, SUMMER-07
 These variables are declared outside any function.
 These variables are active and alive throughout the entire program.
 Also known as global variables and default value is zero.
 Unlike local variables they are accessed by any function in the
program.
 In case local variable and global variable have the same name, the
local variable will have precedence over the global one.
 Sometimes the keyword extern used to declare these variable.
 It is visible only from the point of declaration to the end of the program.
6
BESU, SUMMER-07
int number;
float length=7.5;
main()
{ . . .
. . .
}
funtion1()
{. . .
. . .
}
funtion1()
{. . .
. . .
}
7
int count;
main()
{count=10;
. . .
. . .
}
funtion()
{int count=0;
. . .
. . .
count=count+1;
}
The variable number and length
are available for use in all three
function
When the function references the
variable count, it will be referencing
only its local variable, not the global
one.
BESU, SUMMER-07
int x;
int main()
{
x=10;
printf(“x=%dn”,x);
printf(“x=%dn”,fun1());
printf(“x=%dn”,fun2());
printf(“x=%dn”,fun3());
}
int fun1()
{ x=x+10;
return(x);
}
int fun2()
{ int x
x=1;
return(x);
}
8
int fun3()
{
x=x+10;
return(x);
}
Once a variable has been declared
global any function can use it and
change its value. The subsequent
functions can then reference only that
new value.
Output
x=10
x=20
x=1
x=30
BESU, SUMMER-07
int
main()
{
y=5;
. . .
. . .
}
int y;
func1()
{
y=y+1
}
9
• As far as main is concerned, y is not
defined. So compiler will issue an error
message.
• There are two way out at this point
1. Define y before main.
2. Declare y with the storage class extern
in main before using it.
BESU, SUMMER-07
int main()
{
extern int y;
. . .
. . .
}
func1()
{
extern int y;
. . .
. . .
}
int y;
10
Note that extern declaration
does not allocate storage
space for variables
BESU, SUMMER-07
int main()
{
extern int m;
int i
. . .
. . .
}
function1()
{
int j;
. . .
. . .
}
11
file1.c
int m;
function2()
{
int i
. . .
. . .
}
function3()
{
int count;
. . .
. . .
}
file2.c
BESU, SUMMER-07
int m;
int main()
{
int i;
. . .
. . .
}
function1()
{
int j;
. . .
. . .
}
12
file1.c
extern int m;
function2()
{
int i
. . .
. . .
}
function3()
{
int count;
. . .
. . .
}
file2.c
BESU, SUMMER-07
 The value of static variables persists until the end of the
program.
 It is declared using the keyword static like
static int x;
static float y;
 It may be of external or internal type depending on the
place of there declaration.
 Static variables are initialized only once, when the
program is compiled.
13
BESU, SUMMER-07
 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.
14
BESU, SUMMER-07
 Internal static variable can be used to count the number of calls
made to function. eg.
int main()
{
int I;
for(i =1; i<=3; i++)
stat();
}
void stat()
{
static int x=0;
x = x+1;
printf(“x = %dn”,x);
}
15
Output
x=1
x=2
x=3
BESU, SUMMER-07
 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.
16
BESU, SUMMER-07
 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)
{
. . .
. . .
}
17
BESU, SUMMER-07
 These variables are stored in one of the machine’s register and are
declared using register keyword.
eg. register int count;
 Since register access are much faster than a memory access
keeping frequently accessed variables in the register lead to faster
execution of program.
 Since only few variable can be placed in the register, it is important
to carefully select the variables for this purpose. However, C will
automatically convert register variables into nonregister variables
once the limit is reached.
 Don’t try to declare a global variable as register. Because the
register will be occupied during the lifetime of the program.
18

More Related Content

PDF
Storage classes arrays & functions in C Language
PPTX
User defined functions in C
PPT
Recursion in c
PPTX
Functions in C
PPTX
Presentation on function
PPTX
Pointer to function 2
PPTX
Pointer to function 1
PPT
memory
Storage classes arrays & functions in C Language
User defined functions in C
Recursion in c
Functions in C
Presentation on function
Pointer to function 2
Pointer to function 1
memory

What's hot (20)

PPT
Prsentation on functions
PPT
User Defined Functions
PPT
Pointers
PPTX
User defined functions
PPTX
predefined and user defined functions
PPT
Lecture 11 - Functions
PPT
Functions in c
PPT
user defined function
PDF
Data Structure with C
PPT
RECURSION IN C
PPT
Lecture 14 - Scope Rules
PDF
Pointers and call by value, reference, address in C
PPT
Function in C Language
PPTX
Pointers
PPTX
Pointers in C/C++ Programming
PDF
11 2. variable-scope rule,-storage_class
PPT
User defined functions in C programmig
PPTX
Function in C program
PPTX
Recursive Function
Prsentation on functions
User Defined Functions
Pointers
User defined functions
predefined and user defined functions
Lecture 11 - Functions
Functions in c
user defined function
Data Structure with C
RECURSION IN C
Lecture 14 - Scope Rules
Pointers and call by value, reference, address in C
Function in C Language
Pointers
Pointers in C/C++ Programming
11 2. variable-scope rule,-storage_class
User defined functions in C programmig
Function in C program
Recursive Function
Ad

Viewers also liked (19)

ODP
prueba
PPTX
10 річок світу
ODP
Presentacion Alonso
PPTX
Star craft 2
PDF
Geo 4-17-08 Notes
PDF
Splunk: Mehr Intelligenz für Ihren IT Service - Kinoforum 2016
PPTX
13 Konsep Beyond Leadership
PPT
Volshebnik izumrudnogo goroda
PPTX
ODP
Presentacion Madrid
PPS
Dot(Goodone)
PDF
สมัครงาน
PPTX
AASP.novembro.15.processoetico
PPTX
L9 gastric carcinoma f
PPTX
El cancer
PPTX
Caso de AESP
PPT
L19 hepatic failure
PPT
Python Κεφ. 1.4 Δομή Επανάληψης
prueba
10 річок світу
Presentacion Alonso
Star craft 2
Geo 4-17-08 Notes
Splunk: Mehr Intelligenz für Ihren IT Service - Kinoforum 2016
13 Konsep Beyond Leadership
Volshebnik izumrudnogo goroda
Presentacion Madrid
Dot(Goodone)
สมัครงาน
AASP.novembro.15.processoetico
L9 gastric carcinoma f
El cancer
Caso de AESP
L19 hepatic failure
Python Κεφ. 1.4 Δομή Επανάληψης
Ad

Similar to storage clas (20)

PDF
Storage Classes.pdf DJJKLF DKFF DSLKF. DSF; FD
PDF
C Programming Storage classes, Recursion
PPT
Storage classes
PPT
S torage class in C
PDF
Function in C++
PPTX
Storage classes in C
PDF
Data structure scope of variables
PPTX
Global variables, sorting static variables,function and arrays,
PPT
Storage classes
DOCX
Programming Global variable
PPTX
Storage classes in c language
PPTX
Storage classes in c language
PPTX
STORAGE CLASS.pptx
PPTX
Storage_classes_and_Scope_rules.pptx
PDF
Starting Out With C++ From Control Structures To Objects 9th Edition Gaddis S...
PPTX
C language presentation
PDF
Latest C Interview Questions and Answers
Storage Classes.pdf DJJKLF DKFF DSLKF. DSF; FD
C Programming Storage classes, Recursion
Storage classes
S torage class in C
Function in C++
Storage classes in C
Data structure scope of variables
Global variables, sorting static variables,function and arrays,
Storage classes
Programming Global variable
Storage classes in c language
Storage classes in c language
STORAGE CLASS.pptx
Storage_classes_and_Scope_rules.pptx
Starting Out With C++ From Control Structures To Objects 9th Edition Gaddis S...
C language presentation
Latest C Interview Questions and Answers

More from teach4uin (20)

PPTX
Controls
PPT
validation
PPT
validation
PPT
Master pages
PPTX
.Net framework
PPT
Scripting languages
PPTX
Css1
PPTX
Code model
PPT
Asp db
PPTX
State management
PPT
security configuration
PPT
static dynamic html tags
PPT
static dynamic html tags
PPTX
New microsoft office power point presentation
PPT
.Net overview
PPT
Stdlib functions lesson
PPT
enums
PPT
array
PPT
Cprogrammingprogramcontrols
PPT
Cprogrammingoperator
Controls
validation
validation
Master pages
.Net framework
Scripting languages
Css1
Code model
Asp db
State management
security configuration
static dynamic html tags
static dynamic html tags
New microsoft office power point presentation
.Net overview
Stdlib functions lesson
enums
array
Cprogrammingprogramcontrols
Cprogrammingoperator

Recently uploaded (20)

PDF
Enhancing emotion recognition model for a student engagement use case through...
PDF
A review of recent deep learning applications in wood surface defect identifi...
PDF
Architecture types and enterprise applications.pdf
PPT
Galois Field Theory of Risk: A Perspective, Protocol, and Mathematical Backgr...
PDF
Hindi spoken digit analysis for native and non-native speakers
PPTX
AI IN MARKETING- PRESENTED BY ANWAR KABIR 1st June 2025.pptx
PDF
Consumable AI The What, Why & How for Small Teams.pdf
PDF
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
PPTX
2018-HIPAA-Renewal-Training for executives
PDF
UiPath Agentic Automation session 1: RPA to Agents
PDF
sustainability-14-14877-v2.pddhzftheheeeee
PDF
Five Habits of High-Impact Board Members
PDF
Abstractive summarization using multilingual text-to-text transfer transforme...
PPT
Module 1.ppt Iot fundamentals and Architecture
PPTX
Final SEM Unit 1 for mit wpu at pune .pptx
PDF
A contest of sentiment analysis: k-nearest neighbor versus neural network
PPTX
Modernising the Digital Integration Hub
PPTX
Microsoft Excel 365/2024 Beginner's training
PDF
Getting started with AI Agents and Multi-Agent Systems
DOCX
search engine optimization ppt fir known well about this
Enhancing emotion recognition model for a student engagement use case through...
A review of recent deep learning applications in wood surface defect identifi...
Architecture types and enterprise applications.pdf
Galois Field Theory of Risk: A Perspective, Protocol, and Mathematical Backgr...
Hindi spoken digit analysis for native and non-native speakers
AI IN MARKETING- PRESENTED BY ANWAR KABIR 1st June 2025.pptx
Consumable AI The What, Why & How for Small Teams.pdf
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
2018-HIPAA-Renewal-Training for executives
UiPath Agentic Automation session 1: RPA to Agents
sustainability-14-14877-v2.pddhzftheheeeee
Five Habits of High-Impact Board Members
Abstractive summarization using multilingual text-to-text transfer transforme...
Module 1.ppt Iot fundamentals and Architecture
Final SEM Unit 1 for mit wpu at pune .pptx
A contest of sentiment analysis: k-nearest neighbor versus neural network
Modernising the Digital Integration Hub
Microsoft Excel 365/2024 Beginner's training
Getting started with AI Agents and Multi-Agent Systems
search engine optimization ppt fir known well about this

storage clas

  • 1. BESU, SUMMER-07 Topics  Automatic variables  External variables  Static variables  Register variables  Scopes and longevity of above types of variables. 1
  • 2. BESU, SUMMER-07 1. Scope: the scope of a variable determines over what part(s) of the program a variable is actually available for use(active). 2. Longevity: it refers to the period during which a variables retains a given value during execution of a program(alive) 3. Local(internal) variables: are those which are declared within a particular function. 4. Global(external) variables: are those which are declared outside any function. 2
  • 3. BESU, SUMMER-07  Are declare inside a function in which they are to be utilized.  Are declared using a keyword auto. eg. auto int number;  Are created when the function is called and destroyed automatically when the function is exited.  This variable are therefore private(local) to the function in which they are declared.  Variables declared inside a function without storage class specification is, by default, an automatic variable. 3
  • 4. BESU, SUMMER-07 int main() { int m=1000; function2(); printf(“%dn”,m); } function1() { int m = 10; printf(“%dn”,m); } function2() { int m = 100; function1(); printf(“%dn”,m); } 4 Output 10 100 1000
  • 5. BESU, SUMMER-07  Any variable local to main will normally live throughout the whole program, although it is active only in main.  During recursion, the nested variables are unique auto variables.  Automatic variables can also be defined within blocks. In that case they are meaningful only inside the blocks where they are declared.  If automatic variables are not initialized they will contain garbage. 5
  • 6. BESU, SUMMER-07  These variables are declared outside any function.  These variables are active and alive throughout the entire program.  Also known as global variables and default value is zero.  Unlike local variables they are accessed by any function in the program.  In case local variable and global variable have the same name, the local variable will have precedence over the global one.  Sometimes the keyword extern used to declare these variable.  It is visible only from the point of declaration to the end of the program. 6
  • 7. BESU, SUMMER-07 int number; float length=7.5; main() { . . . . . . } funtion1() {. . . . . . } funtion1() {. . . . . . } 7 int count; main() {count=10; . . . . . . } funtion() {int count=0; . . . . . . count=count+1; } The variable number and length are available for use in all three function When the function references the variable count, it will be referencing only its local variable, not the global one.
  • 8. BESU, SUMMER-07 int x; int main() { x=10; printf(“x=%dn”,x); printf(“x=%dn”,fun1()); printf(“x=%dn”,fun2()); printf(“x=%dn”,fun3()); } int fun1() { x=x+10; return(x); } int fun2() { int x x=1; return(x); } 8 int fun3() { x=x+10; return(x); } Once a variable has been declared global any function can use it and change its value. The subsequent functions can then reference only that new value. Output x=10 x=20 x=1 x=30
  • 9. BESU, SUMMER-07 int main() { y=5; . . . . . . } int y; func1() { y=y+1 } 9 • As far as main is concerned, y is not defined. So compiler will issue an error message. • There are two way out at this point 1. Define y before main. 2. Declare y with the storage class extern in main before using it.
  • 10. BESU, SUMMER-07 int main() { extern int y; . . . . . . } func1() { extern int y; . . . . . . } int y; 10 Note that extern declaration does not allocate storage space for variables
  • 11. BESU, SUMMER-07 int main() { extern int m; int i . . . . . . } function1() { int j; . . . . . . } 11 file1.c int m; function2() { int i . . . . . . } function3() { int count; . . . . . . } file2.c
  • 12. BESU, SUMMER-07 int m; int main() { int i; . . . . . . } function1() { int j; . . . . . . } 12 file1.c extern int m; function2() { int i . . . . . . } function3() { int count; . . . . . . } file2.c
  • 13. BESU, SUMMER-07  The value of static variables persists until the end of the program.  It is declared using the keyword static like static int x; static float y;  It may be of external or internal type depending on the place of there declaration.  Static variables are initialized only once, when the program is compiled. 13
  • 14. BESU, SUMMER-07  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. 14
  • 15. BESU, SUMMER-07  Internal static variable can be used to count the number of calls made to function. eg. int main() { int I; for(i =1; i<=3; i++) stat(); } void stat() { static int x=0; x = x+1; printf(“x = %dn”,x); } 15 Output x=1 x=2 x=3
  • 16. BESU, SUMMER-07  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. 16
  • 17. BESU, SUMMER-07  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) { . . . . . . } 17
  • 18. BESU, SUMMER-07  These variables are stored in one of the machine’s register and are declared using register keyword. eg. register int count;  Since register access are much faster than a memory access keeping frequently accessed variables in the register lead to faster execution of program.  Since only few variable can be placed in the register, it is important to carefully select the variables for this purpose. However, C will automatically convert register variables into nonregister variables once the limit is reached.  Don’t try to declare a global variable as register. Because the register will be occupied during the lifetime of the program. 18