In C++, the range of values that integer types can store depends on the size of the type (i.e., the number of bits it occupies) and whether it is signed or unsigned. Below are the common integer types in C++ and their ranges:
-
int:- Typically 32 bits (though on some systems it may be 16 bits or larger)
- The range for signed
intis approximately -2,147,483,648 to 2,147,483,647 - The range for unsigned
intis 0 to 4,294,967,295
-
short(short int):- Typically 16 bits
- The range for signed
shortis -32,768 to 32,767 - The range for unsigned
shortis 0 to 65,535
-
long(long int):- On most modern systems, it is at least 32 bits, and on many systems it is 64 bits
- For signed
long, on 32-bit systems the range is -2,147,483,648 to 2,147,483,647, and on 64-bit systems it is -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 - For unsigned
long, on 32-bit systems the range is 0 to 4,294,967,295, and on 64-bit systems it is 0 to 18,446,744,073,709,551,615
-
long long(long long int):- Typically 64 bits
- The range for signed
long longis -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 - The range for unsigned
long longis 0 to 18,446,744,073,709,551,615
For example, if you are developing an application that requires handling very large data quantities, such as statistics on the detailed information of all residents in a country, you might choose to use the unsigned long long type, as it provides a sufficiently large range to ensure that any possible population count can be stored.
2024年6月29日 12:07 回复