C++ Modifier Types
C++ allows modifiers to be placed before the char, int, and double data types. Modifiers are used to alter the basic meanings of these types, making them more suitable for various scenarios.
The following lists the data type modifiers:
- signed
- unsigned
- long
- short
Modifiers signed, unsigned, long, and short can be applied to integer types. signed and unsigned can be applied to character types, and long can be applied to double types.
Modifiers signed and unsigned can also be used as prefixes for long or short modifiers. For example: unsigned long int.
C++ allows the use of shorthand notation to declare unsigned short integers or unsigned long integers. You can omit the keyword int and simply use unsigned, short, or long, as int is implied. For example, the following two statements both declare unsigned integer variables:
unsigned x;
unsigned int y;
To understand the difference between signed and unsigned integer modifiers in C++, let's run the following short program:
Example
#include <iostream>
using namespace std;
/*
* This program demonstrates the difference between signed and unsigned integers
*/
int main()
{
short int i; // Signed short integer
short unsigned int j; // Unsigned short integer
j = 50000;
i = j;
cout << i << " " << j;
return 0;
}
When the above program runs, it outputs the following result:
-15536 50000
In the above result, the bit pattern of the unsigned short integer 50,000 is interpreted as the signed short integer -15,536.
Type Qualifiers in C++
Type qualifiers provide additional information about variables.
Qualifier | Meaning |
---|---|
const | Objects of type const cannot be modified during program execution. |
volatile | The volatile modifier tells the compiler not to optimize variables declared as volatile, allowing the program to read the variables directly from memory. Normally, the compiler optimizes variables by storing their values in registers for faster access. |
restrict | A pointer modified by restrict is the only means of accessing the object it points to. The restrict qualifier was added only in C99. |