• Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard
CSEstack

What do you want to Learn Today?

  • Programming
    • Tutorial- C/C++
    • Tutorial- Django
    • Tutorial- Git
    • Tutorial- HTML & CSS
    • Tutorial- Java
    • Tutorial- MySQL
    • Tutorial- Python
    • Competitive Coding Challenges
  • CSE Subject
    • (CD) Compiler Design
    • (CN) Computer Network
    • (COA) Computer Organization & Architecture
    • (DBMS) Database Management System
    • (DS) Data Structure
    • (OS) Operating System
    • (ToA) Theory of Automata
    • (WT) Web Technology
  • Interview Questions
    • Interview Questions- Company Wise
    • Interview Questions- Coding Round
    • Interview Questions- Python
    • Interview Questions- REST API
    • Interview Questions- Web Scraping
    • Interview Questions- HR Round
    • Aptitude Preparation Guide
  • GATE 2022
  • Linux
  • Trend
    • Full Stack Development
    • Artificial Intelligence (AI)
    • BigData
    • Cloud Computing
    • Machine Learning (ML)
  • Write for Us
    • Submit Article
    • Submit Source Code or Program
    • Share Your Interview Experience
  • Tools
    • IDE
    • CV Builder
    • Other Tools …
  • Jobs

Storage Classes in C and C++ | Explained with Example

Heena Rajpal/14511/0
C / C++CodeCSE SubjectData Structure

Every datatype has storage classes in C and C++. This class is used to define the scope and visibility of the variables.
The scope is the area of the program where the variable exists and contains a valid value. Visibility refers to the area of the code where the variable can be accessed.

By default, all the variables have ‘auto’ class. Here auto stands for automatic.

Storage Classes in C and C++ :

The various storage classes in C and C++ are –

  1. Auto
  2. Static
  3. Register
  4. Extern
  5. Mutable

C++ has another storage class apart from the above mentioned four called mutable.

1. Auto

This is the default class for all variables. The variables of this class are usual local variables.

For e.g.: The variables that we define within a particular function that cannot be accessed outside it belong to the auto class.

Basically, all variables that have the scope and are visible within a particular block of code (or within particular parenthesis) belong to the auto category storage class.

Since it is the default, the declarations –

int a;
auto int a;

Means, both of the declaration is the same.

2. Static

This is a class of variables that are visible within a particular block of code but persist throughout the program, i.e., they have a lifetime of the entire program.

They are declared as-

static int a;

These are specifically used within classes in C++ so that multiple objects can share the same variable.

If we have to count the number of objects created, we will initialize as –
static int count;

By default, the static values are initialized to zero. typedef unsigned char BYTE;

Now, since it is shared by all the objects, it won’t be re-initialized to zero after creating a new object. Instead, it will persist its value. Hence, we can record the count of the students.

3. Register

The variables that we create, initialize and use in our program are stored in RAM.

Why should you use Register variable? What are the advantages of register variable?

Every time we need that variable, its value is supposed to be fetched from RAM. This process is time-consuming in terms of CPU cycles. Hence, if a variable is to be accessed more often, we can store it in a register instead. This is mostly used for counters.
The declaration is –

register int c;

Important Points about Register variable:

  • This may (and not will) store the variable in the register as per the CPU’s convenience and quickly access it when needed.
  • The size of the variable depends on the size of the register where it is stored.
  • One important thing to remember is that we cannot use & (address of) operator for register variables. This is because there is no memory address for the registers.

4. Extern

Here, extern stands for external. This is used to re-use the globally defined variables or functions visible in all program files.

Extern keyword is used in the program that uses the globally defined variable (pre-defined in another program) in our program. Hence, we do not initialize variables with the extern keyword.

For Example:

In Program 1 :
int num; //global
void main()
{
num=5;
}

In program 2 :

extern int a //This means we are willing to re-use predefined int num in the current program.
void print()
{
cout<<a;
}

Storage Classes in C++

All the above 4 are storage classes in C and C++. Following is storage class that supports only in C++.

5. Mutable

It is used only in C++.

This keyword is used to modify the data members that should otherwise be constant or aren’t allowed to be modified.

This keyword can be used only with the data members of a class.

Now suppose we want a variable to be modified by a constant function (that cannot change any values), which generally isn’t possible, we make the variable mutable to permit this.

Similarly, if a constant object is created, none of its data members can be modified, but if any of its data members is mutable, it is an exceptional case and that data member can be modified further.

Syntax:

mutable int a;

Next…

If you want to be the expert in C and C++  programming, challenge yourself with these C and C++ coding practices.

In C, typedef is also considered as a storage class. However, its purpose is to give an alias to a particular type, or in other words, define a new kind of type.

Also, remember that one variable can have only one storage class. Multiple storage class specifiers for one variable would give a compiler error.

cppstorage class
Heena Rajpal
Heena Rajpal is pursuing Computer Science Engineering from Indore. She has a knack for writing and an inquisitive outlook towards Computer Science fields like Database Management Systems, Object Oriented Programming and languages like C, C++, JAVA, Python, HTML etc.

Your name can also be listed here. Got a tip? Submit it here to become an CSEstack author.

Leave a Reply Cancel reply

C Programming

  1. C- Introduction
  2. C- Compile & Execute Program
  3. C- Data Types
  4. C- if-else statement
  5. C- While, do-while, for loop
  6. C- Array
  7. C- Function (Types/Call)
  8. C- strlen() vs sizeof()
  9. C- Nested Switch Statement
  10. C- Recursion
  11. C- Dynamic Programming
  12. C- Storage Classes
  13. C- Creating Header File
  14. C- Null Pointer
  15. C- Stack and Queue
  16. C- Implement Stack using Array
  17. C- Implement Linked List in C
  18. C- File Handling
  19. C- Makefile Tutorial

Object Oriented Concepts in C++

  • C++: C vs OOPs Language
  • C++: Introduction to OOPs Concepts
  • C++: Inheritance

Sorting Algorithms

  • Different Types of Sorting Algo
  • Selection Sort
  • Bubble Sort
  • Quick Sort

Programming for Practice

  1. Online C/C++ Compiler

String Handling:

  1. Remove White Spaces from String
  2. Implement strstr Function in C
  3. Convert String to Int – atoi()
  4. Check if String is Palindrome
  5. Check if Two Strings are Anagram
  6. Split String in using strtok_r()
  7. Undefined reference to strrev

Array:

  1. Check if Array is Sorted

Bit Manipulation:

  1. Count Number of 1’s in Binary

Linked List:

  1. Reverse a Linked List Elements

Number System:

  1. Program to Find 2’s Complement
  2. Convert Decimal to Binary in C

Tricky Questions:

  1. Add Two Numbers without Operator
  2. Find Next Greater Number
  3. Swap values without temp variable
  4. Print 1 to 100 without Loop

Interview Coding Questions

  • 50+ Interview Coding Questions

© 2022 – CSEstack.org. All Rights Reserved.

  • Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard