在C或C++中是否有标准宏表示int32_t、int64_t的最大值和最小值

Is there a standard macro in C or C++ represent the max and min value of int32_t, int64_t?

本文关键字:int64 最小值 最大值 int32 表示 C++ 是否 标准      更新时间:2023-10-16

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

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

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

等等。在C中,报头<stdint.h>定义了INT32_MININT32_MAX等。这些在C++<cstdint>报头中也可用。

在C++中,您可以使用此标头中声明的标头<limits>和类别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中,您应该包括标题<stdint.h>,并使用标题中定义的相应宏,例如

INT32_MIN
INT32_MAX
INT64_MIN
INT64_MAX