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,

// positive valued integer
signed int x = 23;
 
// negative valued integer
signed int y = -13;
 
// zero-valued integer
signed int z = 0;
 
// positve valued char
signed char  sin_1 = 12;
 
// zero-valued integer
signed char  sin_2 = 0;
 
// negative valued char
signed char  sin_3 = -12;

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,

// positive valued integer
unsigned int x = 2;
 
// positve valued char
unsigned char unsin_1 = 85;

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,

long int y = 26936;

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:-

long double y = 26936.124;

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:-

long long int j = 26936;

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,

short x = 4590;

Here, x is a short integer variable. Note: short is equivalent to short int.

How's article quality?

Page Contents