最小宽度整数常量的函数宏
INT8_CINT16_CINT32_CINT64_C | 展开成拥有其实参所指定的值且类型分别为 int_least8_t、int_least16_t、int_least32_t、int_least64_t 的整数常量表达式 (宏函数) |
INTMAX_C | 展开成拥有其实参所指定的值且类型为 intmax_t 的整数常量表达式 (宏函数) |
UINT8_CUINT16_CUINT32_CUINT64_C | 展开成拥有其实参所指定的值且类型分别为 uint_least8_t、uint_least16_t、uint_least32_t、uint_least64_t 的整数常量表达式 (宏函数) |
UINTMAX_C | 展开成拥有其实参所指定的值且类型为 uintmax_t 的整数常量表达式 (宏函数) |
调用示例
#include <iostream>
#include <cstdint>
int main()
{
//展开成拥有其实参所指定的值且类型分别为 int_least8_t、int_least16_t、
//int_least32_t、int_least64_t 的整数常量表达式
std::cout << "INT8_C(8): " << INT8_C(8) << std::endl;
std::cout << "INT16_C(16): " << INT16_C(16) << std::endl;
std::cout << "INT32_C(32): " << INT32_C(32) << std::endl;
std::cout << "INT64_C(64): " << INT64_C(64) << std::endl;
std::cout << std::endl;
//展开成拥有其实参所指定的值且类型分别为 uint_least8_t、uint_least16_t、
//uint_least32_t、uint_least64_t 的整数常量表达式
std::cout << "UINT8_C(8): " << UINT8_C(8) << std::endl;
std::cout << "UINT16_C(16): " << UINT16_C(16) << std::endl;
std::cout << "UINT32_C(32): " << UINT32_C(32) << std::endl;
std::cout << "UINT64_C(64): " << UINT64_C(64) << std::endl;
std::cout << std::endl;
//展开成拥有其实参所指定的值且类型为 intmax_t 的整数常量表达式
std::cout << "INTMAX_C(32): " << INTMAX_C(32) << std::endl;
//展开成拥有其实参所指定的值且类型为 uintmax_t 的整数常量表达式
std::cout << "UINTMAX_C(64): " << UINTMAX_C(64) << std::endl;
std::cout << std::endl;
return 0;
}
输出
格式化宏常量
定义于头文件 <cinttypes>
std::fprintf 系列函数的格式化宏常量
unsigned int 的等价者 | 描述 | 数据类型的宏 | ||||
---|---|---|---|---|---|---|
std::intx_t | std::int_leastx_t | std::int_fastx_t | std::intmax_t | std::intptr_t | ||
x = 8 、 16 、 32 或 64 | ||||||
d | 有符号十进制整数值的输入 | PRIdx | PRIdLEASTx | PRIdFASTx | PRIdMAX | PRIdPTR |
i | PRIix | PRIiLEASTx | PRIiFASTx | PRIiMAX | PRIiPTR | |
u | 无符号十进制整数值的输入 | PRIux | PRIuLEASTx | PRIuFASTx | PRIuMAX | PRIuPTR |
o | 无符号八进制整数值的输入 | PRIox | PRIoLEASTx | PRIoFASTx | PRIoMAX | PRIoPTR |
x | 无符号小写十六进制整数值的输入 | PRIxx | PRIxLEASTx | PRIxFASTx | PRIxMAX | PRIxPTR |
X | 无符号大写十六进制整数值的输入 | PRIXx | PRIXLEASTx | PRIXFASTx | PRIXMAX | PRIXPTR |
std::fscanf 系列函数的格式化宏常量
或 unsigned int 的等价者 | 描述 | 数据类型的宏 | ||||
---|---|---|---|---|---|---|
std::intx_t | std::int_leastx_t | std::int_fastx_t | std::intmax_t | std::intptr_t | ||
x = 8 、 16 、 32 或 64 | ||||||
d | 有符号十进制整数值的输入 | SCNdx | SCNdLEASTx | SCNdFASTx | SCNdMAX | SCNdPTR |
i | 有符号整数值的输入 | SCNix | SCNiLEASTx | SCNiFASTx | SCNiMAX | SCNiPTR |
u | 无符号十进制整数值的输入 | SCNux | SCNuLEASTx | SCNuFASTx | SCNuMAX | SCNuPTR |
o | 无符号八进制整数值的输入 | SCNox | SCNoLEASTx | SCNoFASTx | SCNoMAX | SCNoPTR |
x | 无符号十六进制整数值的输入 | SCNxx | SCNxLEASTx | SCNxFASTx | SCNxMAX | SCNxPTR |
调用示例
#include <cstdio>
#include <cinttypes>
int main()
{
std::printf("INT8_MIN: %+" PRId8 "\n", INT8_MIN);
std::printf("INT16_MIN: %+" PRId16 "\n", INT16_MIN);
std::printf("INT32_MIN: %+" PRId32 "\n", INT32_MIN);
std::printf("INT64_MIN: %+" PRId64 "\n", INT64_MIN);
std::printf("\n");
std::printf("INT_LEAST8_MIN: %+" PRIdLEAST8 "\n", INT_LEAST8_MIN);
std::printf("INT_LEAST16_MIN: %+" PRIdLEAST16 "\n", INT_LEAST16_MIN);
std::printf("INT_LEAST32_MIN: %+" PRIdLEAST32 "\n", INT_LEAST32_MIN);
std::printf("INT_LEAST64_MIN: %+" PRIdLEAST64 "\n", INT_LEAST64_MIN);
std::printf("\n");
std::printf("INT_FAST8_MIN: %+" PRIdFAST8 "\n", INT_FAST8_MIN);
std::printf("INT_FAST16_MIN: %+" PRIdFAST16 "\n", INT_FAST16_MIN);
std::printf("INT_FAST32_MIN: %+" PRIdFAST32 "\n", INT_FAST32_MIN);
std::printf("INT_FAST64_MIN: %+" PRIdFAST64 "\n", INT_FAST64_MIN);
std::printf("\n");
std::printf("INT8_MIN: %+" PRIi8 "\n", INT8_MIN);
std::printf("INT16_MIN: %+" PRIi16 "\n", INT16_MIN);
std::printf("INT32_MIN: %+" PRIi32 "\n", INT32_MIN);
std::printf("INT64_MIN: %+" PRIi64 "\n", INT64_MIN);
std::printf("\n");
std::printf("INT_LEAST8_MIN: %+" PRIiLEAST8 "\n", INT_LEAST8_MIN);
std::printf("INT_LEAST16_MIN: %+" PRIiLEAST16 "\n", INT_LEAST16_MIN);
std::printf("INT_LEAST32_MIN: %+" PRIiLEAST32 "\n", INT_LEAST32_MIN);
std::printf("INT_LEAST64_MIN: %+" PRIiLEAST64 "\n", INT_LEAST64_MIN);
std::printf("\n");
std::printf("INT_FAST8_MIN: %+" PRIiFAST8 "\n", INT_FAST8_MIN);
std::printf("INT_FAST16_MIN: %+" PRIiFAST16 "\n", INT_FAST16_MIN);
std::printf("INT_FAST32_MIN: %+" PRIiFAST32 "\n", INT_FAST32_MIN);
std::printf("INT_FAST64_MIN: %+" PRIiFAST64 "\n", INT_FAST64_MIN);
std::printf("\n");
std::printf("INT8_MIN: %" PRIo8 "\n", INT8_MIN);
std::printf("INT16_MIN: %" PRIo16 "\n", INT16_MIN);
std::printf("INT32_MIN: %" PRIo32 "\n", INT32_MIN);
std::printf("INT64_MIN: %" PRIo64 "\n", INT64_MIN);
std::printf("\n");
std::printf("INT_LEAST8_MIN: %" PRIoLEAST8 "\n", INT_LEAST8_MIN);
std::printf("INT_LEAST16_MIN: %" PRIoLEAST16 "\n", INT_LEAST16_MIN);
std::printf("INT_LEAST32_MIN: %" PRIoLEAST32 "\n", INT_LEAST32_MIN);
std::printf("INT_LEAST64_MIN: %" PRIoLEAST64 "\n", INT_LEAST64_MIN);
std::printf("\n");
std::printf("INT_FAST8_MIN: %" PRIoFAST8 "\n", INT_FAST8_MIN);
std::printf("INT_FAST16_MIN: %" PRIoFAST16 "\n", INT_FAST16_MIN);
std::printf("INT_FAST32_MIN: %" PRIoFAST32 "\n", INT_FAST32_MIN);
std::printf("INT_FAST64_MIN: %" PRIoFAST64 "\n", INT_FAST64_MIN);
std::printf("\n");
std::printf("UINT8_MAX: %" PRIu8 "\n", UINT8_MAX);
std::printf("UINT16_MAX: %" PRIu16 "\n", UINT16_MAX);
std::printf("UINT32_MAX: %" PRIu32 "\n", UINT32_MAX);
std::printf("UINT64_MAX: %" PRIu64 "\n", UINT64_MAX);
std::printf("\n");
std::printf("UINT_LEAST8_MAX: %" PRIuLEAST8 "\n", UINT_LEAST8_MAX);
std::printf("UINT_LEAST16_MAX: %" PRIuLEAST16 "\n", UINT_LEAST16_MAX);
std::printf("UINT_LEAST32_MAX: %" PRIuLEAST32 "\n", UINT_LEAST32_MAX);
std::printf("UINT_LEAST64_MAX: %" PRIuLEAST64 "\n", UINT_LEAST64_MAX);
std::printf("\n");
std::printf("UINT_FAST8_MAX: %" PRIuFAST8 "\n", UINT_FAST8_MAX);
std::printf("UINT_FAST16_MAX: %" PRIuFAST16 "\n", UINT_FAST16_MAX);
std::printf("UINT_FAST32_MAX: %" PRIuFAST32 "\n", UINT_FAST32_MAX);
std::printf("UINT_FAST64_MAX: %" PRIuFAST64 "\n", UINT_FAST64_MAX);
std::printf("\n");
std::printf("UINT8_MAX: %" PRIx8 "\n", UINT8_MAX);
std::printf("UINT16_MAX: %" PRIx16 "\n", UINT16_MAX);
std::printf("UINT32_MAX: %" PRIx32 "\n", UINT32_MAX);
std::printf("UINT64_MAX: %" PRIx64 "\n", UINT64_MAX);
std::printf("\n");
std::printf("UINT_LEAST8_MAX: %" PRIxLEAST8 "\n", UINT_LEAST8_MAX);
std::printf("UINT_LEAST16_MAX: %" PRIxLEAST16 "\n", UINT_LEAST16_MAX);
std::printf("UINT_LEAST32_MAX: %" PRIxLEAST32 "\n", UINT_LEAST32_MAX);
std::printf("UINT_LEAST64_MAX: %" PRIxLEAST64 "\n", UINT_LEAST64_MAX);
std::printf("\n");
std::printf("UINT_FAST8_MAX: %" PRIxFAST8 "\n", UINT_FAST8_MAX);
std::printf("UINT_FAST16_MAX: %" PRIxFAST16 "\n", UINT_FAST16_MAX);
std::printf("UINT_FAST32_MAX: %" PRIxFAST32 "\n", UINT_FAST32_MAX);
std::printf("UINT_FAST64_MAX: %" PRIxFAST64 "\n", UINT_FAST64_MAX);
std::printf("\n");
return 0;
}
输出
INT8_MIN: -128
INT16_MIN: -32768
INT32_MIN: -2147483648
INT64_MIN: -9223372036854775808
INT_LEAST8_MIN: -128
INT_LEAST16_MIN: -32768
INT_LEAST32_MIN: -2147483648
INT_LEAST64_MIN: -9223372036854775808
INT_FAST8_MIN: -128
INT_FAST16_MIN: -32768
INT_FAST32_MIN: -2147483648
INT_FAST64_MIN: -9223372036854775808
INT8_MIN: -128
INT16_MIN: -32768
INT32_MIN: -2147483648
INT64_MIN: -9223372036854775808
INT_LEAST8_MIN: -128
INT_LEAST16_MIN: -32768
INT_LEAST32_MIN: -2147483648
INT_LEAST64_MIN: -9223372036854775808
INT_FAST8_MIN: -128
INT_FAST16_MIN: -32768
INT_FAST32_MIN: -2147483648
INT_FAST64_MIN: -9223372036854775808
INT8_MIN: 37777777600
INT16_MIN: 37777700000
INT32_MIN: 20000000000
INT64_MIN: 1000000000000000000000
INT_LEAST8_MIN: 37777777600
INT_LEAST16_MIN: 37777700000
INT_LEAST32_MIN: 20000000000
INT_LEAST64_MIN: 1000000000000000000000
INT_FAST8_MIN: 37777777600
INT_FAST16_MIN: 37777700000
INT_FAST32_MIN: 20000000000
INT_FAST64_MIN: 1000000000000000000000
UINT8_MAX: 255
UINT16_MAX: 65535
UINT32_MAX: 4294967295
UINT64_MAX: 18446744073709551615
UINT_LEAST8_MAX: 255
UINT_LEAST16_MAX: 65535
UINT_LEAST32_MAX: 4294967295
UINT_LEAST64_MAX: 18446744073709551615
UINT_FAST8_MAX: 255
UINT_FAST16_MAX: 65535
UINT_FAST32_MAX: 4294967295
UINT_FAST64_MAX: 18446744073709551615
UINT8_MAX: ff
UINT16_MAX: ffff
UINT32_MAX: ffffffff
UINT64_MAX: ffffffffffffffff
UINT_LEAST8_MAX: ff
UINT_LEAST16_MAX: ffff
UINT_LEAST32_MAX: ffffffff
UINT_LEAST64_MAX: ffffffffffffffff
UINT_FAST8_MAX: ff
UINT_FAST16_MAX: ffff
UINT_FAST32_MAX: ffffffff
UINT_FAST64_MAX: ffffffffffffffff
注意
因为 C++ 转译立即后随字符串字面量的字符为用户定义字面量,诸如 printf("%"PRId64"\n",n); 的 C 代码在 C++ 中非法,并在 PRId64
前要求空格。
C99 标准建议 C++ 实现不应定义上述极限、常量或格式化宏,除非在包含相对的 C 头文件( stdint.h
或 inttypes.h
)前(分别)定义宏 __STDC_LIMIT_MACROS
、 __STDC_CONSTANT_MACROS
或 __STDC_FORMAT_MACROS
。此建议未被采纳,并在 C11 中被移除。然而,某些实现(例如 glibc 2.17 )尝试应用此规则,而可能需要定义 __STDC
宏; C++ 编译器可以通过在某些环境自动定义它们来做变通。