如何找到整数类型在C ++中可以表示的值范围?

时间:2008-12-10 21:13:26

标签: c++ types

C ++中整数值类型的大小和范围是特定于平台的。大多数32位系统上的值可以在Variables. Data Types. - C++ Documentation找到。您如何确定特定系统的实际大小和范围?

9 个答案:

答案 0 :(得分:38)

C风格

limits.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;

有关常量及其常用值的完整列表,请查看:Wikipedia - limits.h


C ++ Style

有一个基于模板的C ++方法,正如其他评论者提到的那样:

  #include <limits>

  std::numeric_limits

看起来像:

  std::numeric_limits<int>::max();

它甚至可以做更多的工具,例如确定可能的数字位数或数据类型是否已签名:

  // 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;

答案 1 :(得分:9)

查看std::numeric_limits

答案 2 :(得分:6)

为什么不确定并使用boost的数字类型?

即:

boost::uint32_t
boost::int32_t

答案 3 :(得分:3)

您可以使用 stdint.h (或 cstdint ,如果您使用的是C ++)中定义的类型,它们是C99标准的一部分。它定义的类型名称为 int32_t uint8_t int64_t ,依此类推,保证可移植且与平台无关。

有关详细信息:stdint.h

答案 4 :(得分:2)

使用C ++中的sizeof()运算符来确定值类型的大小(以字节为单位)。标准库头文件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);
}

答案 5 :(得分:1)

#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.  
}

答案 6 :(得分:0)

您可以通过应用以下formulla来获取任何数据类型的范围:

[ - 2次幂(N-1)]至{[+2次幂(N-1)] - 1}

其中“N”是数据类型的宽度,例如在JAVA中,int的宽度为32,因此N = 32。

尝试一下,你会得到它。

答案 7 :(得分:0)

按位运算可用于查找平台中的位数和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();
    }

这是我的输出:

int has 32 bits 
mininum int: -2147483648, maximum int: 2147483647 
unsigned int has 32 bits 
mininum int: 0, maximum int: 4294967295

答案 8 :(得分:-5)

sizeof(int)
寻找你感兴趣的贴纸↓↓↓
豫ICP备18024241号-1