C++ allows the use of modifiers for int, char, and double types. So, what are modifiers in C++? Find out more in the following Mytour article.
Article Contents:
1. C++ Modifier
2. Some important notes about modifiers
1. C++ Modifiers
In C++, there are four types of modifiers:
- Signed
- Unsigned
- Short
- Long
In which signed and unsigned modifiers handle the signs (+ / -) of variables.
The signed variable stores signed values in allocated memory, occupying an additional 1-bit space. The unsigned variable does not store signed values. Thus, using unsigned values means freeing up additional memory space to store variable values. The range of values for the unsigned type starts from 0.
For example, the value range of the int data type starts from -2,147,483,648 to 2,147,483,647, and the value range of unsigned int starts from 0 to 4,294,967,295.
Short modifier uses fewer bytes and has a shorter value range. For instance, the range of short int starts from -32,768 to 32,767, while the range of int starts from -2,147,483,648 to 2,147,483,647.
2. Some Important Notes About Modifiers
- All 4 modifiers can be applied to the int type.
- The char type allows modification with signed and unsigned modifiers.
- The double type can be used with the long modifier.
The int type allows the use of shorthand notation. Therefore, the definition of the variables below is the same:
short int a and short a;
Examples include unsigned int a and unsigned a;
long int a and long a;
- Lastly, you can combine modifiers.
For example, you can use signed or unsigned modifiers with long or short modifiers. Using the correct modifiers can limit and free up memory space.
When dealing with non-negative variable values, the unsigned modifier comes in handy for optimizing memory space. Additionally, if we know that the variable's range falls below 32,767, we can use the short modifier.
Below is an illustration of using long long:
Declare an unsigned short variable 'a';
Declare an unsigned long variable 'b';
Declare a variable 'c' with the type long long;
Declare an unsigned long long variable 'd';
In this article, Mytour has just introduced you to the concept of Modifiers in C++. In the next article, Mytour will continue to delve into Storage Classes in C++. Additionally, readers can explore other articles on Mytour to gain a deeper understanding of variable scope in C. The articles cover various types of variables, along with specific examples and detailed analysis.