SlideShare uma empresa Scribd logo
1 de 24
-Reddhi Sekhar Basu
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 scope of a variable is the portion of the program
text in which the variable can be referenced.
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
 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.
 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
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.
We cannot use the mutable specifier with
names declared as static or const, or
reference members.
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.

Mais conteúdo relacionado

Mais procurados

Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
Todor Kolev
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
Todor Kolev
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
Todor Kolev
 
Code Generation Cambridge 2013 Introduction to Parsing with ANTLR4
Code Generation Cambridge 2013  Introduction to Parsing with ANTLR4Code Generation Cambridge 2013  Introduction to Parsing with ANTLR4
Code Generation Cambridge 2013 Introduction to Parsing with ANTLR4
Oliver Zeigermann
 
Chapter 11 Function
Chapter 11 FunctionChapter 11 Function
Chapter 11 Function
Deepak Singh
 

Mais procurados (20)

Storage class in c
Storage class in cStorage class in c
Storage class in c
 
Protocol
ProtocolProtocol
Protocol
 
Storage classes in C
Storage classes in C Storage classes in C
Storage classes in C
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
 
Actors model in gpars
Actors model in gparsActors model in gpars
Actors model in gpars
 
Code Generation Cambridge 2013 Introduction to Parsing with ANTLR4
Code Generation Cambridge 2013  Introduction to Parsing with ANTLR4Code Generation Cambridge 2013  Introduction to Parsing with ANTLR4
Code Generation Cambridge 2013 Introduction to Parsing with ANTLR4
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Storage classes
Storage classesStorage classes
Storage classes
 
Java 8 features
Java 8 featuresJava 8 features
Java 8 features
 
Chapter 11 Function
Chapter 11 FunctionChapter 11 Function
Chapter 11 Function
 
Storage class
Storage classStorage class
Storage class
 
Storage class
Storage classStorage class
Storage class
 
Operator Overloading and Scope of Variable
Operator Overloading and Scope of VariableOperator Overloading and Scope of Variable
Operator Overloading and Scope of Variable
 
Java 8 by example!
Java 8 by example!Java 8 by example!
Java 8 by example!
 
Java 8 new features
Java 8 new featuresJava 8 new features
Java 8 new features
 
Java 8 Feature Preview
Java 8 Feature PreviewJava 8 Feature Preview
Java 8 Feature Preview
 
C#4.0 features
C#4.0 featuresC#4.0 features
C#4.0 features
 
A Brief Conceptual Introduction to Functional Java 8 and its API
A Brief Conceptual Introduction to Functional Java 8 and its APIA Brief Conceptual Introduction to Functional Java 8 and its API
A Brief Conceptual Introduction to Functional Java 8 and its API
 

Semelhante a Storage Class Specifiers

Storage Classes and Functions
Storage Classes and FunctionsStorage Classes and Functions
Storage Classes and Functions
Jake Bond
 
Latest C Interview Questions and Answers
Latest C Interview Questions and AnswersLatest C Interview Questions and Answers
Latest C Interview Questions and Answers
DaisyWatson5
 
Static Keyword Static is a keyword in C++ used to give special chara.pdf
  Static Keyword Static is a keyword in C++ used to give special chara.pdf  Static Keyword Static is a keyword in C++ used to give special chara.pdf
Static Keyword Static is a keyword in C++ used to give special chara.pdf
KUNALHARCHANDANI1
 
Data structure scope of variables
Data structure scope of variablesData structure scope of variables
Data structure scope of variables
Saurav Kumar
 

Semelhante a Storage Class Specifiers (20)

Storage classes
Storage classesStorage classes
Storage classes
 
Storage Classes and Functions
Storage Classes and FunctionsStorage Classes and Functions
Storage Classes and Functions
 
Storage classess of C progamming
Storage classess of C progamming Storage classess of C progamming
Storage classess of C progamming
 
Storage classes in c language
Storage classes in c languageStorage classes in c language
Storage classes in c language
 
Storage classes in c language
Storage classes in c languageStorage classes in c language
Storage classes in c language
 
Storage classes
Storage classesStorage classes
Storage classes
 
Latest C Interview Questions and Answers
Latest C Interview Questions and AnswersLatest C Interview Questions and Answers
Latest C Interview Questions and Answers
 
Lecture 13 - Storage Classes
Lecture 13 - Storage ClassesLecture 13 - Storage Classes
Lecture 13 - Storage Classes
 
Terms and Definitions.pdf
Terms and Definitions.pdfTerms and Definitions.pdf
Terms and Definitions.pdf
 
Storage classes
Storage classesStorage classes
Storage classes
 
Static Keyword Static is a keyword in C++ used to give special chara.pdf
  Static Keyword Static is a keyword in C++ used to give special chara.pdf  Static Keyword Static is a keyword in C++ used to give special chara.pdf
Static Keyword Static is a keyword in C++ used to give special chara.pdf
 
Technical Interview
Technical InterviewTechnical Interview
Technical Interview
 
Chapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.pptChapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.ppt
 
STORAGE CLASS.pptx
STORAGE CLASS.pptxSTORAGE CLASS.pptx
STORAGE CLASS.pptx
 
Advanced C Programming Notes
Advanced C Programming NotesAdvanced C Programming Notes
Advanced C Programming Notes
 
Static keyword u.s ass.(2)
Static keyword u.s ass.(2)Static keyword u.s ass.(2)
Static keyword u.s ass.(2)
 
5.program structure
5.program structure5.program structure
5.program structure
 
C questions
C questionsC questions
C questions
 
FUNCTION CPU
FUNCTION CPUFUNCTION CPU
FUNCTION CPU
 
Data structure scope of variables
Data structure scope of variablesData structure scope of variables
Data structure scope of variables
 

Último

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
ssuserdda66b
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 

Último (20)

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 

Storage Class Specifiers

  • 2.
  • 3. 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 scope of a variable is the portion of the program text in which the variable can be referenced. The linkage of a variable determines whether it may be shared by more than one file in the same program.
  • 5. 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.
  • 6. 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.
  • 7. 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.
  • 8. 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
  • 9. External • The same variable may be shared by several files Internal • The variable is restricted to a single file
  • 10.
  • 11.  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
  • 12.  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.
  • 13.  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.
  • 14.  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.
  • 15.
  • 16.  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.
  • 17.  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.
  • 18.  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.
  • 19.  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.
  • 20.  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.
  • 21. 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
  • 22.
  • 23. 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. We cannot use the mutable specifier with names declared as static or const, or reference members.
  • 24. 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.