C++ Type Modifiers
In C++, type modifiers are the keywords used to change or give extra meaning to already existing data types. It is added to primitive data types as a prefix to modify their size or range of data they can store.
C++ have 4 type modifiers which are as follows:
Table of Content
Let's understand at each of them one by one.
1. signed Modifier
The signed modifier indicates that the given variable variables can store both positive and negative values. To specify any variable as signed, just add the keyword signed at the start of the variable declaration.
Syntax
signed type name;
It can be used only with integer and character data types.
Example
#include <iostream>
using namespace std;
int main() {
// Printing the size of normal and signed int
cout << "signed int size: " << sizeof(signed int)
<< " bytes" << endl;
cout << "int size: " << sizeof(int) << " bytes";
return 0;
}
Output
signed int's size: 4 bytes int's size: 4 bytes
Note: The int datatype is signed by default. So, int can directly be used instead of signed int.
2. unsigned Modifier
The unsigned modifier indicates that the given variables can store only non-negative integer values. Like signed modifier, it is also added at the start of the variable declaration to define the given variable as unsigned.
Syntax
unsigned type name;
Unsigned modifier can only be used with integer and character data types.
Example
#include <iostream>
using namespace std;
int main() {
// Declaring unsigned integer with negative value
unsigned int unsigned_i = -10;
// Declaring normal integer with negative value
int i = -10;
// Printing the value and size
cout << "unsigned_i: " << unsigned_i << endl;
cout << "unsigned int size: " << sizeof(unsigned_i)
<< " bytes" << endl;
cout << "i: " << i << endl;
cout << "int size: " << sizeof(i) << " bytes";
return 0;
}
Output
unsigned_i: 4294967286 unsigned_i's size: 4 bytes i: -10 i's size: 4 bytes
As we can see, when we assigned some negative value to unsigned integer, the value gets converted to its 2's complement because unsigned types cannot store the negative values.
3. short Modifier
The short keyword decreases the size of the data type in the memory. It leads to the decrease in the range of value that the given data type can store. A variable can be declared as short by adding the short keyword before the variable declaration.
Syntax
short type name;
The short keyword can only be used with integer data type.
Example
#include <iostream>
using namespace std;
int main() {
// Printing the size of short and normal ints
cout << "short int size: " << sizeof(short int)
<< " bytes" << endl;
cout << "int size: " << sizeof(int)
<< " bytes";
return 0;
}
Output
short int size: 2 bytes int size: 4 bytes
Note: The short int can be written as short also. They are equivalent.
4. long Modifier
The long keyword increases the size of the data type in memory. It leads to increase in the range of value that the given data type can store. A variable can be declared as long by adding the long keyword before the variable declaration.
Syntax
long type name;
The long modifier can be used with integer and double data type. It can also be used twice on integers.
Example:
#include <iostream>
using namespace std;
int main() {
// Printing the size of long and normal ints
// double and long double
cout << "int size: " << sizeof(int)
<< " bytes" << endl;
cout << "long int size: " << sizeof(long int)
<< " bytes" << endl;
cout << "double size: " << sizeof(double)
<< " bytes" << endl;
cout << "long double size: " << sizeof(long double)
<< " bytes";
return 0;
}
Output
int size: 4 bytes long int size: 8 bytes double size: 8 bytes long double size: 16 bytes
Note: The long int can be written as long also. They are equivalent.
Size and Range of Data Types with Modifiers
The below table lists the size and the range of data type (in 64-bit compiler) that is changed with the help of modifiers:
Data Type | Modifiers | Size (bytes) | Range |
---|---|---|---|
char | signed | 1 | -128 to 127 |
unsigned (default) | 1 | 0 to 255 | |
short int | signed (default) | 2 | -32,768 to 32,767 |
unsigned | 2 | 0 to 65,535 | |
int | signed (default) | 4 | -2,147,483,648 to 2,147,483,647 |
unsigned | 4 | 0 to 4,294,967,295 | |
long int | signed (default) | 8 | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
unsigned | 8 | 0 to 18,446,744,073,709,551,615 | |
long long int | signed (default) | 8 | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
unsigned | 8 | 0 to 18,446,744,073,709,551,615 | |
double | None | 8 | ~1.7E-308 to 1.7E+308 |
long double | None | 16 | Higher precision, range varies depending on implementation |