Modifiers in C++

Video Tutorial
FREE
Datatype Modifiers thumbnail
This video belongs to
C++ Course: Learn the Essentials
14 modules
Certificate
Topics Covered

Overview

There are different types of modifiers in C++, also known as data type modifiers. These modifiers in C++ are used with data types like int, char, and float preceding to them. These types of modifiers are useful for increasing and decreasing the memory size.

Introduction to Modifiers in C++

The range of integer datatype is from -2,147,483,647 to 2,147,483,647. This range is because of the integer's size which is 4 bytes in the memory.

There are some situations where we need to modify the inbuilt data-types like int by increasing the size to store big numbers which are not fitting in the given range or by decreasing the size to save some memory in the program.

There are some modifiers in C++ that allow us to alter the meaning of base types like int, char, and double. The modifiers are as follows:

  1. signed - Used for both positive and negative values
  2. unsigned - Used for only positive values
  3. long - Used to increase the size of data-types
  4. short - Used to reduce the size of data-types

In this article, we will explore how to use these modifiers to modify or alter the meaning of base data types in C++.

Types of modifiers in C++ with Integer data-type

Suppose we are working with an application in C++ and there is a requirement to store the value 21474836478 which is not in range of normal int, then we will get the value as follows:

Code:

Output:

Now because the value in number is out of range we got the negative value which is next in the range of integer.

To store the value which is out of range we need to modify regular int to long long int or unsigned int so that it can store that value because the long long int range is greater as shown in the below table.

code:

Output:

Similarly, if we want to store small values in the range of -32,768 to 32,767 we can use short int instead of int to save 2 bytes of memory space.

Below is the table to show the storage size and range of different modifiers with the int data type -

Data TypeStorage SizeRange
short int2 bytes-32,768 to 32,767
unsigned short int2 bytes0 to 65,536
unsigned int4 bytes0 to 4,294,967,295
int4 bytes-2,147,483,648 to 2,147,483,647
long int4 bytes-2,147,483,648 to 2,147,483,647
unsigned long int4 bytes0 to 4,294,967,295
long long int8 bytes-(2^63) to (2^63)-1
unsigned long long int8 bytes0 to 18,446,744,073,709,551,615

In the above table, there are four type modifiers namely signed, unsigned, long, and short. The range of signed integers is the same as normal integers because they both are the same and it is optional to write signed preceding to int.

If there are no negative values in use, then we can use an unsigned type modifier and double the range of positive values as shown in the above table.

Types of Modifiers in C++ with character data-type

The range of char type is from -128 to 127, it stores the numbers, which are known as ASCII Value. Each value has some meaning associated with characters and symbols.

However, for custom use, if there is a need to change the range we can do it as shown in the following table:

Data TypeStorage SizeRange
char1 byte-128 to 127
signed char1 byte-128 to 127
unsigned char1 byte0 to 255

Types of Modifiers in C++ with floating point data-types

The double is also a type under floating-point datatype where double is of 8 bytes and float is of 4 bytes.

Below is the table with the size and range of modifiers with floating-point data types.

Data TypeStorage SizeRange
float4 bytes1.2E-38 to 3.4E+38 (6 decimal places)
double8 bytes2.3E-308 to 1.7E+308 (15 decimal places)
long double12 bytes3.4E-4932 to 1.1E+4932 (19 decimal places)

Type qualifiers in C++

Type qualifiers are used to provide a piece of additional information about the variables they precede.

For example, const is a type qualifier that is used with int as const int which represents the constant integer value that can't be changed during the execution of a program.

Code:

Output:

Below is the list of type qualifiers in C++:

Type qualifierMeaning of Qualifier
constIf this type qualifier is used, then the value can't be changed during the execution of the program.
mutableThis qualifier is applied to non-static class members of the non-reference and non-const type when we want mutable characteristics.
volatileThis qualifier is used to tell the compiler that a variable's value may be changed in a way explicitly specified by the program

Conclusion

In this article,

  1. We saw different types of type modifiers and their meaning
  2. Memory size and the range of modifiers with different data types
  3. We also saw type modifiers and their meaning