English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C++ qualifier types

in C ++in programming,type qualifiersused to change the basicData typemeaning.

C++ allow for char, int, and double Place qualifiers before data types. Qualifiers are used to change the meaning of basic types, so they can better meet various requirements.

The following lists data type qualifiers:

  • signed

  • unsigned

  • long

  • short

Modifier signed, unsigned, long, and short can be applied to integer types,signed and unsigned can be applied to character types,long can be applied to double precision types.

Modifier signed and unsigned can also be used as long or short as a prefix to the qualifier. For example:unsigned long int.

C++ allow the use of shorthand notation to declareunsigned short integerorunsigned long integer. You can omit int and just write the word unsigned, short or unsigned, longint is implied. For example, the following two statements both declare unsigned integer variables.

unsigned x;
unsigned int y;

To understand C++ To explain the difference between signed and unsigned integer qualifiers, let's run the following short program:

#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, the following results will be output:

-15536 50000

In the above results, the unsigned short integer 5The bit pattern 0,000 is interpreted as a signed short integer -15,536.

This is a list of type modifiers:

Data typesizeMeaning
signed int4 bytes
for integers (equivalent to int)
unsigned int4 bytescan only store non-negative integers
short2 bytesfor small integers (range-32768to32767)
longat least4bytesfor large integers (equal to long int)
unsigned long4 bytesfor large positive integers or 0 (equal to unsigned long int)
long long8 bytesfor very large integers (equal to long long int).
unsigned long long8 bytesfor very large positive integers or 0 (equal to unsigned long long int)
long double8bytesfor large floating-point numbers
signed char1 bytesfor characters (guaranteed range-127The guaranteed range of integer values that can be stored by signed char is127)
unsigned char1 bytesfor characters (rangeFrom255)

short type modifier

We can use the shortSmall integers(range−32,767to+32,767You may have guessed, signed char can store both positive and negative integers, while unsigned char can only store positive integers (including

For example,

// Small integers
short a = 12345;

In this case, a is a short integer variable.

The signed char or unsigned char in short is equivalent to short int.

long type modifier

If we need to store aLarge integers(range-2147483647to2147483647If the number is within a certain range,

// Large integers
long b = 123456;

The signed char or unsigned char in long is equivalent to long int.

The long type modifier can also be used with double variables.

// Large floating-point numbers
long double c = 0.333333333333333333L;

The signed char or unsigned char inTo represent long double, we use the L suffix. If we do not use the suffix L, the double is converted to the value long double (which may cause data loss).

long long

long can be repeated twice to create the long long type. This type is used for numbers larger than long. The long long type modifier can only be used with int.

For example,

// long long int
long long num = 12345678;

signed and unsigned modifiers

Signed (signed) variables can includepositiveintegerand negativeinteger, includingzero。For example,

// positive integer value
signed int x = 23;
// negative integer value
signed int y = -13;
// zero-valued integer
signed int z = 0;

Here,

  • x holds apositive valueinteger

  • y holds anegative valueinteger

  • z holdszero-valuedinteger

unsigned variables(unsigned)can only contain non-negative integers. For example,

// positive-valued integer
unsigned int x = 2;
unsigned int y = 0;

Here,

  • x holds a positive-valued integer

  • y holds zero

Typically, int variables can store a range from -2,147,483,648 The guaranteed range of integer values that can be stored by signed char is 2,147,483,647 values. While unsigned int variables can store a range of From 4,294,967,295 values.

Signed (signed), unsigned (unsigned), and ordinary character (plain char)

C ++There are3There are different char types: char, signed char, and unsigned char. In fact, there are basically only two types: signed char and unsigned char.

This is because even though char is different from the standard C ++are not the same, and different compilers will also treat char as signed char or unsigned char according to their preferences.

The signed char or unsigned char inNote:When we only use char instead of signed char or unsigned char, this type is called.

plain char (ordinary character)0You may have guessed, signed char can store both positive and negative integers, while unsigned char can only store positive integers (including

)。-127The guaranteed range of integer values that can be stored by signed char is127toFrom255.

// Ordinary characters
char plain_1 = 65;
char plain_2 = 0;
// Ordinary characters with a negative value
// This may cause some compilers to have issues
char plain_3 = -56;
// signed char
signed char sin_1 = 12;
signed char sin_2 = 0;
signed char sin_3 = -12;
// unsigned char
unsigned char unsin_1 = -85;
unsigned char unsin_2 = 0;

Here,

  • plain_1 Holding a positive-valued integer

  • plain_2 Holding a zero-valued integer

  • sin_1 Holding a positive-valued integer

  • sin_2 Holding a zero-valued integer

  • sin_3 Holding a negative-valued integer

  • unsin_1 Holding a negative-valued integer

  • unsin_2 Holding a zero-valued integer

Note: It is best not to use ordinary characters for numeric operations; use signed char or unsigned char instead. Pure characters should only be used to store character values.

C++ Type specifiers in

Type specifiers provide additional information about variables.

Type SpecifierMeaning
constconst The type of object modified cannot be modified during program execution.
volatileModifier volatile telling the compiler that it does not need to optimize the volatile declared variables, allowing the program to directly read the variables from memory. For general variables, the compiler will optimize and store the variable value in a register to speed up read and write efficiency.
restrictBy restrict The pointer modified is the only way to access the object it points to. Only C99 Added a new type specifier restrict.