C++ Modifiers and Data Types
This article will help you understand the concept of C++ modifiers and its data types. Let’s understand them with the help of suitable examples.
In C++, various kinds of data type modifiers are referred to as type qualifiers. These modifiers are applied before data types such as int, char, and float to alter their memory size. The purpose of these type qualifiers is to either increase or decrease the memory size of the data type.
Must read: A Beginner’s Guide to C++ Programming Language
In this tutorial, we will discuss C++ data types and their modifiers. We will be covering the following sections:
- Introduction to C++ Modifiers
- Modifiers in C++ with Int Data Type
- Modifiers in C++ with Char Data Type
- Modifiers in C++ with Floating Data Type
- Type Qualifiers in C++
Must Check: C++ Online Courses &Certification
Introduction to C++ Modifiers
The range of the integer data type in C++ is from -2,147,483,647 to 2,147,483,647, due to its memory size of 4 bytes.
Sometimes, we may need to modify the built-in data types, such as int, to store larger numbers or save memory.
C++ provides several modifiers, including signed, unsigned, long, and short, that can change the meaning of base data types like int, char, and double.
The signed modifier can be used for both positive and negative values, the unsigned modifier is used for positive values only, the long modifier increases the size of data types, and the short modifier reduces their size.
Must read: Class and objects in C++
Best-suited C++ courses for you
Learn C++ with these high-rated online courses
Modifiers in C++ with Int Data Type
In the case of the int data type, there are four primary modifiers: signed, unsigned, long, and short.
- Signed: By default, an int in C++ is signed, meaning it can store both positive and negative values.
An example of this would be:
signed int a = -10;
- Unsigned: If you only want to store positive values, you can use the unsigned modifier. This increases the range of positive values stored in an int and disallows negative values.
Example
unsigned int a = 10;
- Long: The long modifier increases the size of an int from 4 bytes to 8 bytes. This increases the range of values stored in the variable and takes up more memory.
An example of this would be:
long int a = 100000;
- Short: The short modifier reduces the size of an int from 4 bytes to 2 bytes. This decreases the range of values that can be stored in the variable but also saves memory.
An example of this would be:
short int a = 10;
It is important to choose the right modifier based on the requirements of your program to ensure that the data is stored correctly and efficiently.
In C++, combining multiple modifiers for a single variable is possible. For example, you can use both the unsigned and long modifiers together like this:
unsigned long int a = 100000;
However, it is important to be mindful of the order in which you apply the modifiers. The signed and unsigned keywords must come before the long and short keywords. For example, the following code would be valid:
unsigned long int a = 100000;
However, the following code would not be valid:
long unsigned int a = 100000;
Check out- Constructors in C++ and its Types
Here is a table showing the storage size and range of values for the different modifiers with the int data type in C++:
Modifier | Storage Size | Range of Values |
int | 4 bytes | -2,147,483,648 to 2,147,483,647 |
signed int | 4 bytes | -2,147,483,648 to 2,147,483,647 |
unsigned int | 4 bytes | 0 to 4,294,967,295 |
short int | 2 bytes | -32,768 to 32,767 |
unsigned short int | 2 bytes | 0 to 65,535 |
long int | 8 bytes | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
unsigned long int | 8 bytes | 0 to 18,446,744,073,709,551,615 |
Must read: Convert char to int in C++ Using Different Methods
Modifiers in C++ with Char Data Type
Modifiers in C++ can also be used with the char data type. The char data type is used to store single characters and typically takes up 1 byte of memory. However, the memory size and range of values stored in a char can be modified using the signed and unsigned keywords.
- Signed: By default, a char in C++ is signed, meaning it can store both positive and negative values. This also means it can store various characters, including letters, numbers, and symbols.
An example of this would be:
signed char c = 'a';
- Unsigned: If you only want to store positive values, you can use the unsigned keyword. This increases the range of positive values stored in a char but also disallows negative values.
Example:
unsigned char c = 'a';
It is important to choose the right modifier based on the requirements of your program. For example, if you are storing ASCII values in a char, using the unsigned modifier would be more appropriate as ASCII values are positive.
Here is a table showing the storage size and range of values for the different modifiers with the int data type in C++:
Modifier | Storage Size | Range of Values |
int | 4 bytes | -2,147,483,648 to 2,147,483,647 |
signed int | 4 bytes | -2,147,483,648 to 2,147,483,647 |
unsigned int | 4 bytes | 0 to 4,294,967,295 |
short int | 2 bytes | -32,768 to 32,767 |
unsigned short int | 2 bytes | 0 to 65,535 |
long int | 8 bytes | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
unsigned long int | 8 bytes | 0 to 18,446,744,073,709,551,615 |
Check out: A Brief Introduction to Using Queues in C++
Modifiers in C++ with Floating Data Type
The double data type is a subtype of the floating-point data type, and it has a size of 8 bytes, whereas the float data type has a size of 4 bytes.
Here is a table showing the storage size and range of values for the floating-point data types in C++:
Data Type | Storage Size | Approximate Range of Values |
float | 4 bytes | +/- 3.4 x 10^38 |
double | 8 bytes | +/- 1.7 x 10^308 |
long double | 10 bytes (or more) | +/- 1.2 x 10^4932 |
Note: The exact range of values for floating-point data types may vary depending on the system and compiler being used. It is also worth noting that floating-point numbers have a limited precision, so the actual range of representable values may be smaller than the theoretical range.
Also explore: Default Arguments in C++
Type Qualifiers in C++
Type qualifiers are used to provide additional information about the variables they modify. For instance, the “const” qualifier is used to specify that a variable is a constant integer value, and its value cannot be changed during the execution of a program.
For example, the code const int x = 10; declares a constant integer variable “x” with the value 10.
Below is the table of type qualifiers in C++:
Type Qualifier | Description |
const | Indicates that a variable is a constant and its value cannot be changed during the execution of a program. |
volatile | Indicates that a variable’s value may change unexpectedly, for example, due to hardware or external factors. |
restrict | Indicates that a variable is the only reference to a specific memory location, allowing for optimized access. |
Explore: Top C++ Interview Questions and Answers for 2023
Endnotes
The use of modifiers in C++ allows you to change the meaning of base data types, and adjust the memory size and range of values that can be stored in a variable. Therefore by understanding the different modifiers available, you can make informed decisions about storing your data most efficiently and effectively.
I hope this article helped you grasp the concept of C++ modifiers. Explore our C++ articles to learn more about the language and consolidate your knowledge of the fundamentals.
Contributed By: Prerna Singh
This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio