SlideShare a Scribd company logo
1 of 12
Storage Classes in C
Storage Classes are used to describe the features of a variable/function. These features
basically include the scope, visibility and life-time which help us to trace the existence of a
particular variable during the runtime of a program.
A storage class in C is used to describe the following things:
•The variable scope.
•The location where the variable will be stored.
•The initialized value of a variable.
•A lifetime of a variable.
•Who can access a variable?
Thus a storage class is used to represent the information about a variable.
NOTE: A variable is not only associated with a data type, its value but also
a storage class.
C language uses 4 storage classes, namely:
Storage class Purpose
auto It is a default storage class.
extern It is a global variable.
static It is a local variable which is capable
of returning a value even when
control is transferred to the function
call.
register It is a variable which is stored inside
a Register.
AUTO
EXTERN
STATIC
REGISTER
This is the default storage class for all the variables
declared inside a function or a block. Hence, the
keyword auto is rarely used while writing programs
in C language. Auto variables can be only
accessed within the block/function they have been
declared and not outside them (which defines their
scope). Of course, these can be accessed within
nested blocks within the parent block/function in
which the auto variable was declared. However,
they can be accessed outside their scope as well
using the concept of pointers given here by
pointing to the very exact memory location where
the variables resides. They are assigned a garbage
value by default whenever they are declared.
AUTO
EXTERN
STATIC
REGISTER
Extern storage class simply tells us that the
variable is defined elsewhere and not within
the same block where it is used. Basically,
the value is assigned to it in a different
block and this can be overwritten/changed
in a different block as well. So an extern
variable is nothing but a global variable
initialized with a legal value where it is
declared in order to be used elsewhere. It
can be accessed within any function/block.
Also, a normal global variable can be made
extern as well by placing the ‘extern’
keyword before its declaration/definition in
any function/block. This basically signifies
that we are not initializing a new variable
but instead we are using/accessing the
global variable only.
AUTO
EXTERN
STATIC
REGISTER
This storage class is used to declare static
variables which are popularly used while
writing programs in C language. Static
variables have a property of preserving
their value even after they are out of their
scope! Hence, static variables preserve the
value of their last use in their scope. So we
can say that they are initialized only once
and exist till the termination of the program.
Thus, no new memory is allocated because
they are not re-declared. Their scope is
local to the function to which they were
defined. Global static variables can be
accessed anywhere in the program. By
default, they are assigned the value 0 by
the compiler.
EXTERN
STATIC
REGISTER
AUTO This storage class declares register variables
which have the same functionality as that of the
auto variables. The only difference is that the
compiler tries to store these variables in the
register of the microprocessor if a free register is
available. This makes the use of register variables
to be much faster than that of the variables stored
in the memory during the runtime of the program.
If a free register is not available, these are then
stored in the memory only. Usually few variables
which are to be accessed very frequently in a
program are declared with the register keyword
which improves the running time of the program.
An important and interesting point to be noted
here is that we cannot obtain the address of a
register variable using pointers.
EXAMPLES OF
THE STORAGE
CLASSES
AUTO
EXTERN
STATIC
REGISTER
1.#include <stdio.h>
2.int main()
3.{
4.int a; //auto
5.char b;
6.float c;
7.printf("%d %c %f",a,b,c); // printing initial
default value of automatic variables a, b, and c.
8.return 0;
9.}
Output:
garbage garbage garbage
AUTO
EXTERN
STATIC
REGISTER
1.#include <stdio.h>
2.int a;
3.int main()
4.{
5.extern int a; // variable a is defined
globally, the memory will not be allocated
to a
6.printf("%d",a);
7.}
Output:
0
AUTO
EXTERN
STATIC
REGISTER
1.#include<stdio.h>
2.void sum()
3.{
4.static int a = 10;
5.static int b = 24;
6.printf("%d %d n",a,b);
7.a++;
8.b++;
9.}
10.void main()
11.{
12.int i;
13.for(i = 0; i< 3; i++)
14.{
15.sum(); // The static variables holds their value between
multiple function calls.
16.}
17.}
Output:
10 24
11 25
12 26
AUTO
EXTERN
STATIC
REGISTER
1.#include <stdio.h>
2.int main()
3.{
4.register int a; // variable a is
allocated memory in the CPU
register. The initial default value
of a is 0.
5.printf("%d",a);
6.}
Output:
0
Created By :- Mr. UTTAM VERMA
M.C.A. 1ST SEMESTER
M.D.S.U (AJMER)

More Related Content

What's hot (20)

Access specifier
Access specifierAccess specifier
Access specifier
 
Typecasting in c
Typecasting in cTypecasting in c
Typecasting in c
 
Java access modifiers
Java access modifiersJava access modifiers
Java access modifiers
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
 
Exception handling c++
Exception handling c++Exception handling c++
Exception handling c++
 
Ppt on this and super keyword
Ppt on this and super keywordPpt on this and super keyword
Ppt on this and super keyword
 
Datatype in c++ unit 3 -topic 2
Datatype in c++ unit 3 -topic 2Datatype in c++ unit 3 -topic 2
Datatype in c++ unit 3 -topic 2
 
File handling in c
File handling in cFile handling in c
File handling in c
 
Friends function and_classes
Friends function and_classesFriends function and_classes
Friends function and_classes
 
Pointers, virtual function and polymorphism
Pointers, virtual function and polymorphismPointers, virtual function and polymorphism
Pointers, virtual function and polymorphism
 
Java annotations
Java annotationsJava annotations
Java annotations
 
CP Handout#5
CP Handout#5CP Handout#5
CP Handout#5
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Polymorphism in c++(ppt)
Polymorphism in c++(ppt)Polymorphism in c++(ppt)
Polymorphism in c++(ppt)
 
Final keyword in java
Final keyword in javaFinal keyword in java
Final keyword in java
 
Multiple inheritance possible in Java
Multiple inheritance possible in JavaMultiple inheritance possible in Java
Multiple inheritance possible in Java
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Constructors and Destructor in C++
 
Inner class
Inner classInner class
Inner class
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 

Similar to Storage classes (20)

Storage Class Specifiers in C++
Storage Class Specifiers in C++Storage Class Specifiers in C++
Storage Class Specifiers in C++
 
Storage classes
Storage classesStorage classes
Storage classes
 
Storage classes
Storage classesStorage classes
Storage classes
 
Storage Class Specifiers
Storage Class SpecifiersStorage Class Specifiers
Storage Class Specifiers
 
Storage classes
Storage classesStorage classes
Storage classes
 
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 classess of C progamming
Storage classess of C progamming Storage classess of C progamming
Storage classess of C progamming
 
Storage Class in C Progrmming
Storage Class in C Progrmming Storage Class in C Progrmming
Storage Class in C Progrmming
 
Storage classes
Storage classesStorage classes
Storage classes
 
STORAGE CLASS.pptx
STORAGE CLASS.pptxSTORAGE CLASS.pptx
STORAGE CLASS.pptx
 
Terms and Definitions.pdf
Terms and Definitions.pdfTerms and Definitions.pdf
Terms and Definitions.pdf
 
Storage class
Storage classStorage class
Storage class
 
Lecture 13 - Storage Classes
Lecture 13 - Storage ClassesLecture 13 - Storage Classes
Lecture 13 - Storage Classes
 
Storage Classes and Functions
Storage Classes and FunctionsStorage Classes and Functions
Storage Classes and Functions
 
C storage class
C storage classC storage class
C storage class
 
5.program structure
5.program structure5.program structure
5.program structure
 
What is storage class
What is storage classWhat is storage class
What is storage class
 
Storage classes in C
Storage classes in C Storage classes in C
Storage classes in C
 
Storage class
Storage classStorage class
Storage class
 

Recently uploaded

How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 

Recently uploaded (20)

How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 

Storage classes

  • 1. Storage Classes in C Storage Classes are used to describe the features of a variable/function. These features basically include the scope, visibility and life-time which help us to trace the existence of a particular variable during the runtime of a program. A storage class in C is used to describe the following things: •The variable scope. •The location where the variable will be stored. •The initialized value of a variable. •A lifetime of a variable. •Who can access a variable? Thus a storage class is used to represent the information about a variable. NOTE: A variable is not only associated with a data type, its value but also a storage class.
  • 2. C language uses 4 storage classes, namely: Storage class Purpose auto It is a default storage class. extern It is a global variable. static It is a local variable which is capable of returning a value even when control is transferred to the function call. register It is a variable which is stored inside a Register.
  • 3. AUTO EXTERN STATIC REGISTER This is the default storage class for all the variables declared inside a function or a block. Hence, the keyword auto is rarely used while writing programs in C language. Auto variables can be only accessed within the block/function they have been declared and not outside them (which defines their scope). Of course, these can be accessed within nested blocks within the parent block/function in which the auto variable was declared. However, they can be accessed outside their scope as well using the concept of pointers given here by pointing to the very exact memory location where the variables resides. They are assigned a garbage value by default whenever they are declared.
  • 4. AUTO EXTERN STATIC REGISTER Extern storage class simply tells us that the variable is defined elsewhere and not within the same block where it is used. Basically, the value is assigned to it in a different block and this can be overwritten/changed in a different block as well. So an extern variable is nothing but a global variable initialized with a legal value where it is declared in order to be used elsewhere. It can be accessed within any function/block. Also, a normal global variable can be made extern as well by placing the ‘extern’ keyword before its declaration/definition in any function/block. This basically signifies that we are not initializing a new variable but instead we are using/accessing the global variable only.
  • 5. AUTO EXTERN STATIC REGISTER This storage class is used to declare static variables which are popularly used while writing programs in C language. Static variables have a property of preserving their value even after they are out of their scope! Hence, static variables preserve the value of their last use in their scope. So we can say that they are initialized only once and exist till the termination of the program. Thus, no new memory is allocated because they are not re-declared. Their scope is local to the function to which they were defined. Global static variables can be accessed anywhere in the program. By default, they are assigned the value 0 by the compiler.
  • 6. EXTERN STATIC REGISTER AUTO This storage class declares register variables which have the same functionality as that of the auto variables. The only difference is that the compiler tries to store these variables in the register of the microprocessor if a free register is available. This makes the use of register variables to be much faster than that of the variables stored in the memory during the runtime of the program. If a free register is not available, these are then stored in the memory only. Usually few variables which are to be accessed very frequently in a program are declared with the register keyword which improves the running time of the program. An important and interesting point to be noted here is that we cannot obtain the address of a register variable using pointers.
  • 8. AUTO EXTERN STATIC REGISTER 1.#include <stdio.h> 2.int main() 3.{ 4.int a; //auto 5.char b; 6.float c; 7.printf("%d %c %f",a,b,c); // printing initial default value of automatic variables a, b, and c. 8.return 0; 9.} Output: garbage garbage garbage
  • 9. AUTO EXTERN STATIC REGISTER 1.#include <stdio.h> 2.int a; 3.int main() 4.{ 5.extern int a; // variable a is defined globally, the memory will not be allocated to a 6.printf("%d",a); 7.} Output: 0
  • 10. AUTO EXTERN STATIC REGISTER 1.#include<stdio.h> 2.void sum() 3.{ 4.static int a = 10; 5.static int b = 24; 6.printf("%d %d n",a,b); 7.a++; 8.b++; 9.} 10.void main() 11.{ 12.int i; 13.for(i = 0; i< 3; i++) 14.{ 15.sum(); // The static variables holds their value between multiple function calls. 16.} 17.} Output: 10 24 11 25 12 26
  • 11. AUTO EXTERN STATIC REGISTER 1.#include <stdio.h> 2.int main() 3.{ 4.register int a; // variable a is allocated memory in the CPU register. The initial default value of a is 0. 5.printf("%d",a); 6.} Output: 0
  • 12. Created By :- Mr. UTTAM VERMA M.C.A. 1ST SEMESTER M.D.S.U (AJMER)