你如何找到整数类型在 C++ 中可以表示的值的范围?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/357629/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you find the range of values that integer types can represent in C++?
提问by Bill the Lizard
The size and range of the integer value types in C++ are platform specific. Values found on most 32-bit systems can be found at Variables. Data Types. - C++ Documentation. How do you determine what the actual size and range are for your specific system?
C++ 中整数值类型的大小和范围是特定于平台的。大多数 32 位系统上的值都可以在变量中找到。数据类型。- C++ 文档。您如何确定特定系统的实际尺寸和范围是多少?
回答by Adam
C Style
C风格
limits.h contains the min and max values for ints as well as other data types which should be exactly what you need:
limit.h 包含整数的最小值和最大值以及其他数据类型,这应该正是您所需要的:
#include <limits.h> // C header
#include <climits> // C++ header
// Constant containing the minimum value of a signed integer (–2,147,483,648)
INT_MIN;
// Constant containing the maximum value of a signed integer (+2,147,483,647)
INT_MAX;
For a complete list of constants and their common values check out: Wikipedia - limits.h
有关常量及其常用值的完整列表,请查看:维基百科 - limits.h
C++ Style
C++ 风格
There is a template based C++ method as other commenters have mentioned using:
正如其他评论者提到的那样,有一个基于模板的 C++ 方法:
#include <limits>
std::numeric_limits
which looks like:
看起来像:
std::numeric_limits<int>::max();
and it can even do craftier things like determine the number of digits possible or whether the data type is signed or not:
它甚至可以做一些更狡猾的事情,比如确定可能的位数或数据类型是否有符号:
// Number of digits for decimal (base 10)
std::numeric_limits<char>::digits10;
// Number of digits for binary
std::numeric_limits<char>::digits;
std::numeric_limits<unsigned int>::is_signed;
回答by Nemanja Trifunovic
Take a look at std::numeric_limits
看一眼 std::numeric_limits
回答by Doug T.
Why not just be sure and use boost's numeric types?
为什么不只是确定并使用 boost 的数字类型?
ie:
IE:
boost::uint32_t
boost::int32_t
etc
等等
回答by Marc
You can use the types defined in stdint.h(or cstdint, if you are using C++), which are part of the C99 standard. It defines types with such names as int32_t, uint8_t, int64_t, an so on, which are guaranteed to be portable and platform independent.
您可以使用stdint.h(或cstdint,如果您使用 C++)中定义的类型,它们是 C99 标准的一部分。它定义了具有诸如int32_t、uint8_t、int64_t等名称的类型,这些类型保证可移植且独立于平台。
For more information: stdint.h
更多信息:stdint.h
回答by Bill the Lizard
Use the sizeof()
operator in C++ to determine the size (in bytes) of a value type. The standard library header file limits.h contains the range limits for integer value types. You can run the following program to learn the size and range limits for integer types on your system.
使用sizeof()
C++ 中的运算符来确定值类型的大小(以字节为单位)。标准库头文件limits.h 包含整数值类型的范围限制。您可以运行以下程序来了解系统上整数类型的大小和范围限制。
#include <stdlib.h>
#include <iostream>
#include <limits>
using namespace std;
int main(int argc, char** argv) {
cout << "\nCharacter Types" << endl;
cout << "Size of character type is " << sizeof(char) << " byte." << endl;
cout << "Signed char min: " << SCHAR_MIN << endl;
cout << "Signed char max: " << SCHAR_MAX << endl;
cout << "Unsigned char min: 0" << endl;
cout << "Unsigned char max: " << UCHAR_MAX << endl;
cout << "\nShort Int Types" << endl;
cout << "Size of short int type is " << sizeof(short) << " bytes." << endl;
cout << "Signed short min: " << SHRT_MIN << endl;
cout << "Signed short max: " << SHRT_MAX << endl;
cout << "Unsigned short min: 0" << endl;
cout << "Unsigned short max: " << USHRT_MAX << endl;
cout << "\nInt Types" << endl;
cout << "Size of int type is " << sizeof(int) << " bytes." << endl;
cout << "Signed int min: " << INT_MIN << endl;
cout << "Signed int max: " << INT_MAX << endl;
cout << "Unsigned int min: 0" << endl;
cout << "Unsigned int max: " << UINT_MAX << endl;
cout << "\nLong Int Types" << endl;
cout << "Size of long int type is " << sizeof(long) << " bytes." << endl;
cout << "Signed long min: " << LONG_MIN << endl;
cout << "Signed long max: " << LONG_MAX << endl;
cout << "Unsigned long min: 0" << endl;
cout << "Unsigned long max: " << ULONG_MAX << endl;
return (EXIT_SUCCESS);
}
回答by Th.Srinivas
#include<stdio.h>
#include<limits.h>
void main()
{
printf(" signed data types " );
printf(" int min : %d ", INT_MIN); // INT_MIN, INT_MAX, SCHAR_MIN, SCHAR_MAX ....etc
printf(" int max : %d ",INT_MAX);// pre defined constants to get the values of datatypes
printf(" signed char min : %d ", SCHAR_MIN);
printf(" signed char max : %d ", SCHAR_MAX);
// similarly for un_signed
// use %u for control charter, and UINT_MAX, UCHAR_MAX, USHRT_MAX, ULONG_MAX.
}
回答by Shaohong Li
Bitwise operations can be used to find the number of bits and range of int in a platform. Here is a sample I wrote to test the range of int on my machine.
按位运算可用于查找平台中 int 的位数和范围。这是我编写的一个示例,用于在我的机器上测试 int 的范围。
#include <iostream>
using namespace std;
void print_int_range() {
int i=1;
int nOfBits=0;
while (i != 0) {
i = i << 1;
nOfBits++;
}
cout << "int has " << nOfBits << " bits" << endl;
cout << "mininum int: " << (1 << (nOfBits - 1)) << ", maximum int: " << ~(1 << (nOfBits - 1)) << endl;
}
void print_unsigned_int_range() {
unsigned int i=1;
int nOfBits=0;
while (i != 0) {
i = i << 1;
nOfBits++;
}
cout << "unsigned int has " << nOfBits << " bits" << endl;
cout << "mininum int: " << (0) << ", maximum int: " << (unsigned int) (~0) << endl;
}
int main() {
print_int_range();
print_unsigned_int_range();
}
And here is my output:
这是我的输出:
int has 32 bits
mininum int: -2147483648, maximum int: 2147483647
unsigned int has 32 bits
mininum int: 0, maximum int: 4294967295
回答by Shaohong Li
You can get the range of any data type by applying the following formulla:
您可以通过应用以下公式来获取任何数据类型的范围:
[-2 power (N-1)] to { [+2 power (N-1)] - 1 }
[-2 次幂 (N-1)] 到 { [+2 次幂 (N-1)] - 1 }
Where "N" is the width of data type, for example in JAVA the width of int is 32,hence N = 32.
其中“N”是数据类型的宽度,例如在JAVA中int的宽度是32,因此N = 32。
Try this out you will get it.
试试这个你会明白的。
回答by Robert
sizeof(int)