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

C Standard Library

C Standard Library

In C programming, when handling integer types, we often need to ensure code portability across different platforms. Different platforms may have different integer sizes and representations, which can cause code to behave inconsistently in different environments.

To solve the above problems, the C standard library provides theheader file, which defines a set of fixed-size integer types and corresponding formatting macros, helping developers write portable code.

inttypes.h is a very important header file in the C standard library. It provides a set of fixed-width integer types and corresponding formatting macros. By using these types and macros, developers can ensure that integer sizes and formatted output are consistent across different platforms, thereby avoiding potential problems.

Fixed-width integer types

inttypes.h defines a set of fixed-width integer types that have the same size on different platforms. The following are common fixed-width integer types:

These types ensure that integer sizes are consistent across different platforms, thereby improving code portability.

Formatting macros

inttypes.h also defines a set of formatting macros for formatting fixed-width integer types in functions such asprintfandscanfprintf. These macros ensure that the formatted output of integers is consistent across different platforms.

The following are common formatting macros:

These macros are used as follows:

Example

#include

#include

int

main

(

)

{

   

int32_t

myInt

=

42

;

   

printf

(

"The value of myInt is: %"

PRId32

"\n"

,

myInt

)

;

   

return

0

;

}

In the code above,PRId32 The macro is used to format int32_t signed integers of type, ensuring consistent output across different platforms.

The output is:

The value of myInt is: 42


Other macros

inttypes.h also defines some other useful macros, for example:

These macros help developers safely use fixed-width integer types in code, avoiding overflow and other potential problems.

Example

The following is an example of usinginttypes.hcomplete example showing how to define and use fixed-width integer types and their formatting macros:

Example

#include

#include

int

main

(

)

{

   

int32_t

myInt

=

42

;

   

uint64_t

myUInt

=

1234567890123456789ULL

;

   

printf

(

"The value of myInt is: %"

PRId32

"\n"

,

myInt

)

;

   

printf

(

"The value of myUInt is: %"

PRIu64

"\n"

,

myUInt

)

;

   

printf

(

"The minimum value of int32_t is: %"

PRId32

"\n"

,

INT32_MIN

)

;

   

printf

(

"The maximum value of uint64_t is: %"

PRIu64

"\n"

,

UINT64_MAX

)

;

   

return

0

;

}

The output is:

The value of myInt is: 42

The value of myUInt is: 1234567890123456789

The minimum value of int32_t is: -2147483648

The maximum value of uint64_t is: 18446744073709551615

In the above code, we defined a int32_t signed integer of type and an uint64_t unsigned integer of type, and printed them using the corresponding formatting macros. We also used INT32_MIN and UINT64_MAX macros to print the minimum and maximum values of these types.