Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/types/integer"

From cppreference.com
< cpp‎ | types
(Removed duplicated (un)signed keywords)
(Fix chapter name and id)
 
(33 intermediate revisions by 15 users not shown)
Line 1: Line 1:
{{title | Fixed width integer types {{mark since c++11}}}}
+
{{title|Fixed width integer types {{mark since c++11}}}}
 
{{cpp/types/navbar}}
 
{{cpp/types/navbar}}
  
 
===Types===
 
===Types===
 +
{{dsc begin}}
 +
{{dsc header|cstdint}}
 +
{{dsc inc|cpp/types/dsc intN_t}}
 +
{{dsc inc|cpp/types/dsc int_fastN_t}}
 +
{{dsc inc|cpp/types/dsc int_leastN_t}}
 +
{{dsc inc|cpp/types/dsc intmax_t}}
 +
{{dsc inc|cpp/types/dsc intptr_t}}
 +
{{dsc inc|cpp/types/dsc uintN_t}}
 +
{{dsc inc|cpp/types/dsc uint_fastN_t}}
 +
{{dsc inc|cpp/types/dsc uint_leastN_t}}
 +
{{dsc inc|cpp/types/dsc uintmax_t}}
 +
{{dsc inc|cpp/types/dsc uintptr_t}}
 +
{{dsc end}}
  
{{dcl list begin}}
+
The implementation may define typedef names {{tt|int''N''_t}}, {{tt|int_fast''N''_t}}, {{tt|int_least''N''_t}}, {{tt|uint''N''_t}}, {{tt|uint_fast''N''_t}}, and {{tt|uint_least''N''_t}} when ''N'' is not 8, 16, 32 or 64. Typedef names of the form {{tt|int''N''_t}} may only be defined if the implementation supports an integer type of that width with no padding. Thus, {{tt|std::uint24_t}} denotes an unsigned integer type with a width of exactly 24 bits.
{{dcl list header | cstdint}}
+
 
{{dcl list item | {{tt|int8_t}}<br>{{tt|int16_t}}<br>{{tt|int32_t}}<br>{{tt|int64_t}} | signed integer type with width of<br> exactly 8, 16, 32 and 64 bits respectively<br>with no padding bits and using 2's complement for negative values<br>(provided only if the implementation directly supports the type) }}
+
Each of the macros listed in below is defined if and only if the implementation defines the corresponding typedef name. The macros {{tt|INT''N''_C}} and {{tt|UINT''N''_C}} correspond to the typedef names {{tt|int_least''N''_t}} and {{tt|uint_least''N''_t}}, respectively.
{{dcl list item | {{tt|int_fast8_t}}<br>{{tt|int_fast16_t}}<br>{{tt|int_fast32_t}}<br>{{tt|int_fast64_t}} | fastest signed integer type with width of<br> at least 8, 16, 32 and 64 bits respectively }}
+
{{dcl list item | {{tt|int_least8_t}}<br>{{tt|int_least16_t}}<br>{{tt|int_least32_t}}<br>{{tt|int_least64_t}} | smallest signed integer type with width of<br> at least 8, 16, 32 and 64 bits respectively }}
+
{{dcl list item | {{tt|intmax_t}} | maximum width integer type }}
+
{{dcl list item | {{tt|intptr_t}} | integer type capable of holding a pointer}}{{dcl list item | {{tt|uint8_t}}<br>{{tt|uint16_t}}<br>{{tt|uint32_t}}<br>{{tt|uint64_t}} | unsigned integer type with width of<br> exactly 8, 16, 32 and 64 bits respectively <br>(provided only if the implementation directly supports the type) }}
+
{{dcl list item | {{tt|uint_fast8_t}}<br>{{tt|uint_fast16_t}}<br>{{tt|uint_fast32_t}}<br>{{tt|uint_fast64_t}} | fastest unsigned integer type with width of<br> at least 8, 16, 32 and 64 bits respectively }}
+
{{dcl list item | {{tt|uint_least8_t}}<br>{{tt|uint_least16_t}}<br>{{tt|uint_least32_t}}<br>{{tt|uint_least64_t}} | smallest unsigned integer type with width of<br> at least 8, 16, 32 and 64 bits respectively }}
+
{{dcl list item | {{tt|uintmax_t}} | maximum width unsigned integer type }}
+
{{dcl list item | {{tt|uintptr_t}} | unsigned integer type capable of holding a pointer}}
+
{{dcl list end}}
+
  
 
===Macro constants===
 
===Macro constants===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list header | cstdint}}
+
{{dsc header|cstdint}}
{{dcl list h2 | Signed integers : minimum value}}
+
{{dsc h2|Signed integers : minimum value}}
{{dcl list macro const | INT8_MIN<br>INT16_MIN<br>INT32_MIN<br>INT64_MIN |  minimum value of an object of type {{c|int8_t}}, {{c|int16_t}}, {{c|int32_t}}, {{c|int64_t}} | nolink=true }}
+
{{dsc inc|cpp/types/dsc INTn_MIN}}
{{dcl list macro const | INT_FAST8_MIN<br>INT_FAST16_MIN<br>INT_FAST32_MIN<br>INT_FAST64_MIN |  minimum value of an object of type {{c|int_fast8_t}}, {{c|int_fast16_t}}, {{c|int_fast32_t}}, {{c|int_fast64_t}} | nolink=true }}
+
{{dsc inc|cpp/types/dsc INT_FASTn_MIN}}
{{dcl list macro const | INT_LEAST8_MIN<br>INT_LEAST16_MIN<br>INT_LEAST32_MIN<br>INT_LEAST64_MIN | minimum value of an object of type {{c|int_least8_t}}, {{c|int_least16_t}}, {{c|int_least32_t}}, {{c|int_least64_t}} | nolink=true }}
+
{{dsc inc|cpp/types/dsc INT_LEASTn_MIN}}
{{dcl list macro const | INTPTR_MIN | minimum value of an object of type {{c|intptr_t}} | nolink=true }}
+
{{dsc inc|cpp/types/dsc INTPTR_MIN}}
{{dcl list macro const | INTMAX_MIN | minimum value of an object of type {{c|intmax_t}} | nolink=true }}
+
{{dsc inc|cpp/types/dsc INTMAX_MIN}}
{{dcl list h2 | Signed integers : maximum value| nolink=true }}
+
{{dsc h2|Signed integers : maximum value|nolink=true}}
 +
{{dsc inc|cpp/types/dsc INTn_MAX}}
 +
{{dsc inc|cpp/types/dsc INT_FASTn_MAX}}
 +
{{dsc inc|cpp/types/dsc INT_LEAST8_MAX}}
 +
{{dsc inc|cpp/types/dsc INTPTR_MAX}}
 +
{{dsc inc|cpp/types/dsc INTMAX_MAX}}
 +
{{dsc h2|Unsigned integers : maximum value|nolink=true}}
 +
{{dsc inc|cpp/types/dsc UINTn_MAX}}
 +
{{dsc inc|cpp/types/dsc UINT_FASTn_MAX}}
 +
{{dsc inc|cpp/types/dsc UINT_LEASTn_MAX}}
 +
{{dsc inc|cpp/types/dsc UINTPTR_MAX}}
 +
{{dsc inc|cpp/types/dsc UINTMAX_MAX}}
 +
{{dsc end}}
  
{{dcl list macro const | INT8_MAX<br>INT16_MAX<br>INT32_MAX<br>INT64_MAX |  maximum value of an object of type {{c|int8_t}}, {{c|int16_t}}, {{c|int32_t}}, {{c|int64_t}} | nolink=true }}
+
===Function macros for minimum-width integer constants===
{{dcl list macro const | INT_FAST8_MAX<br>INT_FAST16_MAX<br>INT_FAST32_MAX<br>INT_FAST64_MAX |  maximum value of an object of type {{c|int_fast8_t}}, {{c|int_fast16_t}}, {{c|int_fast32_t}}, {{c|int_fast64_t}} | nolink=true }}
+
{{dsc begin}}
{{dcl list macro const | INT_LEAST8_MAX<br>INT_LEAST16_MAX<br>INT_LEAST32_MAX<br>INT_LEAST64_MAX |  maximum value of an object of type {{c|int_least8_t}}, {{c|int_least16_t}}, {{c|int_least32_t}}, {{c|int_least64_t}} | nolink=true }}
+
{{dsc inc|cpp/types/dsc INTn_C}}
{{dcl list macro const | INTPTR_MAX | maximum value of an object of type {{c|intptr_t}} | nolink=true }}
+
{{dsc inc|cpp/types/dsc INTMAX_C}}
{{dcl list macro const | INTMAX_MAX | maximum value of an object of type {{c|intmax_t}} | nolink=true }}
+
{{dsc inc|cpp/types/dsc UINTn_C}}
{{dcl list h2 | Unsigned integers : maximum value| nolink=true }}{{dcl list macro const | UINT8_MAX<br>UINT16_MAX<br>UINT32_MAX<br>UINT64_MAX |  maximum value of an object of type {{c|uint8_t}}, {{c|uint16_t}}, {{c|uint32_t}}, {{c|uint64_t}} | nolink=true }}
+
{{dsc inc|cpp/types/dsc UINTMAX_C}}
{{dcl list macro const | UINT_FAST8_MAX<br>UINT_FAST16_MAX<br>UINT_FAST32_MAX<br>UINT_FAST64_MAX |  maximum value of an object of type {{c|uint_fast8_t}}, {{c|uint_fast16_t}}, {{c|uint_fast32_t}}, {{c|uint_fast64_t}} | nolink=true }}
+
{{dsc end}}
{{dcl list macro const | UINT_LEAST8_MAX<br>UINT_LEAST16_MAX<br>UINT_LEAST32_MAX<br>UINT_LEAST64_MAX |  maximum value of an object of type {{c|uint_least8_t}}, {{c|uint_least16_t}}, {{c|uint_least32_t}}, {{c|uint_least64_t}} | nolink=true }}
+
 
{{dcl list macro const | UINTPTR_MAX | maximum value of an object of type {{c|uintptr_t}} | nolink=true }}
+
{{source|
{{dcl list macro const | UINTMAX_MAX | maximum value of an object of type {{c|uintmax_t}} | nolink=true }}
+
#include <cstdint>
{{dcl list end}}
+
UINT64_C(0x123) // expands to a literal of type uint_least64_t and value 0x123
 +
}}
  
 
===Format macro constants===
 
===Format macro constants===
{{todo|will look better as a table}}
+
{{dsc begin}}
{{dcl list begin}}
+
{{dsc header|cinttypes}}
{{dcl list header | cinttypes}}
+
{{dsc end}}
{{dcl list h2 | Format constants for the {{c|std::fprintf}} family of functions}}
+
 
{{dcl list macro const | PRId8<br>PRId16<br>PRId32<br>PRId64<br>PRIdLEAST8<br>PRIdLEAST16<br>PRIdLEAST32<br>PRIdLEAST64<br>PRIdFAST8<br>PRIdFAST16<br>PRIdFAST32<br>PRIdFAST64<br>PRIdMAX<br>PRIdPTR | format conversion specifier to output a signed decimal integer value of type {{c|std::int8_t}}, {{c|std::int16_t}}, {{c|std::int32_t}}, {{c|std::int64_t}}, {{c|std::int_least8_t}}, {{c|std::int_least16_t}}, {{c|std::int_least32_t}}, {{c|std::int_least64_t}}, {{c|std::int_fast8_t}}, {{c|std::int_fast16_t}}, {{c|std::int_fast32_t}}, {{c|std::int_fast64_t}}, {{c|std:intmax_t}}, {{c|std::intptr_t}} respectively, equivalent to {{tt|%d}} for {{c|int}} | nolink=true }}
+
====Format constants for the {{lc|std::fprintf}} family of functions====
{{dcl list macro const | PRIi8<br>PRIi16<br>PRIi32<br>PRIi64<br>PRIiLEAST8<br>PRIiLEAST16<br>PRIiLEAST32<br>PRIiLEAST64<br>PRIiFAST8<br>PRIiFAST16<br>PRIiFAST32<br>PRIiFAST64<br>PRIiMAX<br>PRIiPTR | format conversion specifier to output a signed decimal integer value of type {{c|std::int8_t}}, {{c|std::int16_t}}, {{c|std::int32_t}}, {{c|std::int64_t}}, {{c|std::int_least8_t}}, {{c|std::int_least16_t}}, {{c|std::int_least32_t}}, {{c|std::int_least64_t}}, {{c|std::int_fast8_t}}, {{c|std::int_fast16_t}}, {{c|std::int_fast32_t}}, {{c|std::int_fast64_t}}, {{c|std:intmax_t}}, {{c|std::intptr_t}} respectively, equivalent to {{tt|%i}} for {{c|int}} | nolink=true }}
+
Each of the {{tt|PRI}} macros listed here is defined if and only if the implementation defines the corresponding typedef name.
{{dcl list macro const | PRIu8<br>PRIu16<br>PRIu32<br>PRIu64<br>PRIuLEAST8<br>PRIuLEAST16<br>PRIuLEAST32<br>PRIuLEAST64<br>PRIuFAST8<br>PRIuFAST16<br>PRIuFAST32<br>PRIuFAST64<br>PRIuMAX<br>PRIuPTR | format conversion specifier to output an unsigned decimal integer value of type {{c|std::uint8_t}}, {{c|std::uint16_t}}, {{c|std::uint32_t}}, {{c|std::uint64_t}}, {{c|std::uint_least8_t}}, {{c|std::uint_least16_t}}, {{c|std::uint_least32_t}}, {{c|std::uint_least64_t}}, {{c|std::uint_fast8_t}}, {{c|std::uint_fast16_t}}, {{c|std::uint_fast32_t}}, {{c|std::uint_fast64_t}}, {{c|std:uintmax_t}}, {{c|std::uintptr_t}} respectively, equivalent to {{tt|%u}} for {{c|unsigned int}} | nolink=true }}
+
 
{{dcl list macro const | PRIo8<br>PRIo16<br>PRIo32<br>PRIo64<br>PRIoLEAST8<br>PRIoLEAST16<br>PRIoLEAST32<br>PRIoLEAST64<br>PRIoFAST8<br>PRIoFAST16<br>PRIoFAST32<br>PRIoFAST64<br>PRIoMAX<br>PRIoPTR | format conversion specifier to output an unsigned octal integer value of type {{c|std::uint8_t}}, {{c|std::uint16_t}}, {{c|std::uint32_t}}, {{c|std::uint64_t}}, {{c|std::uint_least8_t}}, {{c|std::uint_least16_t}}, {{c|std::uint_least32_t}}, {{c|std::uint_least64_t}}, {{c|std::uint_fast8_t}}, {{c|std::uint_fast16_t}}, {{c|std::uint_fast32_t}}, {{c|std::uint_fast64_t}}, {{c|std:uintmax_t}}, {{c|std::uintptr_t}} respectively, equivalent to {{tt|%o}} for {{c|unsigned int}} | nolink=true }}
+
{| class="wikitable" style="text-align:center; font-size:85%; max-width:90em;"
{{dcl list macro const | PRIx8<br>PRIx16<br>PRIx32<br>PRIx64<br>PRIxLEAST8<br>PRIxLEAST16<br>PRIxLEAST32<br>PRIxLEAST64<br>PRIxFAST8<br>PRIxFAST16<br>PRIxFAST32<br>PRIxFAST64<br>PRIxMAX<br>PRIxPTR | format conversion specifier to output an unsigned hexadecimal integer value of type {{c|std::uint8_t}}, {{c|std::uint16_t}}, {{c|std::uint32_t}}, {{c|std::uint64_t}}, {{c|std::uint_least8_t}}, {{c|std::uint_least16_t}}, {{c|std::uint_least32_t}}, {{c|std::uint_least64_t}}, {{c|std::uint_fast8_t}}, {{c|std::uint_fast16_t}}, {{c|std::uint_fast32_t}}, {{c|std::uint_fast64_t}}, {{c|std:uintmax_t}}, {{c|std::uintptr_t}} respectively, equivalent to {{tt|%x}} for {{c|unsigned int}} | nolink=true }}
+
|-
{{dcl list macro const | PRIX8<br>PRIX16<br>PRIX32<br>PRIX64<br>PRIXLEAST8<br>PRIXLEAST16<br>PRIXLEAST32<br>PRIXLEAST64<br>PRIXFAST8<br>PRIXFAST16<br>PRIXFAST32<br>PRIXFAST64<br>PRIXMAX<br>PRIXPTR | format conversion specifier to output an unsigned uppercase hexadecimal integer value of type {{c|std::uint8_t}}, {{c|std::uint16_t}}, {{c|std::uint32_t}}, {{c|std::uint64_t}}, {{c|std::uint_least8_t}}, {{c|std::uint_least16_t}}, {{c|std::uint_least32_t}}, {{c|std::uint_least64_t}}, {{c|std::uint_fast8_t}}, {{c|std::uint_fast16_t}}, {{c|std::uint_fast32_t}}, {{c|std::uint_fast64_t}}, {{c|std:uintmax_t}}, {{c|std::uintptr_t}} respectively, equivalent to {{tt|%X}} for {{c|unsigned int}} | nolink=true }}
+
! rowspan=2|Equivalent<br>for {{c/core|int}} or<br>{{c/core|unsigned int}}
{{dcl list h2 | Format constants for the {{c|std::fscanf}} family of functions}}
+
! rowspan=2|Description
{{dcl list macro const | SCNd8<br>SCNd16<br>SCNd32<br>SCNd64<br>SCNdLEAST8<br>SCNdLEAST16<br>SCNdLEAST32<br>SCNdLEAST64<br>SCNdFAST8<br>SCNdFAST16<br>SCNdFAST32<br>SCNdFAST64<br>SCNdMAX<br>SCNdPTR | format conversion specifier to input a signed decimal integer value of type {{c|std::int8_t}}, {{c|std::int16_t}}, {{c|std::int32_t}}, {{c|std::int64_t}}, {{c|std::int_least8_t}}, {{c|std::int_least16_t}}, {{c|std::int_least32_t}}, {{c|std::int_least64_t}}, {{c|std::int_fast8_t}}, {{c|std::int_fast16_t}}, {{c|std::int_fast32_t}}, {{c|std::int_fast64_t}}, {{c|std:intmax_t}}, {{c|std::intptr_t}} respectively, equivalent to {{tt|%d}} for {{c|int}} | nolink=true }}
+
! colspan=5|Macros for data types
{{dcl list macro const | SCNi8<br>SCNi16<br>SCNi32<br>SCNi64<br>SCNiLEAST8<br>SCNiLEAST16<br>SCNiLEAST32<br>SCNiLEAST64<br>SCNiFAST8<br>SCNiFAST16<br>SCNiFAST32<br>SCNiFAST64<br>SCNiMAX<br>SCNiPTR | format conversion specifier to input a signed decimal/octal/hexadecimal integer value of type {{c|std::int8_t}}, {{c|std::int16_t}}, {{c|std::int32_t}}, {{c|std::int64_t}}, {{c|std::int_least8_t}}, {{c|std::int_least16_t}}, {{c|std::int_least32_t}}, {{c|std::int_least64_t}}, {{c|std::int_fast8_t}}, {{c|std::int_fast16_t}}, {{c|std::int_fast32_t}}, {{c|std::int_fast64_t}}, {{c|std:intmax_t}}, {{c|std::intptr_t}} respectively, equivalent to {{tt|%i}} for {{c|int}} | nolink=true }}
+
|-
{{dcl list macro const | SCNu8<br>SCNu16<br>SCNu32<br>SCNu64<br>SCNuLEAST8<br>SCNuLEAST16<br>SCNuLEAST32<br>SCNuLEAST64<br>SCNuFAST8<br>SCNuFAST16<br>SCNuFAST32<br>SCNuFAST64<br>SCNuMAX<br>SCNuPTR | format conversion specifier to input an unsigned decimal integer value of type {{c|std::uint8_t}}, {{c|std::uint16_t}}, {{c|std::uint32_t}}, {{c|std::uint64_t}}, {{c|std::uint_least8_t}}, {{c|std::uint_least16_t}}, {{c|std::uint_least32_t}}, {{c|std::uint_least64_t}}, {{c|std::uint_fast8_t}}, {{c|std::uint_fast16_t}}, {{c|std::uint_fast32_t}}, {{c|std::uint_fast64_t}}, {{c|std:uintmax_t}}, {{c|std::uintptr_t}} respectively, equivalent to {{tt|%u}} for {{c|unsigned int}} | nolink=true }}
+
|<br><br><br><br>{{vertical|{{tt|std::int}}'''x'''{{tt|_t}}}}<br><br><br><br>
{{dcl list macro const | SCNo8<br>SCNo16<br>SCNo32<br>SCNo64<br>SCNoLEAST8<br>SCNoLEAST16<br>SCNoLEAST32<br>SCNoLEAST64<br>SCNoFAST8<br>SCNoFAST16<br>SCNoFAST32<br>SCNoFAST64<br>SCNoMAX<br>SCNoPTR | format conversion specifier to input an unsigned octal integer value of type {{c|std::uint8_t}}, {{c|std::uint16_t}}, {{c|std::uint32_t}}, {{c|std::uint64_t}}, {{c|std::uint_least8_t}}, {{c|std::uint_least16_t}}, {{c|std::uint_least32_t}}, {{c|std::uint_least64_t}}, {{c|std::uint_fast8_t}}, {{c|std::uint_fast16_t}}, {{c|std::uint_fast32_t}}, {{c|std::uint_fast64_t}}, {{c|std:uintmax_t}}, {{c|std::uintptr_t}} respectively, equivalent to {{tt|%o}} for {{c|unsigned int}} | nolink=true }}
+
|{{vertical|{{tt|std::int_least}}'''x'''{{lc|_t}}}}
{{dcl list macro const | SCNx8<br>SCNx16<br>SCNx32<br>SCNx64<br>SCNxLEAST8<br>SCNxLEAST16<br>SCNxLEAST32<br>SCNxLEAST64<br>SCNxFAST8<br>SCNxFAST16<br>SCNxFAST32<br>SCNxFAST64<br>SCNxMAX<br>SCNxPTR | format conversion specifier to input an unsigned hexadecimal integer value of type {{c|std::uint8_t}}, {{c|std::uint16_t}}, {{c|std::uint32_t}}, {{c|std::uint64_t}}, {{c|std::uint_least8_t}}, {{c|std::uint_least16_t}}, {{c|std::uint_least32_t}}, {{c|std::uint_least64_t}}, {{c|std::uint_fast8_t}}, {{c|std::uint_fast16_t}}, {{c|std::uint_fast32_t}}, {{c|std::uint_fast64_t}}, {{c|std:uintmax_t}}, {{c|std::uintptr_t}} respectively, equivalent to {{tt|%x}} for {{c|unsigned int}} | nolink=true }}
+
|{{vertical|{{tt|std::int_fast}}'''x'''{{lc|_t}}}}
{{dcl list macro const | SCNX8<br>SCNX16<br>SCNX32<br>SCNX64<br>SCNXLEAST8<br>SCNXLEAST16<br>SCNXLEAST32<br>SCNXLEAST64<br>SCNXFAST8<br>SCNXFAST16<br>SCNXFAST32<br>SCNXFAST64<br>SCNXMAX<br>SCNXPTR | format conversion specifier to input an unsigned uppercase hexadecimal integer value of type {{c|std::uint8_t}}, {{c|std::uint16_t}}, {{c|std::uint32_t}}, {{c|std::uint64_t}}, {{c|std::uint_least8_t}}, {{c|std::uint_least16_t}}, {{c|std::uint_least32_t}}, {{c|std::uint_least64_t}}, {{c|std::uint_fast8_t}}, {{c|std::uint_fast16_t}}, {{c|std::uint_fast32_t}}, {{c|std::uint_fast64_t}}, {{c|std:uintmax_t}}, {{c|std::uintptr_t}} respectively, equivalent to {{tt|%X}} for {{c|unsigned int}} | nolink=true }}
+
|{{vertical|{{tt|std::intmax_t}}}}
{{dcl list end}}
+
|{{vertical|{{tt|std::intptr_t}}}}
 +
|-
 +
! {{tt|d}}
 +
|rowspan=2 style="text-align:left;"|output of a signed decimal integer value
 +
|PRId'''x'''
 +
|PRIdLEAST'''x'''
 +
|PRIdFAST'''x'''
 +
|PRIdMAX
 +
|PRIdPTR
 +
|-
 +
! {{tt|i}}
 +
|PRIi'''x'''
 +
|PRIiLEAST'''x'''
 +
|PRIiFAST'''x'''
 +
|PRIiMAX
 +
|PRIiPTR
 +
|-
 +
! {{tt|u}}
 +
|style="text-align:left;"|output of an unsigned decimal integer value
 +
|PRIu'''x'''
 +
|PRIuLEAST'''x'''
 +
|PRIuFAST'''x'''
 +
|PRIuMAX
 +
|PRIuPTR
 +
|-
 +
! {{tt|o}}
 +
|style="text-align:left;"|output of an unsigned octal integer value
 +
|PRIo'''x'''
 +
|PRIoLEAST'''x'''
 +
|PRIoFAST'''x'''
 +
|PRIoMAX
 +
|PRIoPTR
 +
|-
 +
! {{tt|x}}
 +
|style="text-align:left;"|output of an unsigned lowercase hexadecimal integer value
 +
|PRIx'''x'''
 +
|PRIxLEAST'''x'''
 +
|PRIxFAST'''x'''
 +
|PRIxMAX
 +
|PRIxPTR
 +
|-
 +
! {{tt|X}}
 +
|style="text-align:left;"|output of an unsigned uppercase hexadecimal integer value
 +
|PRIX'''x'''
 +
|PRIXLEAST'''x'''
 +
|PRIXFAST'''x'''
 +
|PRIXMAX
 +
|PRIXPTR
 +
|}
 +
 
 +
====Format constants for the {{lc|std::fscanf}} family of functions====
 +
Each of the {{tt|SCN}} macros listed in here is defined if and only if the implementation defines the corresponding typedef name and has a suitable {{lc|std::fscanf}} length modifier for the type.
 +
 
 +
{| class="wikitable" style="text-align:center; font-size:85%; max-width:90em;"
 +
|-
 +
! rowspan=2|Equivalent<br>for {{c/core|int}} or<br>{{c/core|unsigned int}}
 +
! rowspan=2|Description
 +
! colspan=5|Macros for data types
 +
|-
 +
|<br><br><br><br>{{vertical|{{tt|std::int}}'''x'''{{tt|_t}}}}<br><br><br><br>
 +
|{{vertical|{{tt|std::int_least}}'''x'''{{lc|_t}}}}
 +
|{{vertical|{{tt|std::int_fast}}'''x'''{{lc|_t}}}}
 +
|{{vertical|{{tt|std::intmax_t}}}}
 +
|{{vertical|{{tt|std::intptr_t}}}}
 +
|-
 +
! {{tt|d}}
 +
|style="text-align:left;"|input of a signed decimal integer value
 +
|SCNd'''x'''
 +
|SCNdLEAST'''x'''
 +
|SCNdFAST'''x'''
 +
|SCNdMAX
 +
|SCNdPTR
 +
|-
 +
! {{tt|i}}
 +
|style="text-align:left;"|input of a signed integer value
 +
|SCNi'''x'''
 +
|SCNiLEAST'''x'''
 +
|SCNiFAST'''x'''
 +
|SCNiMAX
 +
|SCNiPTR
 +
|-
 +
! {{tt|u}}
 +
|style="text-align:left;"|input of an unsigned decimal integer value
 +
|SCNu'''x'''
 +
|SCNuLEAST'''x'''
 +
|SCNuFAST'''x'''
 +
|SCNuMAX
 +
|SCNuPTR
 +
|-
 +
! {{tt|o}}
 +
|style="text-align:left;"|input of an unsigned octal integer value
 +
|SCNo'''x'''
 +
|SCNoLEAST'''x'''
 +
|SCNoFAST'''x'''
 +
|SCNoMAX
 +
|SCNoPTR
 +
|-
 +
! {{tt|x}}
 +
|style="text-align:left;"|input of an unsigned hexadecimal integer value
 +
|SCNx'''x'''
 +
|SCNxLEAST'''x'''
 +
|SCNxFAST'''x'''
 +
|SCNxMAX
 +
|SCNxPTR
 +
|}
 +
<!-- note: SCNX* macros listed in [c.files] of C++11 and C++14 do not exist; they were an editorial error corrected in a post-C++14 standard draft -->
 +
 
 +
===Notes===
 +
Because C++ interprets a character immediately following a string literal as a [[cpp/language/user literal|user-defined string literal]], C code such as {{c|printf("%"PRId64"\n",n);}} is invalid C++ and requires a space before {{tt|PRId64}}.
 +
 
 +
The C99 standard suggests that C++ implementations should not define the above limit, constant, or format macros unless the macros {{c|__STDC_LIMIT_MACROS}}, {{c|__STDC_CONSTANT_MACROS}} or {{c|__STDC_FORMAT_MACROS}} (respectively) are defined before including the relevant C header ({{tt|stdint.h}} or {{tt|inttypes.h}}). This recommendation was not adopted by any C++ standard and was removed in C11. However, some implementations (such as glibc 2.17) try to apply this rule, and it may be necessary to define the {{tt|__STDC}} macros; C++ compilers may try to work around this by automatically defining them in some circumstances.
 +
 
 +
{{tt|std::int8_t}} may be {{c/core|signed char}} and {{tt|std::uint8_t}} may be {{c/core|unsigned char}}, but neither can be {{c/core|char}} regardless of its signedness (because {{c/core|char}} is not considered a "signed integer type" or "unsigned integer type").
 +
 
 +
===Example===
 +
{{example
 +
|See also a [[cpp/language/user_literal#Notes|note]] regarding spaces before [[#Format macro constants|format macros]] used in this example.
 +
|code=
 +
#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);
 +
}
 +
|p=true
 +
|output=
 +
8
 +
lld
 +
-9223372036854775808
 +
+9223372036854775807
 +
+7
 +
}}
 +
 
 +
===Defect reports===
 +
{{dr list begin}}
 +
{{dr list item|wg=lwg|dr=2820|std=C++11|before=the requirements for optional typedef names and macros were inconsistent with C|after=made consistent}}
 +
{{dr list end}}
 +
 
 +
===References===
 +
{{ref std c++23}}
 +
{{ref std|section=17.4.1|title=Header <cstdint> synopsis|id=cstdint.syn}}
 +
{{ref std|section=31.13.2|title=Header <cinttypes> synopsis|id=cinttypes.syn}}
 +
{{ref std end}}
 +
{{ref std c++20}}
 +
{{ref std|section=17.4|title=Integer types|id=cstdint}}
 +
{{ref std|section=29.12.2|title=Header <cinttypes> synopsis|id=cinttypes.syn}}
 +
{{ref std end}}
 +
{{ref std c++17}}
 +
{{ref std|section=21.4|title=Integer types|id=cstdint}}
 +
{{ref std|section=30.11.2|title=Header <cinttypes> synopsis|id=cinttypes.syn}}
 +
{{ref std end}}
 +
{{ref std c++14}}
 +
{{ref std|section=18.4|title=Integer types|id=cstdint}}
 +
{{ref std|section=27.9.2|title=C library files|id=c.files}}
 +
{{ref std end}}
 +
{{ref std c++11}}
 +
{{ref std|section=18.4|title=Integer types|id=cstdint}}
 +
{{ref std|section=27.9.2|title=C library files|id=c.files}}
 +
{{ref std end}}
 +
 
 +
===See also===
 +
* [[cpp/language/types|Fundamental types]]
 +
{{dsc begin}}
 +
{{dsc see c|c/types/integer|Fixed width integer types|nomono=true}}
 +
{{dsc end}}
  
[[de:cpp/types/integer]]
+
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
[[es:cpp/types/integer]]
+
[[fr:cpp/types/integer]]
+
[[it:cpp/types/integer]]
+
[[ja:cpp/types/integer]]
+
[[pt:cpp/types/integer]]
+
[[ru:cpp/types/integer]]
+
[[zh:cpp/types/integer]]
+

Latest revision as of 09:18, 8 February 2024

 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
Type support
Basic types
Fixed width integer types (C++11)
Fixed width floating-point types (C++23)
(C++11)    
(C++17)
Numeric limits
C numeric limits interface
Runtime type information
 

Contents

[edit] Types

Defined in header <cstdint>
int8_tint16_tint32_tint64_t
(optional)
signed integer type with width of exactly 8, 16, 32 and 64 bits respectively
with no padding bits and using 2's complement for negative values
(provided if and only if the implementation directly supports the type)
(typedef) [edit]
int_fast8_tint_fast16_tint_fast32_tint_fast64_t
fastest signed integer type with width of at least 8, 16, 32 and 64 bits respectively
(typedef) [edit]
int_least8_tint_least16_tint_least32_tint_least64_t
smallest signed integer type with width of at least 8, 16, 32 and 64 bits respectively
(typedef) [edit]
intmax_t
maximum-width signed integer type
(typedef) [edit]
intptr_t
(optional)
signed integer type capable of holding a pointer to void
(typedef) [edit]
uint8_tuint16_tuint32_tuint64_t
(optional)
unsigned integer type with width of exactly 8, 16, 32 and 64 bits respectively
(provided if and only if the implementation directly supports the type)
(typedef) [edit]
uint_fast8_tuint_fast16_tuint_fast32_tuint_fast64_t
fastest unsigned integer type with width of at least 8, 16, 32 and 64 bits respectively
(typedef) [edit]
uint_least8_tuint_least16_tuint_least32_tuint_least64_t
smallest unsigned integer type with width of at least 8, 16, 32 and 64 bits respectively
(typedef) [edit]
uintmax_t
maximum-width unsigned integer type
(typedef) [edit]
uintptr_t
(optional)
unsigned integer type capable of holding a pointer to void
(typedef) [edit]

The implementation may define typedef names intN_t, int_fastN_t, int_leastN_t, uintN_t, uint_fastN_t, and uint_leastN_t when N is not 8, 16, 32 or 64. Typedef names of the form intN_t may only be defined if the implementation supports an integer type of that width with no padding. Thus, std::uint24_t denotes an unsigned integer type with a width of exactly 24 bits.

Each of the macros listed in below is defined if and only if the implementation defines the corresponding typedef name. The macros INTN_C and UINTN_C correspond to the typedef names int_leastN_t and uint_leastN_t, respectively.

[edit] Macro constants

Defined in header <cstdint>
Signed integers : minimum value
INT8_MININT16_MININT32_MININT64_MIN
(optional)
minimum value of std::int8_t, std::int16_t, std::int32_t and std::int64_t respectively
(macro constant) [edit]
INT_FAST8_MININT_FAST16_MININT_FAST32_MININT_FAST64_MIN
minimum value of std::int_fast8_t, std::int_fast16_t, std::int_fast32_t and std::int_fast64_t respectively
(macro constant) [edit]
INT_LEAST8_MININT_LEAST16_MININT_LEAST32_MININT_LEAST64_MIN
minimum value of std::int_least8_t, std::int_least16_t, std::int_least32_t and std::int_least64_t respectively
(macro constant) [edit]
INTPTR_MIN
(optional)
minimum value of std::intptr_t
(macro constant) [edit]
INTMAX_MIN
minimum value of std::intmax_t
(macro constant) [edit]
Signed integers : maximum value
INT8_MAXINT16_MAXINT32_MAXINT64_MAX
(optional)
maximum value of std::int8_t, std::int16_t, std::int32_t and std::int64_t respectively
(macro constant) [edit]
INT_FAST8_MAXINT_FAST16_MAXINT_FAST32_MAXINT_FAST64_MAX
maximum value of std::int_fast8_t, std::int_fast16_t, std::int_fast32_t and std::int_fast64_t respectively
(macro constant) [edit]
INT_LEAST8_MAXINT_LEAST16_MAXINT_LEAST32_MAXINT_LEAST64_MAX
maximum value of std::int_least8_t, std::int_least16_t, std::int_least32_t and std::int_least64_t respectively
(macro constant) [edit]
INTPTR_MAX
(optional)
maximum value of std::intptr_t
(macro constant) [edit]
INTMAX_MAX
maximum value of std::intmax_t
(macro constant) [edit]
Unsigned integers : maximum value
UINT8_MAXUINT16_MAXUINT32_MAXUINT64_MAX
(optional)
maximum value of std::uint8_t, std::uint16_t, std::uint32_t and std::uint64_t respectively
(macro constant) [edit]
UINT_FAST8_MAXUINT_FAST16_MAXUINT_FAST32_MAXUINT_FAST64_MAX
maximum value of std::uint_fast8_t, std::uint_fast16_t, std::uint_fast32_t and std::uint_fast64_t respectively
(macro constant) [edit]
UINT_LEAST8_MAXUINT_LEAST16_MAXUINT_LEAST32_MAXUINT_LEAST64_MAX
maximum value of std::uint_least8_t, std::uint_least16_t, std::uint_least32_t and std::uint_least64_t respectively
(macro constant) [edit]
UINTPTR_MAX
(optional)
maximum value of std::uintptr_t
(macro constant) [edit]
UINTMAX_MAX
maximum value of std::uintmax_t
(macro constant) [edit]

[edit] Function macros for minimum-width integer constants

INT8_CINT16_CINT32_CINT64_C
expands to an integer constant expression having the value specified by its argument and whose type is the promoted type of std::int_least8_t, std::int_least16_t, std::int_least32_t and std::int_least64_t respectively
(function macro) [edit]
INTMAX_C
expands to an integer constant expression having the value specified by its argument and the type std::intmax_t
(function macro) [edit]
UINT8_CUINT16_CUINT32_CUINT64_C
expands to an integer constant expression having the value specified by its argument and whose type is the promoted type of std::uint_least8_t, std::uint_least16_t, std::uint_least32_t and std::uint_least64_t respectively
(function macro) [edit]
UINTMAX_C
expands to an integer constant expression having the value specified by its argument and the type std::uintmax_t
(function macro) [edit]
#include <cstdint>
UINT64_C(0x123) // expands to a literal of type uint_least64_t and value 0x123

[edit] Format macro constants

Defined in header <cinttypes>

[edit] Format constants for the std::fprintf family of functions

Each of the PRI macros listed here is defined if and only if the implementation defines the corresponding typedef name.

Equivalent
for int or
unsigned int
Description Macros for data types




std::intx_t




std::int_leastx_t
std::int_fastx_t
std::intmax_t
std::intptr_t
d output of a signed decimal integer value PRIdx PRIdLEASTx PRIdFASTx PRIdMAX PRIdPTR
i PRIix PRIiLEASTx PRIiFASTx PRIiMAX PRIiPTR
u output of an unsigned decimal integer value PRIux PRIuLEASTx PRIuFASTx PRIuMAX PRIuPTR
o output of an unsigned octal integer value PRIox PRIoLEASTx PRIoFASTx PRIoMAX PRIoPTR
x output of an unsigned lowercase hexadecimal integer value PRIxx PRIxLEASTx PRIxFASTx PRIxMAX PRIxPTR
X output of an unsigned uppercase hexadecimal integer value PRIXx PRIXLEASTx PRIXFASTx PRIXMAX PRIXPTR

[edit] Format constants for the std::fscanf family of functions

Each of the SCN macros listed in here is defined if and only if the implementation defines the corresponding typedef name and has a suitable std::fscanf length modifier for the type.

Equivalent
for int or
unsigned int
Description Macros for data types




std::intx_t




std::int_leastx_t
std::int_fastx_t
std::intmax_t
std::intptr_t
d input of a signed decimal integer value SCNdx SCNdLEASTx SCNdFASTx SCNdMAX SCNdPTR
i input of a signed integer value SCNix SCNiLEASTx SCNiFASTx SCNiMAX SCNiPTR
u input of an unsigned decimal integer value SCNux SCNuLEASTx SCNuFASTx SCNuMAX SCNuPTR
o input of an unsigned octal integer value SCNox SCNoLEASTx SCNoFASTx SCNoMAX SCNoPTR
x input of an unsigned hexadecimal integer value SCNxx SCNxLEASTx SCNxFASTx SCNxMAX SCNxPTR

[edit] Notes

Because C++ interprets a character immediately following a string literal as a user-defined string literal, C code such as printf("%"PRId64"\n",n); is invalid C++ and requires a space before PRId64.

The C99 standard suggests that C++ implementations should not define the above limit, constant, or format macros unless the macros __STDC_LIMIT_MACROS, __STDC_CONSTANT_MACROS or __STDC_FORMAT_MACROS (respectively) are defined before including the relevant C header (stdint.h or inttypes.h). This recommendation was not adopted by any C++ standard and was removed in C11. However, some implementations (such as glibc 2.17) try to apply this rule, and it may be necessary to define the __STDC macros; C++ compilers may try to work around this by automatically defining them in some circumstances.

std::int8_t may be signed char and std::uint8_t may be unsigned char, but neither can be char regardless of its signedness (because char is not considered a "signed integer type" or "unsigned integer type").

[edit] Example

See also a note regarding spaces before format macros used in this example.

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

Possible output:

8
lld
-9223372036854775808
+9223372036854775807
+7

[edit] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
LWG 2820 C++11 the requirements for optional typedef names and macros were inconsistent with C made consistent

[edit] References

  • C++23 standard (ISO/IEC 14882:2024):
  • 17.4.1 Header <cstdint> synopsis [cstdint.syn]
  • 31.13.2 Header <cinttypes> synopsis [cinttypes.syn]
  • C++20 standard (ISO/IEC 14882:2020):
  • 17.4 Integer types [cstdint]
  • 29.12.2 Header <cinttypes> synopsis [cinttypes.syn]
  • C++17 standard (ISO/IEC 14882:2017):
  • 21.4 Integer types [cstdint]
  • 30.11.2 Header <cinttypes> synopsis [cinttypes.syn]
  • C++14 standard (ISO/IEC 14882:2014):
  • 18.4 Integer types [cstdint]
  • 27.9.2 C library files [c.files]
  • C++11 standard (ISO/IEC 14882:2011):
  • 18.4 Integer types [cstdint]
  • 27.9.2 C library files [c.files]

[edit] See also

C documentation for Fixed width integer types