如何找到整数类型在C ++中可以表示的值的范围? [英] How do you find the range of values that integer types can represent in C++?

查看:197
本文介绍了如何找到整数类型在C ++中可以表示的值的范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++中的整数值类型的大小和范围是平台特定的。在大多数32位系统上找到的值可以在变量中找到。数据类型。 - C ++文档

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风格



limits.h包含int和其他数据类型的最小值和最大值,这些数据类型应该是您需要的:

C Style

limits.h contains the min and max values for ints as well as other data types which should be exactly what you need:

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

有关常量及其常用值的完整列表,请参阅:维基百科 - limits.h

For a complete list of constants and their common values check out: Wikipedia - limits.h


有其他注释者提到的基于模板的C ++方法:

There is a template based C++ method as other commenters have mentioned using:

  #include <limits>

  std::numeric_limits

看起来像:

  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;

这篇关于如何找到整数类型在C ++中可以表示的值的范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆