Storage Classes in C++ (auto, register, extern, mutable) –

|
In C++, storage classes are keywords that define the lifespan, scope, and storage location of variables (such as auto, register, static, etc.) inside a program. Storage classes specified that how memory can allocated and deallocated for variables in a C++ Program. Syntax:It has the following syntax: In this syntax, the storage_class represents the storage class, var_data_type represents the variable data types, and var_name represents the variable name. Simple C++ Storage Classes Example:Let’s take an simple example to demonstrate the storage classes in C++. ExampleCompile and Run Output: Enter the value of X: 5 Enter the value of Y: 10 Addition result (auto + register): 15 Types of Storage Classes in C++:There are mainly six types of storage classes in C++.
Here is a table that shows the lifetime, visibility, and initial value.
1. Automatic (auto) Storage Class in C++The auto storage class is the default storage class for local variables. It is mainly utilized to declare the auto class for variables. It enables us to declare variables of specific data types, including int, double, and char. It offers faster memory access, logical data organization, and enhance program clarity and effectiveness. C++ Automatic Storage Class Example:Let us take an example to illustrate the automatic storage class in C++. ExampleCompile and Run Output: Explanation: In this example, the auto storage class is utilized for variables. The auto keyword cannot be utilized to declare the auto variables explicitly. 2. Register Storage Class in C++In C++, the “register” storage class is mainly utilized to instruct the compiler to store a variable in a register. We can access a register variable faster than a local or automatic variable via both have the same functionality. The default value of register storage class is 0. The (&) operator may not be utilized with a register variable. Variables with the “register” storage class also have automatic storage duration. C++ Register Storage Class Example:Let us take an example to illustrate the register storage class in C++. ExampleCompile and Run Output: i = 0, count = 2 i = 1, count = 3 i = 2, count = 5 i = 3, count = 8 i = 4, count = 12 i = 5, count = 17 i = 6, count = 23 3. Static Storage Class in C++In C++, the static storage class is used to declare the variables that hold their value for the complete program’s duration. A static variable is declared using the static keyword. We may declare a static variable for several times but the variable value may be assigned only once. Local variables are created when the program starts and destroyed when it terminates. The “static” variables have local scope within a block or function, but their values persist across function calls. They are typically stored in a separate data segment of memory. C++ Static Storage Class Example:Let us take an example to illustrate the static storage class in C++. ExampleCompile and Run Output: x = 3, Counter = 7 x = 4, Counter = 6 x = 5, Counter = 5 x = 6, Counter = 4 x = 7, Counter = 3 x = 8, Counter = 2 x = 9, Counter = 1 x = 10, Counter = 0 4. External (extern) Storage Class in C++In C++, the extern storage class is used to provide a reference of a global variable. It is declared outside of the function. Whenever the external variable is declared in C++, the variable may be utilized in any line of codes throughout the complete program. This extern modifier is very useful when multiple C++ files share the same global variables and functions. C++ External Storage Class Example:Let’s take an example to demonstrate the ‘extern’ storage class with global variables in C++. ExampleCompile and Run Output: Enter the Value of x: 7 Value of the variable 'x', declared as extern: 7 Modified value of the variable 'x', declared as extern: 7 5. Mutable Storage Class in C++In C++, a mutable storage class is used to change one or more data members of a class via a constant (const) function. It can easily be declared by mutable keywords, which are mainly utilized to enable a particular data member of an object to be a Modifier. The ‘mutable’ keyword allows modifying a data member of a const object, even within const member functions. C++ Mutable Storage Class Example:Let’s take an example to demonstrate the mutable storage class in C++. ExampleCompile and Run Output: Before modifying the value of y: 15 After modifying the value of y: 50 6. Thread_local Storage Class in C++In C++, the thread_local storage class is new storage class that introduces in C++11. It can be used to define the objects as thread_local. The thread_local object attributes modify under the combination of the its variables with several storage specifiers, such as extern and static. C++ Thread_local Storage Class Example:Let’s take an instance to demonstrate the thread_local storage class in C++. ExampleCompile and Run Output: After modifying the value of x inside the Main() function: 25 0x771e4eece73c Before modifying the value of x inside the Thread_localTest() function: 40 0x771e4ee006bc The value of x inside the Main() function: 25 0x771e4eece73c Why storage classes are used in C++?In C++, storage classes are used to regulate how variables are saved in memory, how long they exist, and where they may be accessed inside a program. C++ is essential for managing memory resources, improving efficiency, and assuring variable scope and lifespan. Here are some key reasons why storage classes are used in C++:
C++ Storage Classes MCQs1. What will be the default initial value of a variable defined as static in C++?
Answer: b) 0 2. When a register variable is declared in C++ program, where is it allocated?
Answer: c) In the CPU register for quicker access 3. In which storage class, the const keyword is used to declare variables in C++?
Answer: a) Auto Storage Class 4. What will be the output of the following program?
Answer: b) TpointTech Class: 1 TpointTech Class: 2 5. Which of the following storage class is the default storage class for global variables in C++?
Answer: d) Extern Next TopicC++ Arrays
|




