Modifiers, Qualifiers, Storage Class
In C++, variables can be modified and qualified using various modifiers, qualifiers, and storage classes.
These elements help control the behavior, scope, and storage allocation of variables within a program.
Modifiers
We have 4 main modifiers in C/C++:
- long
- short
- unsigned
- signed
We can use data type modifiers with int, double and char data types.
long
long is used to increase the length of a data type to 4 more bytes. We can use long with int and double data types. Let’s use a long modifier with built-in data types.
int integer = 2147483649; // it will have value of -2147483647
long int long_integer = 2147483649; // it will have the correct value
short
short decreases the available length of a data type to 2 bytes. We can use short with an int data type.
int integer = 32768; // it will have the correct value
short int short_integer = 32768; // it will have value of -32768
unsigned
unsigned allows us to store positive values only. We can use unsigned with char and int data types. With unsigned int, we can store any value from 0 to 4294967295. With unsigned char, we can store any value from 0 to 255.
int integer = -10; // it has a value -10
unsigned int unsigned_integer = -10; // it has a value 4294967286
char character = 'A'; // it has a value A
unsigned char unsigned_character = 'B'; // it has a value B
signed
signed allows us to store both positive and negative values. We can use signed with char and int data types.With signed int, we can store any value from -2,147,483,648 to 2,147,483,647. With signed char, we can store any value from -128 to 127.
int integer = -10; // it has a value -10
signed int signed_integer = -10; // it has a value -10
char character = 'A'; // it has a value A
signed char signed_character = 'B'; // it has a value B
Qualifiers
Type qualifiers
- const: which makes the variable immutable, so its value can not be changed.
- volatile: which marks an object that may be changed by another process, this is commonly used for threaded and multi-user applications that share memory.
- mutable: this is used to modify a data member from a const qualified member function. (only C++)
const
In C++, we can use the const keyword to declare a constant.
Declares a variable as constant. It means the variable’s value cannot be changed once it’s initialized. The basic syntax for creating a constant is:
// const data_type identifier = const_value;
const int number = 10;
In C/C++, you have to initialize a constant at the time of its declaration. If you don’t initialize a constant at the time of creating it, an error will occur.
volatile
Indicates that a variable’s value can be changed unexpectedly by external sources (hardware, threads, etc.). It prevents the compiler from optimizing code involving that variable.
volatile int sensorValue = 0;
mutable
Applies to class members. It allows a member of an object to be modified even if the object is declared as const.
class Example {
public:
mutable int counter; // Mutable variable within a const object
// ...
};
Storage class
- auto: this is the default storage class.
- static: objects stored in a static memory have a life beyond the execution of a block, it is commonly used for keeping state between usages of a given function.
- register: to store the processor register, it is a suggestion for the compiler, maybe use it or not.
- extern: extern variables are defined in a separate translation unit and are linked together by the linker.
auto
The storage class “Auto” is applied to the local variables and is automatically assigned by the compiler to local variables. Local variables preceded by the ‘auto’ keyword remain active in the function in which they are declared and go out of scope once the function exits.
Automatically deduces the data type of a variable from its initializer.
auto value = 10; // Deduces value as an int
static
Alters the lifetime and visibility of a variable. It preserves the variable value between function calls and is accessible only within the current scope.
The static storage class tells the compiler to maintain the value of the variable throughout the lifetime of the program. Static variables are similar to the local variables but are preceded by a static keyword.
void exampleFunction() {
static int num = 0;
// Static variable retains its value between function calls
// ...
}
register
Suggests to the compiler that a variable should be stored in a CPU register for faster access. However, modern compilers often optimize this automatically.
register int count;
extern
Used to declare a variable that is defined in another translation unit (typically another source file). It allows multiple files to access the same variable.
// In File1.cpp
extern int sharedValue; // Declaration
// In File2.cpp
int sharedValue = 10; // Definition
These modifiers, qualifiers, and storage classes provide flexibility and control over how variables are declared, defined, and used in C++ programs, ensuring proper behavior, optimization, and data integrity.