Difficulty: Medium
Correct Answer: Storage qualifiers such as auto, register, static, and extern control storage duration, visibility, and linkage of variables in a C++ program
Explanation:
Introduction / Context:
In C and C++ programming, storage class or storage duration qualifiers specify how and where variables are stored, how long they live, and how they are linked across translation units. Understanding storage qualifiers is important for global variables, local variables, performance tuning, and correct program organisation. This question asks for a conceptual definition and examples of common storage qualifiers in C++.
Given Data / Assumptions:
Concept / Approach:
The main idea is that storage qualifiers tell the compiler how long a variable should exist and how it should be visible to other translation units. auto describes automatic storage duration for local variables (which is the default), register suggests that a variable be stored in a CPU register, static gives the variable static storage duration and often internal linkage, and extern declares that a variable is defined in another translation unit with external linkage. The correct option should explicitly mention these keywords and their role.
Step-by-Step Solution:
Step 1: Recall that auto (in classic C++) is the default for local variables, meaning they are created when the block is entered and destroyed when it is exited.
Step 2: Remember that register suggests to the compiler that a variable should be stored in a CPU register to speed up access, although the compiler is free to ignore this hint.
Step 3: Recognise that static gives a variable static storage duration, meaning it is allocated once and exists for the life of the program, which applies to static local variables and static global variables with internal linkage.
Step 4: Understand that extern declares variables with external linkage, allowing other translation units to use a variable defined elsewhere.
Step 5: Examine option (a), which references auto, register, static, and extern and states that they control storage duration, visibility, and linkage, matching textbook definitions.
Verification / Alternative check:
To verify, think of a simple multi file program. A global counter declared with extern in a header and defined once in a source file uses external linkage. A static global variable in a source file is not visible outside that file. A static local variable remembers its value between function calls. Local variables declared without any qualifier are automatic and destroyed when the function returns. These examples show how storage qualifiers affect lifetime and visibility, which is precisely what option (a) describes.
Why Other Options Are Wrong:
Option (b) is wrong because storage qualifiers are part of the language, not graphical settings in an integrated development environment. Option (c) is incorrect because const and volatile are type qualifiers, not storage class specifiers; they affect mutability and optimisation, not storage duration or linkage. Option (d) is wrong because storage qualifiers apply to ordinary variables and are not limited to template metaprogramming.
Common Pitfalls:
A common pitfall is to confuse storage qualifiers with type qualifiers such as const and volatile. Another mistake is to over rely on register, expecting it to guarantee register allocation; compilers today perform advanced optimisation and often ignore explicit register hints. Developers sometimes also misunderstand static, assuming it means constant value; in reality, static describes lifetime and linkage rather than immutability. Clarifying these distinctions is important for correct understanding of C++ program structure.
Final Answer:
Storage qualifiers such as auto, register, static, and extern control storage duration, visibility, and linkage of variables in a C++ program.
Discussion & Comments