In C++, modifiers are used to alter the built-in data types.
It specifies how much memory will be available for a variable.
This tutorial will teach you how to utilize C++ Modifier Types with examples.
Introduction to Modifiers in C++
-2,147,483,647 to 2,147,483,647 is the range of the integer data type.
This range is due to the size of the integer in memory, which is 4 bytes.
In certain circumstances, it is necessary to adjust the default value of types.
Modifiers change (increase or decrease default value) the data size to assist large values that can not fit within a specific range to save program memory.
In C++, some modifiers allow us to change the meaning of primitive data types such as int, char, and double.
Four Types of Modifiers in C++
The four types of modifiers in C++ are signed, unsigned, long, and short.
Here’s a quick lesson about C++ Data Type Modifiers and Qualifiers:
|Data Type Modifiers||Range Value|
|signed int||-2,147,483,648 to 2,147,483,647|
|unsigned int||0 to 4,294,967,295|
|short||-32768 to 32767|
|long||-2147483647 to 2147483647|
|unsigned long||0 to 4,294,967,295|
|long long||-(2^63) to (2^63)-1|
|unsigned long long|
0 to 18,446,744,073,709,551,615
|signed char||-127 to 127|
|unsigned char||0 to 255.|
|const||The running program cannot modify objects of type “const”.|
|volatile||The modifier “volatile” informs the compiler that the value of a variable may be altered in ways not expressly defined by the program.|
|restrict||Initially, only a pointer qualified by “restrict” is the object it points to can be accessed. “Restrict” is a new type of qualifier exclusive to C++.|
Learn more about C++ special words from our C++ Basic Syntax Tutorial.
Short Type Modifier in C++
The short type modifier can adjust the data type’s maximum and minimum values to save memory. For example:
// short int (small integer) short x = 12345;
This example shows that “x” is the short variable whose value is 12345.
Long Type Modifier in C++
The long data-type modifier in C++ is used to expand the size of the current data type, which allows for the storage of large numbers.
However, it only applies to int and double data types, for example:
// long int (large integer) long y = 123456;
It shows an example wherein the default value was extended. This modifier is equal to long int.
Remember that long and short modifiers can also be used as short or long instead of long int or short int.
We can also use long modifiers with double variables, such as decimal numbers.
// long double (large floating-point number) long double z = 0.3333333333333333333L;
The L suffix means a long double. If this suffix is omitted, a double value converts into long double value (which may result in data loss).
Long Long Modifiers in C++
long can be repeated twice to form the long long data type and can be used for even greater numbers than long and is only used to int data type.
// long long int (very large number) long long number = 12345678;
This example shows that the variable “number” is a long long int.
Signed and Unsigned Modifiers in C++
Positive and negative numbers, including zero, can be stored in signed variables. For example:
// for signed int // positive integer signed int a = 1; //negative integer signed int b = -1; // 0 value signed int c = 0;
Signed integers are the default values, so instead of using signed int, we can use int directly.
signed and unsigned are only usable for int and char data types.
Unsigned is a C++ modifier that can only store positive integer values.
// unsigned int (positive number) unsigned a = 1; unsigned b = 0;
In this example, the unsigned a has a positive value (1) and the unsigned b holds zero.
A typical int variable can store values between -2,147,483,648 and 2,147,483,647.
But, an unsigned int variable can store values between 0 and 4,294,967,295 inclusive.
Signed, Unsigned, and Plain Char
There are three char types in C++ language.
- signed char
- unsigned char
- plain char
Initially, there were only two sorts of characters which is the signed char and the unsigned char.
Different compilers treat char as either a signed char or an unsigned char, based on the user’s choices.
Also, an unsigned char can only hold positive integers, but a signed char can store both positive and negative integers (including 0).
Examples of plain char:
// plain char // positive value char a = 1; // zero-value char b = 0; // negative value char c = -1;
This example shows that we can directly use plain char with positive, negative, and zero values.
Remember that negative values in plain char might cause problems in some C++ compilers.
Examples of signed char:
// signed char // positive value signed char d = 1; // zero-value signed char e = 0; // negative value signed char f = -1;
The example shows that the signed char can hold positive, negative, and positive values.
This modifier is also recognized in different C++ compilers.
Examples of unsigned char:
// signed char // positive value unsigned char g = 1; // zero-value unsigned char h = 0;
In this example, we can apply the unsigned char with positive and zero values.
Reminder: In actual operation, it is best to utilize signed char or unsigned char instead of plain char.
PlainChar should be used for storing character values only.
In summary, C++ modifiers are special words that can modify the default value of data types.
It can adjust the range of data types and reserve memory space for variables.
Learning the C++ data-type modifiers could help us avoid problems when running programs in C++ Programming.