In the article C++ Data Types along with basic data types like int
, char
, double
you can see short int
, unsigned char
, long double
, signed int
etc. The words short
, unsigned
, long
, signed
are called type modifiers. C++ allows us to use some modifiers for int
, char
and double
types
There are four kinds of modifiers:
- Signed
- Unsigned
- Short
- Long
As the meaning tells, signed
and unsigned
modifiers deals with the (+/-) sign of the variable. signed variable stores the signed value in the allocated memory. the unsigned variable does not store signed value. The sign takes 1 bit extra. So if we are using unsigned value then we can get one-bit extra space to save the value of a variable. The range of values for unsigned types starts from 0.
For example, for int
data type range of values is from -2,147,483,648 to 2,147,483,647, and for unsigned int
the range is from 0 to 4,294,967,295.
The short
modifier makes a type to use fewer bytes and it reduces the range of values for that type. For example, range for short int is from -32,768 to 32,767 in comparison with int that has range from -2,147,483,648 to 2,147,483,647.
Important points to know about modifiers
- All four modifiers can be applied to the
int
type char
type allows onlysigned
andunsigned
modifiersdouble
type can be used with thelong
modifierint
type allows the use of the shorthand notation. So, the following variable definitions are identical
- All four modifiers can be applied to the
short int a;
and short a;
unsigned int a;
and unsigned a;
long int a
; and long a
;
5. The modifiers can be combined. For example, you can use signed
or unsigned
with long
or short
modifiers. The correct use of modifiers can reduce memory usage. So if we know that our variable can never be negative then to save memory we should use unsigned
modifiers. And we should short
modifier if we know the range of variables will be below 32,767. Below are the examples where you can see even we can use long long
unsigned short a; unsigned long b; long long c; unsigned long long d;