Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

The C++ standard library

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:

In addition, it defines the maximum-width integer types:

as well as types for bit operations:

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.

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.

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.

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

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.

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.