Modifiers in C++
Overview
There are different types of modifiers in C++, also known as data type modifiers. These modifiers in C++ are used with data types like int, char, and float preceding to them. These types of modifiers are useful for increasing and decreasing the memory size.
Introduction to Modifiers in C++
The range of integer datatype is from -2,147,483,647 to 2,147,483,647. This range is because of the integer's size which is 4 bytes in the memory.
There are some situations where we need to modify the inbuilt data-types like int by increasing the size to store big numbers which are not fitting in the given range or by decreasing the size to save some memory in the program.
There are some modifiers in C++ that allow us to alter the meaning of base types like int, char, and double. The modifiers are as follows:
- signed - Used for both positive and negative values
- unsigned - Used for only positive values
- long - Used to increase the size of data-types
- short - Used to reduce the size of data-types
In this article, we will explore how to use these modifiers to modify or alter the meaning of base data types in C++.
Types of modifiers in C++ with Integer data-type
Suppose we are working with an application in C++ and there is a requirement to store the value 21474836478 which is not in range of normal int, then we will get the value as follows:
Code:
Output:
Now because the value in number is out of range we got the negative value which is next in the range of integer.
To store the value which is out of range we need to modify regular int to long long int or unsigned int so that it can store that value because the long long int range is greater as shown in the below table.
code:
Output:
Similarly, if we want to store small values in the range of -32,768 to 32,767 we can use short int instead of int to save 2 bytes of memory space.
Below is the table to show the storage size and range of different modifiers with the int data type -
Data Type | Storage Size | Range |
---|---|---|
short int | 2 bytes | -32,768 to 32,767 |
unsigned short int | 2 bytes | 0 to 65,536 |
unsigned int | 4 bytes | 0 to 4,294,967,295 |
int | 4 bytes | -2,147,483,648 to 2,147,483,647 |
long int | 4 bytes | -2,147,483,648 to 2,147,483,647 |
unsigned long int | 4 bytes | 0 to 4,294,967,295 |
long long int | 8 bytes | -(2^63) to (2^63)-1 |
unsigned long long int | 8 bytes | 0 to 18,446,744,073,709,551,615 |
In the above table, there are four type modifiers namely signed, unsigned, long, and short. The range of signed integers is the same as normal integers because they both are the same and it is optional to write signed preceding to int.
If there are no negative values in use, then we can use an unsigned type modifier and double the range of positive values as shown in the above table.
Types of Modifiers in C++ with character data-type
The range of char type is from -128 to 127, it stores the numbers, which are known as ASCII Value. Each value has some meaning associated with characters and symbols.
However, for custom use, if there is a need to change the range we can do it as shown in the following table:
Data Type | Storage Size | Range |
---|---|---|
char | 1 byte | -128 to 127 |
signed char | 1 byte | -128 to 127 |
unsigned char | 1 byte | 0 to 255 |
Types of Modifiers in C++ with floating point data-types
The double is also a type under floating-point datatype where double is of 8 bytes and float is of 4 bytes.
Below is the table with the size and range of modifiers with floating-point data types.
Data Type | Storage Size | Range |
---|---|---|
float | 4 bytes | 1.2E-38 to 3.4E+38 (6 decimal places) |
double | 8 bytes | 2.3E-308 to 1.7E+308 (15 decimal places) |
long double | 12 bytes | 3.4E-4932 to 1.1E+4932 (19 decimal places) |
Type qualifiers in C++
Type qualifiers are used to provide a piece of additional information about the variables they precede.
For example, const is a type qualifier that is used with int as const int which represents the constant integer value that can't be changed during the execution of a program.
Code:
Output:
Below is the list of type qualifiers in C++:
Type qualifier | Meaning of Qualifier |
---|---|
const | If this type qualifier is used, then the value can't be changed during the execution of the program. |
mutable | This qualifier is applied to non-static class members of the non-reference and non-const type when we want mutable characteristics. |
volatile | This qualifier is used to tell the compiler that a variable's value may be changed in a way explicitly specified by the program |
Conclusion
In this article,
- We saw different types of type modifiers and their meaning
- Memory size and the range of modifiers with different data types
- We also saw type modifiers and their meaning