固定寬度整型 (C++11 起)
目錄 |
[編輯] 型別
定義於標頭檔案
<cstdint> | |
int8_tint16_tint32_tint64_t (可選) |
寬度分別為恰好 8、16、32 和 64 位的有符號整型 沒有填充位,對負值使用 2 的補碼 (當且僅當實現直接支援該型別時才提供) (typedef) |
int_fast8_tint_fast16_tint_fast32_tint_fast64_t |
寬度至少為 8、16、32 和 64 位的最快有符號整型 (typedef) |
int_least8_tint_least16_tint_least32_tint_least64_t |
寬度至少為 8、16、32 和 64 位的最小有符號整型 (typedef) |
intmax_t |
最大寬度有符號整型 (typedef) |
intptr_t (可選) |
能夠容納 void 指標的有符號整型 (typedef) |
uint8_tuint16_tuint32_tuint64_t (可選) |
寬度分別為恰好 8、16、32 和 64 位的無符號整型 (當且僅當實現直接支援該型別時才提供) (typedef) |
uint_fast8_tuint_fast16_tuint_fast32_tuint_fast64_t |
寬度至少為 8、16、32 和 64 位的最快無符號整型 (typedef) |
uint_least8_tuint_least16_tuint_least32_tuint_least64_t |
寬度至少為 8、16、32 和 64 位的最小無符號整型 (typedef) |
uintmax_t |
最大寬度無符號整型 (typedef) |
uintptr_t (可選) |
能夠容納 void 指標的無符號整型 (typedef) |
當 N 不是 8、16、32 或 64 時,實現可以定義類型別名 intN_t
、int_fastN_t
、int_leastN_t
、uintN_t
、uint_fastN_t
和 uint_leastN_t
。只有當實現支援無填充位的指定寬度的整型時,才可以定義 intN_t
形式的類型別名。因此,std::uint24_t
表示寬度恰好為 24 位的無符號整型。
下面列出的每個宏都當且僅當實現定義了相應的類型別名時才被定義。宏 INTN_C
和 UINTN_C
分別對應於類型別名 int_leastN_t
和 uint_leastN_t
。
[編輯] 宏常量
定義於標頭檔案
<cstdint> | |
有符號整數:最小值 | |
INT8_MININT16_MININT32_MININT64_MIN (可選) |
std::int8_t 、std::int16_t 、std::int32_t 和 std::int64_t 的最小值(宏常量) |
INT_FAST8_MININT_FAST16_MININT_FAST32_MININT_FAST64_MIN |
std::int_fast8_t 、std::int_fast16_t 、std::int_fast32_t 和 std::int_fast64_t 的最小值(宏常量) |
INT_LEAST8_MININT_LEAST16_MININT_LEAST32_MININT_LEAST64_MIN |
std::int_least8_t 、std::int_least16_t 、std::int_least32_t 和 std::int_least64_t 的最小值(宏常量) |
INTPTR_MIN (可選) |
std::intptr_t 的最小值(宏常量) |
INTMAX_MIN |
std::intmax_t 的最小值(宏常量) |
有符號整數:最大值 | |
INT8_MAXINT16_MAXINT32_MAXINT64_MAX (可選) |
std::int8_t 、std::int16_t 、std::int32_t 和 std::int64_t 的最大值(宏常量) |
INT_FAST8_MAXINT_FAST16_MAXINT_FAST32_MAXINT_FAST64_MAX |
std::int_fast8_t 、std::int_fast16_t 、std::int_fast32_t 和 std::int_fast64_t 的最大值(宏常量) |
INT_LEAST8_MAXINT_LEAST16_MAXINT_LEAST32_MAXINT_LEAST64_MAX |
std::int_least8_t 、std::int_least16_t 、std::int_least32_t 和 std::int_least64_t 的最大值(宏常量) |
INTPTR_MAX (可選) |
std::intptr_t 的最大值(宏常量) |
INTMAX_MAX |
std::intmax_t 的最大值(宏常量) |
無符號整數:最大值 | |
UINT8_MAXUINT16_MAXUINT32_MAXUINT64_MAX (可選) |
std::uint8_t 、std::uint16_t 、std::uint32_t 和 std::uint64_t 的最大值(宏常量) |
UINT_FAST8_MAXUINT_FAST16_MAXUINT_FAST32_MAXUINT_FAST64_MAX |
std::uint_fast8_t 、std::uint_fast16_t 、std::uint_fast32_t 和 std::uint_fast64_t 的最大值(宏常量) |
UINT_LEAST8_MAXUINT_LEAST16_MAXUINT_LEAST32_MAXUINT_LEAST64_MAX |
std::uint_least8_t 、std::uint_least16_t 、std::uint_least32_t 和 std::uint_least64_t 的最大值(宏常量) |
UINTPTR_MAX (可選) |
std::uintptr_t 的最大值(宏常量) |
UINTMAX_MAX |
std::uintmax_t 的最大值(宏常量) |
[編輯] 最小寬度整型常量的函式宏
INT8_CINT16_CINT32_CINT64_C |
展開為一個整型常量表達式,其值為其引數指定的值,並且其型別是 std::int_least8_t 、std::int_least16_t 、std::int_least32_t 和 std::int_least64_t 對應的提升型別(函式宏) |
INTMAX_C |
展開為一個整型常量表達式,其值為其引數指定的值,型別為 std::intmax_t (函式宏) |
UINT8_CUINT16_CUINT32_CUINT64_C |
展開為一個整型常量表達式,其值為其引數指定的值,並且其型別是 std::uint_least8_t 、std::uint_least16_t 、std::uint_least32_t 和 std::uint_least64_t 對應的提升型別(函式宏) |
UINTMAX_C |
展開為一個整型常量表達式,其值為其引數指定的值,型別為 std::uintmax_t (函式宏) |
#include <cstdint> UINT64_C(0x123) // expands to a literal of type uint_least64_t and value 0x123
[編輯] 格式宏常量
定義於標頭檔案
<cinttypes> |
[編輯] std::fprintf 函式族的格式常量
這裡列出的每個 PRI
宏都當且僅當實現定義了相應的類型別名時才被定義。
等效於 對於 int 或 unsigned int |
描述 | 資料型別的宏 | ||||
---|---|---|---|---|---|---|
std::int x_t |
std::int_least x_t |
std::int_fast x_t |
std::intmax_t |
std::intptr_t | ||
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 函式族的格式常量
這裡列出的每個 SCN
宏都當且僅當實現定義了相應的類型別名並且該型別具有合適的 std::fscanf 長度修飾符時才被定義。
等效於 對於 int 或 unsigned int |
描述 | 資料型別的宏 | ||||
---|---|---|---|---|---|---|
std::int x_t |
std::int_least x_t |
std::int_fast x_t |
std::intmax_t |
std::intptr_t | ||
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 |
[編輯] 註記
因為 C++ 將字串字面量後面的字元立即解釋為使用者定義字串字面量,所以像 printf("%"PRId64"\n",n); 這樣的 C 程式碼在 C++ 中是無效的,需要在 PRId64
前加一個空格。
C99 標準建議 C++ 實現不應定義上述限制、常量或格式宏,除非在包含相關的 C 標頭檔案(stdint.h
或 inttypes.h
)之前定義了宏 __STDC_LIMIT_MACROS、__STDC_CONSTANT_MACROS 或 __STDC_FORMAT_MACROS(分別)。此建議未被任何 C++ 標準採納,並在 C11 中被移除。然而,一些實現(如 glibc 2.17)試圖應用此規則,因此可能需要定義 __STDC
宏;C++ 編譯器可能會嘗試透過在某些情況下自動定義它們來規避此問題。
std::int8_t
可以是 signed char,std::uint8_t
可以是 unsigned char,但兩者都不能是 char,無論其有符號性如何(因為 char 不被認為是“有符號整型”或“無符號整型”)。
[編輯] 示例
#include <cinttypes> #include <cstdio> int main() { std::printf("%zu\n", sizeof(std::int64_t)); std::printf("%s\n", PRId64); std::printf("%+" PRId64 "\n", INT64_MIN); std::printf("%+" PRId64 "\n", INT64_MAX); std::int64_t n = 7; std::printf("%+" PRId64 "\n", n); }
可能的輸出
8 lld -9223372036854775808 +9223372036854775807 +7
[編輯] 缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 2820 | C++11 | 可選類型別名和宏的要求與 C 不一致 | 已使其保持一致 |
[編輯] 參考
- C++23 標準 (ISO/IEC 14882:2024)
- 17.4.1 標頭檔案 <cstdint> 提要 [cstdint.syn]
- 31.13.2 標頭檔案 <cinttypes> 提要 [cinttypes.syn]
- C++20 標準 (ISO/IEC 14882:2020)
- 17.4 整型 [cstdint]
- 29.12.2 標頭檔案 <cinttypes> 提要 [cinttypes.syn]
- C++17 標準 (ISO/IEC 14882:2017)
- 21.4 整型 [cstdint]
- 30.11.2 標頭檔案 <cinttypes> 提要 [cinttypes.syn]
- C++14 標準 (ISO/IEC 14882:2014)
- 18.4 整型 [cstdint]
- 27.9.2 C 庫檔案 [c.files]
- C++11 標準 (ISO/IEC 14882:2011)
- 18.4 整型 [cstdint]
- 27.9.2 C 庫檔案 [c.files]
[編輯] 另請參閱
C 文件 關於 固定寬度整型
|