SlideShare a Scribd company logo
-Reddhi Sekhar Basu (559)
Storage Class Specifiers in C++
Every C++ variable has a storage class and a
scope.
The storage class determines the part of the
memory where storage is allocated for an
object and how long the storage allocation
continues to exist.
It also determines the scope(visibility )which
specifies the part of the program over which
the variable name is visible i.e. the variable is
accessible by name.
Lifetime of a variable/function or program is
the time during which the variable/function or
program holds memory space.
 Automatic i.e. auto
 Register
 Static
 External i.e. extern
A storage class specifier is used to refine the declaration
of a variable, a function, and parameters.
The choice of storage class affects a variable’s storage
duration, scope, and linkage.
The storage duration of a variable is the portion of
program execution during which storage for the variable
exists.
The linkage of a variable determines whether it may be
shared by more than one file in the same program.
Scope
Block
Scope
Function
Scope
Program
Scope
In this section, a block refers to any sets of statements enclosed
in braces ({ and }). A variable declared within a block has block
scope. Thus, the variable is active and accessible from its
declaration point to the end of the block. Sometimes, block
scope is also called local scope.
For example, the variable i declared within the block of the
following main function has block scope:
int main()
{
int i; /* block scope */
.
.
.
return 0;
}
 Usually, a variable with block scope is called a local variable.
Function scope indicates that a variable is active and visible from the beginning to the end
of a function.
In C, only the goto label has function scope. For example, the goto label, start, shown in
the following code portion has function scope:
int main()
{
int i; /* block scope */
.
.
start: /* A goto label has function scope */
.
.
goto start; /* the goto statement */
.
.
return 0;
}
Here the label start is visible from the beginning to the end of the main() function.
Therefore, there should not be more than one label having the same name within the
main() function.
A variable is said to have program scope when it is declared
outside a function. For instance, look at the following code:
int x = 0; /* program scope */
float y = 0.0; /* program scope */
int main()
{
int i; /* block scope */
.
.
return 0;
}
Here the int variable x and the float variable y have program
scope.
Variables with program scope are also called global variables,
which are visible among different files. These files are the
entire source files that make up an executable program. Note
that a global variable is declared with an initializer outside a
function.
Automatic :Storage for the variable
is allocated when the surrounding
block is executed; storage is
deallocated when the block
terminates, causing the variable to
lose its value.
• auto
• register
Static : The variable has a
permanent storage location, hence it
retains its value throughout the
execution of the program.
• static
• extern
External
• The same
variable may
be shared by
several files
Internal
• The variable
is restricted
to a single
file
Storage Class Specifiers in C++
 The auto storage class specifier lets you
explicitly declare a variable with automatic
storage.
 The auto storage class is the default for
variables declared inside a block. A variable x
that has automatic storage is deleted when
the block in which x was declared exits.
 Until C++11, auto had the semantic of a
storage duration specifier.
 Syntax
• auto variable initializer
• auto function -> return type
 You can only apply the auto storage class
specifier to names of variables declared in a
block or to names of function parameters.
However, these names by default have
automatic storage. Therefore the storage
class specifier auto is usually redundant in a
data declaration.
 Specifies that the type of the variable that is
being declared will be automatically deduced
from its initializer. For functions, specifies that
the return type is a trailing return type.
 You can initialize any auto variable except
parameters. If you do not explicitly initialize an
automatic object, its value is indeterminate. If you
provide an initial value, the expression
representing the initial value can be any valid C or
C++ expression. The object is then set to that
initial value each time the program block that
contains the object's definition is entered.
 If automatic variables are not initialized they will
contain garbage.
 Note that if you use the goto statement to jump
into the middle of a block, automatic variables
within that block are not initialized.
 Objects with the auto storage class specifier have
automatic storage duration. Each time a block is
entered, storage for auto objects defined in that
block is made available. When the block is
exited, the objects are no longer available for use.
An object declared with no linkage specification
and without the static storage class specifier has
automatic storage duration.
 If an auto object is defined within a function that is
recursively invoked, memory is allocated for the
object at each invocation of the block.
Storage Class Specifiers in C++
 The word register is borrowed from the world of
computer hardware. Each computer has a certain
number of registers to hold data and perform
arithmetic or logical calculations. Because
registers are located within the CPU (central
processing unit) chip, it's much quicker to access a
register than a memory location. Therefore, storing
variables in registers may help to speed up your
program.
 You can apply this specifier to variables when you
think it's necessary to put the variables into the
computer registers.
 However, the register specifier only gives the
compiler a suggestion. In other words, a variable
specified by the register keyword is not guaranteed
to be stored in a register. The compiler can ignore
the suggestion if there is no register available, or if
some other restrictions have to apply.
 Because of the limited size and number of
registers available on most systems, few variables
can actually be put in registers. If the compiler
does not allocate a machine register for a register
object, the object is treated as having the storage
class specifier auto.
 An object having the register storage class
specifier must be defined within a block or
declared as a parameter to a function.
 You can initialize any register object except
parameters. If you do not initialize an
automatic object, its value is indeterminate. If
you provide an initial value, the expression
representing the initial value can be any valid
C or C++ expression. The object is then set to
that initial value each time the program block
that contains the object's definition is entered.
 Objects with the register storage class
specifier have automatic storage duration.
Each time a block is entered, storage for
register objects defined in that block is made
available. When the block is exited, the
objects are no longer available for use.
 If a register object is defined within a function
that is recursively invoked, memory is
allocated for the variable at each invocation of
the block.
 Loop Control Variables:
int main()
{
/* block scope with the register specifier */
register int i;
. . .
for (i=0; i<MAX_NUM; i++){
/* some statements */
}
. . .
return 0;
}
 NOTE: The declaration of ‘i’ suggests that the compiler store the
variable in a register. Because ‘i’ is intensively used in the for loop,
storing ‘i’ in a register may increase the speed of the code shown
here.
IN C LANGUAGE IN C++ LANGUAGE
 The register storage class
specifier is legal only for
variables declared in a
block. You cannot use it in
global scope data
declarations.
 A register does not have an
address. Therefore, you
cannot apply the address
operator (&) to a register
variable.
 You cannot use the register
storage class specifier when
declaring objects in
namespace scope.
 Unlike C, C++ lets you take
the address of an object with
the register storage class.
For example:
register int i;
int* b = &i; // valid in C++,
but not in C
Storage Class Specifiers in C++
The mutable storage class specifier is
available only in C++
It is used only on a class data member to
make it modifiable even though the
member is part of an object declared as
const.
class A
{
public:
A() : x(4), y(5) { };
mutable int x;
int y;
};
int main()
{
const A var2;
var2.x = 345;
// var2.y = 2345;
}
In this example, the compiler would not allow the assignment var2.y
= 2345 because var2 has been declared as const. The compiler will
allow the assignment var2.x = 345 because A::x has been declared
as mutable.
Storage Class Specifiers in C++
The ‘static’ storage class has the same visibility that of the ‘automatic’ local
variable.
It doesn’t come into existence until the first call to the function containing
it.Thereafter it remains in existence for the life of the program.
It basically instructs the compiler to keep a local variable in existence during the
lifetime of the program instead of creating and destroying it each time it comes
into and goes out of scope.
Therefore ‘static’ modifiers are used when it’s necessary to remember a value
when it is not being executed ; that is between calls to the function.
Making local variables ‘static’ allows them to maintain their values between
function calls.
The ‘static’ modifier may also be applied to global variables. When this is done,
it causes that variable's scope to be restricted to the file in which it is declared.
In C++, when static is used on a class data member, it causes only one copy of
that member to be shared by all objects of its class.
#include <iostream>
#include<conio.h>
using namespace std;
static int cost = 10; /* Global variable */
class c
{
public :
static int i=5;
void func()
{
cout<<"i is "<<i<<endl;
i--;
}
void func2()
{ cout<<“ cost is " <<cost<<endl;
cost--;
}
class d
{
public :
void disp()
{
cout<<"cost is " <<cost<<endl;
}
};
main()
{
c ob; d ob1;
while( ob.i!=0)
ob.func();
ob.func2();
cost-=5;
ob1.disp();
getch();
}
i is 5
i is 4
i is 3
i is 2
i is 1
i is 0
cost is 10
cost is 4.
Storage Class Specifiers in C++
Unlike local variables global variables are declared
outside of any function or class. So a global
variable is visible to all the functions in a file.
More precisely global variables are visible to only
those functions in the file that follow the variable’s
definition in listing.
Usually we want global variables to be visible to all
functions or classes in the file so we put their
definition at the beginning of the listing.
file1
#include<iostream>
#include<conio.h>
using namespace std;
int i; // initialized to 0 by default as global
extern void write_extern(); //to be discussed little later
class c
{ public :
void disp()
{ cout<<“The value of i is “<<i<<endl;
i++;
}
};
class d
{ public :
void disp1()
{ cout<<“The value of i is “<<i<<endl;
}
};
main()
{ c ob; d ob1;
ob.disp();
i++;
ob1.disp1();
write_extern();
getch();
}
Output :
The value of i is 0
The value of i is 2
Note :
• Here the first thing to be noted is that variable i, declared at the
very beginning right after import ing the header files. This is done
so that i is visible to all the classes or functions under the same
file.
• i is declared though but not initialized by any value as global
variables get automatically initialized to 0 if not explicitly done.
• Lastly as said earlier if i here was specified following class c but
before class d then it would be visible in class d & main() function
but NOT in class c since global variables are visible to functions
or classes that follow the variable’s definition in listing.
Lifetime & visibility :
Global variables are automatically given the static storage class
specifier during their definition. So they exist for the life of the
program.
Memory space is set aside for them when the program begins
and continues to exist until the program ends.
The key word ‘extern’ basically extends the scope of a global
variable. ( Global variables are ‘extern’ by default.)
 It is used to give a reference of a global variable that is visible to
ALL the program files.
 When ‘extern’ precedes a variable, it cannot be initialized as all it
does is point the variable name at a storage location that has
been previously defined.
 When we have multiple files and we define a global variable or
function which will be used in other files also or say the program
is spread across two or more files, then ‘extern’ will be used to
give reference of defined variable or function. So basically
‘extern’ is used to declare a global variable or function in other
files.
 The extern modifier is most commonly used when there are two
or more files sharing the same global variables or functions .
A small demonstration is given from the previous
example to help understand the concept better :
file2
#include<iostream>
#include<conio.h>
using namespace std;
extern int i; //..here the extern keyword is used to declare i in this
file and it refers to g.v. i declared in the previous file…
class a
{
public :
void write_extern()
{ cout<<“The value of i is “<<i<<endl;
}
};
Note : It is because of the ‘extern’ specifier that variable i could
be used in this file & void write_extern() in the previous
file.
Static local Global
Visibility function file
Lifetime program program
Initialized value 0 0
Purpose Same as local but
retains value
when function
terminates.
Variables used by
several classes &
functions but
inside the same
file.
Comparison between Static local and global :
Books:
1. Robert Lafore
2. Herbert Schildt
 Websites:
1. Hawaii.edu
2. Tutorialspoint.com
Storage Class Specifiers in C++

More Related Content

What's hot (20)

PPT
Input and output in C++
Nilesh Dalvi
 
PDF
C++ Files and Streams
Ahmed Farag
 
PPTX
Segmentation in Operating Systems.
Muhammad SiRaj Munir
 
PPTX
Dynamic memory allocation in c++
Tech_MX
 
PPTX
File handling in C
Rabin BK
 
PPT
Multidimensional array in C
Smit Parikh
 
PPT
Chapter1 c programming data types, variables and constants
vinay arora
 
PPTX
Array of objects.pptx
RAGAVIC2
 
PPT
File handling in C++
Hitesh Kumar
 
PPT
Enumerated data types in C
Arpana shree
 
PPTX
sSCOPE RESOLUTION OPERATOR.pptx
Nidhi Mehra
 
PPTX
Storage class
Joy Forerver
 
PPT
Class and object in C++
rprajat007
 
PDF
Action Bar in Android
Prof. Erwin Globio
 
PDF
CONST-- once initialised, no one can change it
Ajay Chimmani
 
PPTX
Floating point arithmetic operations (1)
cs19club
 
PPTX
Associative Memory in Computer architecture
pritheeshg03
 
PPT
Operation on string presentation
Aliul Kadir Akib
 
DOCX
Encapsulation in C++
Hitesh Kumar
 
PPTX
Storage classes in c++
Jaspal Singh
 
Input and output in C++
Nilesh Dalvi
 
C++ Files and Streams
Ahmed Farag
 
Segmentation in Operating Systems.
Muhammad SiRaj Munir
 
Dynamic memory allocation in c++
Tech_MX
 
File handling in C
Rabin BK
 
Multidimensional array in C
Smit Parikh
 
Chapter1 c programming data types, variables and constants
vinay arora
 
Array of objects.pptx
RAGAVIC2
 
File handling in C++
Hitesh Kumar
 
Enumerated data types in C
Arpana shree
 
sSCOPE RESOLUTION OPERATOR.pptx
Nidhi Mehra
 
Storage class
Joy Forerver
 
Class and object in C++
rprajat007
 
Action Bar in Android
Prof. Erwin Globio
 
CONST-- once initialised, no one can change it
Ajay Chimmani
 
Floating point arithmetic operations (1)
cs19club
 
Associative Memory in Computer architecture
pritheeshg03
 
Operation on string presentation
Aliul Kadir Akib
 
Encapsulation in C++
Hitesh Kumar
 
Storage classes in c++
Jaspal Singh
 

Viewers also liked (20)

PPTX
11 lec 11 storage class
kapil078
 
PPTX
Storage Classes and Functions
Jake Bond
 
PPT
Storage classes
Leela Koneru
 
PPTX
Storage class in c
kash95
 
PPTX
Mutable and immutable classes
Tech_MX
 
PPTX
Storage classes in C
Nitesh Bichwani
 
PPTX
Variables, Data Types, Operator & Expression in c in detail
gourav kottawar
 
PPTX
#OOP_D_ITS - 8th - Class Diagram
Hadziq Fabroyir
 
PPTX
Five components of communication
jumar dimas
 
PPTX
Array in c++
Mahesha Mano
 
PPTX
class c++
vinay chauhan
 
PPT
Structure of C++ - R.D.Sivakumar
Sivakumar R D .
 
PPT
Structure in c
Prabhu Govind
 
PDF
Pointer in c++ part1
Subhasis Nayak
 
PPT
C Structures & Unions
Ram Sagar Mourya
 
PPTX
Communication skills week 4
Wardah Azhar
 
PPTX
C++ loop
Khelan Ameen
 
DOCX
Notes on c++
Selvam Edwin
 
PPTX
Loops c++
Shivani Singh
 
11 lec 11 storage class
kapil078
 
Storage Classes and Functions
Jake Bond
 
Storage classes
Leela Koneru
 
Storage class in c
kash95
 
Mutable and immutable classes
Tech_MX
 
Storage classes in C
Nitesh Bichwani
 
Variables, Data Types, Operator & Expression in c in detail
gourav kottawar
 
#OOP_D_ITS - 8th - Class Diagram
Hadziq Fabroyir
 
Five components of communication
jumar dimas
 
Array in c++
Mahesha Mano
 
class c++
vinay chauhan
 
Structure of C++ - R.D.Sivakumar
Sivakumar R D .
 
Structure in c
Prabhu Govind
 
Pointer in c++ part1
Subhasis Nayak
 
C Structures & Unions
Ram Sagar Mourya
 
Communication skills week 4
Wardah Azhar
 
C++ loop
Khelan Ameen
 
Notes on c++
Selvam Edwin
 
Loops c++
Shivani Singh
 
Ad

Similar to Storage Class Specifiers in C++ (20)

PPTX
Storage Class Specifiers
Reddhi Basu
 
PPTX
Storage classes
UTTAM VERMA
 
DOC
What is storage class
Isha Aggarwal
 
PPTX
Storage classes
priyanka jain
 
PDF
Function in C++
Prof Ansari
 
DOC
Storage classess of C progamming
Appili Vamsi Krishna
 
PDF
Latest C Interview Questions and Answers
DaisyWatson5
 
PPT
STORAGE CLASSES
sathish sak
 
PPT
Lecture 13 - Storage Classes
Md. Imran Hossain Showrov
 
DOCX
Storage class
Kalaikumar Thangapandi
 
PPT
Functions in c
SunithaVesalpu
 
DOC
5.program structure
Shankar Gangaju
 
PDF
Advanced C Programming Notes
Leslie Schulte
 
PPTX
STORAGE CLASS.pptx
BU210535JeevanKishor
 
PPTX
Storage classes in C
Self employed
 
PPTX
Storage classes in c language
tanmaymodi4
 
PPTX
Storage classes in c language
Tanmay Modi
 
PPT
Storage classes
Praveen M Jigajinni
 
PPT
Storage classes
Leela Koneru
 
PDF
Chapter 11 Function
Deepak Singh
 
Storage Class Specifiers
Reddhi Basu
 
Storage classes
UTTAM VERMA
 
What is storage class
Isha Aggarwal
 
Storage classes
priyanka jain
 
Function in C++
Prof Ansari
 
Storage classess of C progamming
Appili Vamsi Krishna
 
Latest C Interview Questions and Answers
DaisyWatson5
 
STORAGE CLASSES
sathish sak
 
Lecture 13 - Storage Classes
Md. Imran Hossain Showrov
 
Storage class
Kalaikumar Thangapandi
 
Functions in c
SunithaVesalpu
 
5.program structure
Shankar Gangaju
 
Advanced C Programming Notes
Leslie Schulte
 
STORAGE CLASS.pptx
BU210535JeevanKishor
 
Storage classes in C
Self employed
 
Storage classes in c language
tanmaymodi4
 
Storage classes in c language
Tanmay Modi
 
Storage classes
Praveen M Jigajinni
 
Storage classes
Leela Koneru
 
Chapter 11 Function
Deepak Singh
 
Ad

Recently uploaded (20)

PPTX
Artificial Intelligence in Gastroentrology: Advancements and Future Presprec...
AyanHossain
 
PPTX
PROTIEN ENERGY MALNUTRITION: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
PPTX
Gupta Art & Architecture Temple and Sculptures.pptx
Virag Sontakke
 
PPTX
Virus sequence retrieval from NCBI database
yamunaK13
 
PPT
DRUGS USED IN THERAPY OF SHOCK, Shock Therapy, Treatment or management of shock
Rajshri Ghogare
 
DOCX
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
PDF
Tips for Writing the Research Title with Examples
Thelma Villaflores
 
PPTX
I INCLUDED THIS TOPIC IS INTELLIGENCE DEFINITION, MEANING, INDIVIDUAL DIFFERE...
parmarjuli1412
 
PDF
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
PPTX
Translation_ Definition, Scope & Historical Development.pptx
DhatriParmar
 
PPTX
ENGLISH 8 WEEK 3 Q1 - Analyzing the linguistic, historical, andor biographica...
OliverOllet
 
PPTX
Introduction to Probability(basic) .pptx
purohitanuj034
 
PDF
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
PPTX
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
PDF
John Keats introduction and list of his important works
vatsalacpr
 
PPTX
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
PPTX
TOP 10 AI TOOLS YOU MUST LEARN TO SURVIVE IN 2025 AND ABOVE
digilearnings.com
 
DOCX
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
 
PPTX
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
PDF
TOP 10 AI TOOLS YOU MUST LEARN TO SURVIVE IN 2025 AND ABOVE
digilearnings.com
 
Artificial Intelligence in Gastroentrology: Advancements and Future Presprec...
AyanHossain
 
PROTIEN ENERGY MALNUTRITION: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
Gupta Art & Architecture Temple and Sculptures.pptx
Virag Sontakke
 
Virus sequence retrieval from NCBI database
yamunaK13
 
DRUGS USED IN THERAPY OF SHOCK, Shock Therapy, Treatment or management of shock
Rajshri Ghogare
 
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
Tips for Writing the Research Title with Examples
Thelma Villaflores
 
I INCLUDED THIS TOPIC IS INTELLIGENCE DEFINITION, MEANING, INDIVIDUAL DIFFERE...
parmarjuli1412
 
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
Translation_ Definition, Scope & Historical Development.pptx
DhatriParmar
 
ENGLISH 8 WEEK 3 Q1 - Analyzing the linguistic, historical, andor biographica...
OliverOllet
 
Introduction to Probability(basic) .pptx
purohitanuj034
 
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
John Keats introduction and list of his important works
vatsalacpr
 
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
TOP 10 AI TOOLS YOU MUST LEARN TO SURVIVE IN 2025 AND ABOVE
digilearnings.com
 
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
 
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
TOP 10 AI TOOLS YOU MUST LEARN TO SURVIVE IN 2025 AND ABOVE
digilearnings.com
 

Storage Class Specifiers in C++

  • 3. Every C++ variable has a storage class and a scope. The storage class determines the part of the memory where storage is allocated for an object and how long the storage allocation continues to exist. It also determines the scope(visibility )which specifies the part of the program over which the variable name is visible i.e. the variable is accessible by name. Lifetime of a variable/function or program is the time during which the variable/function or program holds memory space.
  • 4.  Automatic i.e. auto  Register  Static  External i.e. extern
  • 5. A storage class specifier is used to refine the declaration of a variable, a function, and parameters. The choice of storage class affects a variable’s storage duration, scope, and linkage. The storage duration of a variable is the portion of program execution during which storage for the variable exists. The linkage of a variable determines whether it may be shared by more than one file in the same program.
  • 7. In this section, a block refers to any sets of statements enclosed in braces ({ and }). A variable declared within a block has block scope. Thus, the variable is active and accessible from its declaration point to the end of the block. Sometimes, block scope is also called local scope. For example, the variable i declared within the block of the following main function has block scope: int main() { int i; /* block scope */ . . . return 0; }  Usually, a variable with block scope is called a local variable.
  • 8. Function scope indicates that a variable is active and visible from the beginning to the end of a function. In C, only the goto label has function scope. For example, the goto label, start, shown in the following code portion has function scope: int main() { int i; /* block scope */ . . start: /* A goto label has function scope */ . . goto start; /* the goto statement */ . . return 0; } Here the label start is visible from the beginning to the end of the main() function. Therefore, there should not be more than one label having the same name within the main() function.
  • 9. A variable is said to have program scope when it is declared outside a function. For instance, look at the following code: int x = 0; /* program scope */ float y = 0.0; /* program scope */ int main() { int i; /* block scope */ . . return 0; } Here the int variable x and the float variable y have program scope. Variables with program scope are also called global variables, which are visible among different files. These files are the entire source files that make up an executable program. Note that a global variable is declared with an initializer outside a function.
  • 10. Automatic :Storage for the variable is allocated when the surrounding block is executed; storage is deallocated when the block terminates, causing the variable to lose its value. • auto • register Static : The variable has a permanent storage location, hence it retains its value throughout the execution of the program. • static • extern
  • 11. External • The same variable may be shared by several files Internal • The variable is restricted to a single file
  • 13.  The auto storage class specifier lets you explicitly declare a variable with automatic storage.  The auto storage class is the default for variables declared inside a block. A variable x that has automatic storage is deleted when the block in which x was declared exits.  Until C++11, auto had the semantic of a storage duration specifier.  Syntax • auto variable initializer • auto function -> return type
  • 14.  You can only apply the auto storage class specifier to names of variables declared in a block or to names of function parameters. However, these names by default have automatic storage. Therefore the storage class specifier auto is usually redundant in a data declaration.  Specifies that the type of the variable that is being declared will be automatically deduced from its initializer. For functions, specifies that the return type is a trailing return type.
  • 15.  You can initialize any auto variable except parameters. If you do not explicitly initialize an automatic object, its value is indeterminate. If you provide an initial value, the expression representing the initial value can be any valid C or C++ expression. The object is then set to that initial value each time the program block that contains the object's definition is entered.  If automatic variables are not initialized they will contain garbage.  Note that if you use the goto statement to jump into the middle of a block, automatic variables within that block are not initialized.
  • 16.  Objects with the auto storage class specifier have automatic storage duration. Each time a block is entered, storage for auto objects defined in that block is made available. When the block is exited, the objects are no longer available for use. An object declared with no linkage specification and without the static storage class specifier has automatic storage duration.  If an auto object is defined within a function that is recursively invoked, memory is allocated for the object at each invocation of the block.
  • 18.  The word register is borrowed from the world of computer hardware. Each computer has a certain number of registers to hold data and perform arithmetic or logical calculations. Because registers are located within the CPU (central processing unit) chip, it's much quicker to access a register than a memory location. Therefore, storing variables in registers may help to speed up your program.  You can apply this specifier to variables when you think it's necessary to put the variables into the computer registers.
  • 19.  However, the register specifier only gives the compiler a suggestion. In other words, a variable specified by the register keyword is not guaranteed to be stored in a register. The compiler can ignore the suggestion if there is no register available, or if some other restrictions have to apply.  Because of the limited size and number of registers available on most systems, few variables can actually be put in registers. If the compiler does not allocate a machine register for a register object, the object is treated as having the storage class specifier auto.
  • 20.  An object having the register storage class specifier must be defined within a block or declared as a parameter to a function.  You can initialize any register object except parameters. If you do not initialize an automatic object, its value is indeterminate. If you provide an initial value, the expression representing the initial value can be any valid C or C++ expression. The object is then set to that initial value each time the program block that contains the object's definition is entered.
  • 21.  Objects with the register storage class specifier have automatic storage duration. Each time a block is entered, storage for register objects defined in that block is made available. When the block is exited, the objects are no longer available for use.  If a register object is defined within a function that is recursively invoked, memory is allocated for the variable at each invocation of the block.
  • 22.  Loop Control Variables: int main() { /* block scope with the register specifier */ register int i; . . . for (i=0; i<MAX_NUM; i++){ /* some statements */ } . . . return 0; }  NOTE: The declaration of ‘i’ suggests that the compiler store the variable in a register. Because ‘i’ is intensively used in the for loop, storing ‘i’ in a register may increase the speed of the code shown here.
  • 23. IN C LANGUAGE IN C++ LANGUAGE  The register storage class specifier is legal only for variables declared in a block. You cannot use it in global scope data declarations.  A register does not have an address. Therefore, you cannot apply the address operator (&) to a register variable.  You cannot use the register storage class specifier when declaring objects in namespace scope.  Unlike C, C++ lets you take the address of an object with the register storage class. For example: register int i; int* b = &i; // valid in C++, but not in C
  • 25. The mutable storage class specifier is available only in C++ It is used only on a class data member to make it modifiable even though the member is part of an object declared as const.
  • 26. class A { public: A() : x(4), y(5) { }; mutable int x; int y; }; int main() { const A var2; var2.x = 345; // var2.y = 2345; } In this example, the compiler would not allow the assignment var2.y = 2345 because var2 has been declared as const. The compiler will allow the assignment var2.x = 345 because A::x has been declared as mutable.
  • 28. The ‘static’ storage class has the same visibility that of the ‘automatic’ local variable. It doesn’t come into existence until the first call to the function containing it.Thereafter it remains in existence for the life of the program. It basically instructs the compiler to keep a local variable in existence during the lifetime of the program instead of creating and destroying it each time it comes into and goes out of scope. Therefore ‘static’ modifiers are used when it’s necessary to remember a value when it is not being executed ; that is between calls to the function. Making local variables ‘static’ allows them to maintain their values between function calls. The ‘static’ modifier may also be applied to global variables. When this is done, it causes that variable's scope to be restricted to the file in which it is declared. In C++, when static is used on a class data member, it causes only one copy of that member to be shared by all objects of its class.
  • 29. #include <iostream> #include<conio.h> using namespace std; static int cost = 10; /* Global variable */ class c { public : static int i=5; void func() { cout<<"i is "<<i<<endl; i--; } void func2() { cout<<“ cost is " <<cost<<endl; cost--; }
  • 30. class d { public : void disp() { cout<<"cost is " <<cost<<endl; } }; main() { c ob; d ob1; while( ob.i!=0) ob.func(); ob.func2(); cost-=5; ob1.disp(); getch(); }
  • 31. i is 5 i is 4 i is 3 i is 2 i is 1 i is 0 cost is 10 cost is 4.
  • 33. Unlike local variables global variables are declared outside of any function or class. So a global variable is visible to all the functions in a file. More precisely global variables are visible to only those functions in the file that follow the variable’s definition in listing. Usually we want global variables to be visible to all functions or classes in the file so we put their definition at the beginning of the listing.
  • 34. file1 #include<iostream> #include<conio.h> using namespace std; int i; // initialized to 0 by default as global extern void write_extern(); //to be discussed little later class c { public : void disp() { cout<<“The value of i is “<<i<<endl; i++; } };
  • 35. class d { public : void disp1() { cout<<“The value of i is “<<i<<endl; } }; main() { c ob; d ob1; ob.disp(); i++; ob1.disp1(); write_extern(); getch(); }
  • 36. Output : The value of i is 0 The value of i is 2 Note : • Here the first thing to be noted is that variable i, declared at the very beginning right after import ing the header files. This is done so that i is visible to all the classes or functions under the same file. • i is declared though but not initialized by any value as global variables get automatically initialized to 0 if not explicitly done. • Lastly as said earlier if i here was specified following class c but before class d then it would be visible in class d & main() function but NOT in class c since global variables are visible to functions or classes that follow the variable’s definition in listing. Lifetime & visibility : Global variables are automatically given the static storage class specifier during their definition. So they exist for the life of the program. Memory space is set aside for them when the program begins and continues to exist until the program ends.
  • 37. The key word ‘extern’ basically extends the scope of a global variable. ( Global variables are ‘extern’ by default.)  It is used to give a reference of a global variable that is visible to ALL the program files.  When ‘extern’ precedes a variable, it cannot be initialized as all it does is point the variable name at a storage location that has been previously defined.  When we have multiple files and we define a global variable or function which will be used in other files also or say the program is spread across two or more files, then ‘extern’ will be used to give reference of defined variable or function. So basically ‘extern’ is used to declare a global variable or function in other files.  The extern modifier is most commonly used when there are two or more files sharing the same global variables or functions . A small demonstration is given from the previous example to help understand the concept better :
  • 38. file2 #include<iostream> #include<conio.h> using namespace std; extern int i; //..here the extern keyword is used to declare i in this file and it refers to g.v. i declared in the previous file… class a { public : void write_extern() { cout<<“The value of i is “<<i<<endl; } }; Note : It is because of the ‘extern’ specifier that variable i could be used in this file & void write_extern() in the previous file.
  • 39. Static local Global Visibility function file Lifetime program program Initialized value 0 0 Purpose Same as local but retains value when function terminates. Variables used by several classes & functions but inside the same file. Comparison between Static local and global :
  • 40. Books: 1. Robert Lafore 2. Herbert Schildt  Websites: 1. Hawaii.edu 2. Tutorialspoint.com