Type Modifiers In C++
In C++, type modifiers are keywords or specifiers used to modify the properties or behavior of fundamental data types. These modifiers allow you to control the range of values a variable can hold, its memory usage, and how it's interpreted by the compiler.
Here are some common type modifiers in C++:
signed
unsigned
long
short
Signed Modifier
Signed variables can hold both positive and negative integers including zero. The value range of signed int
is from -2,147,483,648 to 2,147,483,647
and its size (in bytes) is 4
and the value range of signed char
is -127 to 127 and its size (in bytes) is 1.
For example,
Note: By default, integers are signed
. Hence instead of signed int
, we can directly use int
.
Unsigned Modifier
The unsigned variables can hold only non-negative integer values. The value range of unsigned int
is from 0 to 4,294,967,295 its size (in bytes) is 4
and the value range of unsigned char
is 0 to 255 and its size (in bytes) is 1. The size (in bytes) of unsigned long
is also 4.
For example,
Note: The signed
and unsigned
modifiers are primarily used with int
and char
data types.
Long Modifier
If we need to store a large integer (in the range -2147483647 to 2147483647), we can use the type specifier long
. It's size in bytes is at least 4. For example,
The long int
can be written as long
also. They are equivalent.
The long
type modifier can also be used with double
variables. The size (in bytes) of long double
is 8. For eg:-
The long
modifier can be used as twice as long long
. It is used for even larger numbers than long
.
The size (in bytes) of long long
and unsigned long long
is 8.
However, it can only be used with int
. For example:-
Short Modifier
We can use short
for small integers (in the range - 32,767 to 32,767). It's size in bytes is 2. For example,
Here, x is a short
integer variable.
Note: short
is equivalent to short int
.