【问题标题】:Is there a standard macro in C or C++ represent the max and min value of int32_t, int64_t?C 或 C++ 中是否有标准宏表示 int32_t、int64_t 的最大值和最小值?
【发布时间】:2015-10-19 17:08:13
【问题描述】:

C 或 C++ 中是否有任何宏表示 int32_t 和 int64_t 的最大值和最小值?我知道它可以从字面上自己定义,但是如果有一个标准的宏就更好了。请注意,我不是在询问 int、long 等的最大值。但是intXX_t

【问题讨论】:

标签: c++ c


【解决方案1】:

在 C++ 中,<limits> 标头中有 std::numeric_limits::min()std::numeric_limits::max()

std::cout << std::numeric_limits<std::int32_t>::min() << std::endl;

等等。在 C 中,标头 &lt;stdint.h&gt; 定义了 INT32_MININT32_MAX 等。这些在 C++ 中也可用 &lt;cstdint&gt; 标头。

【讨论】:

    【解决方案2】:

    在 C++ 中,您可以使用在此标头中声明的标头 &lt;limits&gt; 和类 std::numeric_limts

    要知道 int32_t 和 int64_t 类型的最大值和最小值,您可以编写示例

    #include <cstdint>
    #include <limits>
    
    //...
    
    std::cout << std::numeric_limits<int32_t>::max() << std::endl;
    std::cout << std::numeric_limits<int32_t>::min() << std::endl;
    std::cout << std::numeric_limits<int64_t>::max() << std::endl;
    std::cout << std::numeric_limits<int64_t>::min() << std::endl;
    

    在 C 中,您应该包含标题 &lt;stdint.h&gt; 并使用标题中定义的相应宏,例如

    INT32_MIN
    INT32_MAX
    INT64_MIN
    INT64_MAX
    

    【讨论】:

      猜你喜欢
      • 2011-03-27
      • 1970-01-01
      • 1970-01-01
      • 2015-05-04
      • 1970-01-01
      • 2013-03-07
      • 2014-03-22
      • 2016-05-26
      • 2021-11-27
      相关资源
      最近更新 更多