The C++ standard library
is a header introduced in C++11 that defines a set of fixed-width integer types, which have the same size and representation range across different platforms.
Why Use
In C++, the standard integer types (such as int, long ) size and representation range depend on the compiler and platform. This can lead to inconsistent behavior of programs compiled on different platforms. Using 's fixed-width integer types defined in can avoid these problems, because they have the same size and representation range on all platforms.
Definition and syntax
defines the following integer types:
int8_t: 8-bit signed integeruint8_t: 8-bit unsigned integerint16_t: 16-bit signed integeruint16_t: 16-bit unsigned integerint32_t: 32-bit signed integeruint32_t: 32-bit unsigned integerint64_t: 64-bit signed integeruint64_t: 64-bit unsigned integer
In addition, it defines the maximum-width integer types:
intmax_t: Maximum-width signed integeruintmax_t: Maximum-width unsigned integer
as well as types for bit operations:
intptr_t: A signed integer large enough to store a pointer valueuintptr_t: An unsigned integer large enough to store a pointer value
Example
Below is an example using is a simple example showing how to declare and use these types.
Example
#include
#include
int
main
(
)
{
// Declare fixed-width integer types
int8_t
a
=
128
;
// Minimum value
uint8_t
b
=
255
;
// Maximum value
int16_t
c
=
32768
;
uint16_t
d
=
65535
;
int32_t
e
=
2147483648
;
uint32_t
f
=
4294967295U
;
// Use the U suffix to denote an unsigned constant
int64_t
g
=
9223372036854775807LL
;
// Use the LL suffix to denote a long long constant
uint64_t
h
=
18446744073709551615ULL
;
// Use the ULL suffix to denote an unsigned long long constant
// Output result
std
::
cout
<<
"int8_t: "
<<
static_cast
<
int
(
a
)
<<
std::
endl
;
std
::
cout
<<
"uint8_t: "
<<
static_cast
<
unsigned
int
(
b
)
<<
std::
endl
;
std
::
cout
<<
"int16_t: "
<<
c
<<
std
::
endl
;
std
::
cout
<<
"uint16_t: "
<<
d
<<
std
::
endl
;
std
::
cout
<<
"int32_t: "
<<
e
<<
std
::
endl
;
std
::
cout
<<
"uint32_t: "
<<
f
<<
std
::
endl
;
std
::
cout
<<
"int64_t: "
<<
g
<<
std
::
endl
;
std
::
cout
<<
"uint64_t: "
<<
h
<<
std
::
endl
;
return
0
;
}
Output result:
int8_t: -128
uint8_t: 255
int16_t: -32768
uint16_t: 65535
int32_t: -2147483648
uint32_t: 4294967295
int64_t: -9223372036854775807
uint64_t: 18446744073709551615
Fixed-width integer types
Fixed-width integer types ensure variables have a fixed width, which is very useful when precise control over data size and layout is needed. Their naming form is intN_t and uintN_t, where N , where indicates the bit width.
int8_t,int16_t,int32_t,int64_t: Signed fixed-width integer type.uint8_t,uint16_t,uint32_t,uint64_t: Unsigned fixed-width integer type.
Example
#include
#include
int
main
(
)
{
int8_t
a
=
128
;
uint8_t
b
=
255
;
int32_t
c
=
2147483647
;
uint64_t
d
=
18446744073709551615U
;
std
::
cout
<<
"a = "
<<
static_cast
<
int
(
a
)
<<
std::
endl
;
std
::
cout
<<
"b = "
<<
static_cast
<
unsigned
(
b
)
<<
std::
endl
;
std
::
cout
<<
"c = "
<<
c
<<
std
::
endl
;
std
::
cout
<<
"d = "
<<
d
<<
std
::
endl
;
return
0
;
}
Minimum-width integer types
Minimum-width integer types ensure variables have at least the specified bit width, which is very useful when a minimum width guarantee is needed but precise control over the width is not. Their naming form is int_leastN_t and uint_leastN_t.
int_least8_t,int_least16_t,int_least32_t,int_least64_t: Signed minimum-width integer type.uint_least8_t,uint_least16_t,uint_least32_t,uint_least64_t: Unsigned minimum-width integer type.
Example
#include
#include
int
main
(
)
{
int_least8_t
a
=
128
;
uint_least8_t
b
=
255
;
int_least32_t
c
=
2147483647
;
uint_least64_t
d
=
18446744073709551615U
;
std
::
cout
<<
"a = "
<<
static_cast
<
int
(
a
)
<<
std::
endl
;
std
::
cout
<<
"b = "
<<
static_cast
<
unsigned
(
b
)
<<
std::
endl
;
std
::
cout
<<
"c = "
<<
c
<<
std
::
endl
;
std
::
cout
<<
"d = "
<<
d
<<
std
::
endl
;
return
0
;
}
Fastest-width integer types
Fastest-width integer types ensure variables have at least the specified bit width and are as fast as possible on the given system. Their naming form is int_fastN_t and uint_fastN_t.
int_fast8_t,int_fast16_t,int_fast32_t,int_fast64_t: Signed fastest-width integer type.uint_fast8_t,uint_fast16_t,uint_fast32_t,uint_fast64_t: Unsigned fastest-width integer type.
Example
#include
#include
int
main
(
)
{
int_fast8_t
a
=
128
;
uint_fast8_t
b
=
255
;
int_fast32_t
c
=
2147483647
;
uint_fast64_t
d
=
18446744073709551615U
;
std
::
cout
<<
"a = "
<<
static_cast
<
int
(
a
)
<<
std::
endl
;
std
::
cout
<<
"b = "
<<
static_cast
<
unsigned
(
b
)
<<
std::
endl
;
std
::
cout
<<
"c = "
<<
c
<<
std
::
endl
;
std
::
cout
<<
"d = "
<<
d
<<
std
::
endl
;
return
0
;
}
Special types
intmax_t: The largest representable signed integer type.uintmax_t: The largest representable unsigned integer type.intptr_t: A signed integer type large enough to store a pointer.uintptr_t: An unsigned integer type large enough to store a pointer.
Example
#include
#include
int
main
(
)
{
intmax_t
max_int
=
INTMAX_MAX
;
uintmax_t
max_uint
=
UINTMAX_MAX
;
intptr_t
ptr_int
=
reinterpret_cast
<
intptr_t
(
&
max_int
);
uintptr_t
ptr_uint
=
reinterpret_cast
<
uintptr_t
(
&
max_uint
);
std
::
cout
<<
"max_int = "
<<
max_int
<<
std
::
endl
;
std
::
cout
<<
"max_uint = "
<<
max_uint
<<
std
::
endl
;
std
::
cout
<<
"ptr_int = "
<<
ptr_int
<<
std
::
endl
;
std
::
cout
<<
"ptr_uint = "
<<
ptr_uint
<<
std
::
endl
;
return
0
;
}
Boundary value macros
also defines a set of macros representing the boundary values of these types.
INT8_MIN,INT8_MAX,UINT8_MAXINT16_MIN,INT16_MAX,UINT16_MAXINT32_MIN,INT32_MAX,UINT32_MAXINT64_MIN,INT64_MAX,UINT64_MAX
Example
#include
#include
int
main
(
)
{
std
::
cout
<<
"INT8_MIN = "
<<
static_cast
<
int
(
INT8_MIN
)
<<
std::
endl
;
std
::
cout
<<
"INT8_MAX = "
<<
static_cast
<
int
(
INT8_MAX
)
<<
std::
endl
;
std
::
cout
<<
"UINT8_MAX = "
<<
static_cast
<
unsigned
(
UINT8_MAX
)
<<
std::
endl
;
std
::
cout
<<
"INT32_MIN = "
<<
INT32_MIN
<<
std
::
endl
;
std
::
cout
<<
"INT32_MAX = "
<<
INT32_MAX
<<
std
::
endl
;
std
::
cout
<<
"UINT32_MAX = "
<<
UINT32_MAX
<<
std
::
endl
;
return
0
;
}
provides a set of fixed-width integer types that have the same size and representation range across different platforms. Using these types improves code portability and predictability. When writing cross-platform C++ programs, it is recommended to use 's defined types.