乐闻世界logo
搜索文章和话题

What range of values can integer types store in C++?

1个答案

1

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:

  1. int:

    • Typically 32 bits (though on some systems it may be 16 bits or larger)
    • The range for signed int is approximately -2,147,483,648 to 2,147,483,647
    • The range for unsigned int is 0 to 4,294,967,295
  2. short (short int):

    • Typically 16 bits
    • The range for signed short is -32,768 to 32,767
    • The range for unsigned short is 0 to 65,535
  3. 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
  4. long long (long long int):

    • Typically 64 bits
    • The range for signed long long is -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
    • The range for unsigned long long is 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 回复

你的答案