Data Type Modifiers in C++

In this tutorial, we will learn about data type modifiers in C++.

As we already know, there are four types of built-in data types in C++, namely

  1. int
  2. char
  3. float
  4. double

Various Data Type Modifiers in C++

The memory occupied by each is fixed. However, to make a program more flexible and memory efficient, there are some modifiers available in C++. They allow us to assign to a variable a specific data type such that it uses no more than the required space of memory. Thus, we have, a modifier or a data type modifier is the keyword which modifies the meaning of the base data type to fit the situation more precisely.

In C++, we have four types of modifiers, namely,

  1. short
  2. long
  3. signed
  4. unsigned.

The modifier signed, as its name suggests, declares that the variable can have both positive as well as negative values.  Similarly, modifier unsigned declares that the variable can have only positive values. If we know that for a particular variable we won’t be using any negative values, it is useful to declare that variable as unsigned. This allows the variable to have a larger range.

The modifier short decreases the number of bytes occupied and hence, save up some unnecessary memory space. Using short makes the variable of the minimum size of that respective data type. On the other hand, long increases the number of bytes occupied and hence increases the range of values for that variable. Using long makes the variable of the maximum size which that data type can hold.

With int, we can use all of the modifiers as well as there combinations. With char, we can use only signed and unsigned. This is because char for a single variable behaves like an integer type also. None of the modifiers can be used with the float data type. This is due to the fact that short float is equivalent to int and long float is equivalent to double. With double, we can only use the long modifier.

Modifiers, size and ranges of data types in C++

Below is a table showing the combinations of various data types and modifiers, along with the size and their respective ranges.

For numerical values

Sr. No.

Type

Size (in bytes)

Minimal Range

1 short

2

-32768 to 32767
2 unsigned short

2

0 to 65,535
3 signed short

2

same as short
4 int

2

-32768 to 32767
5 unsigned int

2

0 to 65,535
6 signed int

2

same as int
7 long

4

-2,147,483,648 to 2,147,483,647
8 unsigned long

4

0 to 4,294,967,295
9 signed long

4

same as long

For char :

Sr. No.

Type

Size (in bytes)

Minimal Range

1 char

1

-128 to 127
2 unsigned char

1

0 to 256
3 signed char

1

same as char

For float/double :

Sr. No.

Type

Size (in bytes)

Minimal Range

1 float

4

3.4 × 10-38 to 3.4 × 1038 – 1
2 double

8

1.7 × 10-308 to 1.7 × 10308 – 1
3 long double

10

3.4 × 10-4932 to 3.4 × 104932 – 1

C++ Program :

#include <iostream>
using namespace std;

int main()
{
  short int a;
  long int b = 70000;
  unsigned short c;
  signed short d;
  a = b;			//value of a, b, c and d
  c = b;                        //is assigned same 
  d = b;
  cout << " short int a = " << a << endl;        //outputs of the values
  cout << " long int b = " << b << endl;
  cout << " unsigned short c = " << c << endl;
  cout << " signed short d = " << d << endl;
  
  return 0;
}

Output :

short int a = 4464
long int b = 70000
unsigned short c = 4464
signed short d = 4464

 

Hope this was helpful. Enjoy Coding!

Also learn :

Bitwise Operators in C or C++

Different Types of Constants in C++

Leave a Reply

Your email address will not be published. Required fields are marked *