| United States-English |
|
|
|
![]() |
HP-UX 64-bit Porting and Transition Guide: HP 9000 Computers > Chapter 5 Writing Portable Code Using Integral Types Defined in <inttypes.h> |
|
The <inttypes.h> header files provide the following features that help you write portable code:
When you use <inttypes.h>, you can maintain one set of source files for both data models. Using the typedefs and constants included in these header files protects your code from underlying data model changes. It also reduces the need to #ifdef platform-specific code, which improves the readability of your source code. There are two <inttypes.h> header files. The following information is included in <sys/_inttypes.h>:
The following information is included in <inttypes.h>:
The basic types in <sys/_inttypes.h> have consistent lengths on UNIX 32-bit and 64-bit platforms. Use these basic data types instead of the standard C language types whenever you want data types to remain the same size across different architectures. Table 5-1 “C Basic Integer Types in <sys/_inttypes.h>” shows the basic integral types in <sys/_inttypes.h>: Table 5-1 C Basic Integer Types in <sys/_inttypes.h>
The pointer data types are signed and unsigned integer data types that are large enough to hold a pointer. A pointer can be moved to or from these data types without corruption. Table 5-2 Pointer Types in <inttypes.h>
One way to improve portability of programs that require integral data items to be 32-bits or 64-bits long, regardless of the hardware platform, is to #include the <inttypes.h> header file, and to make the following substitutions: Table 5-3 Title not available (Using Integer Data Types with Consistent Lengths )
Here are some typedefs in <sys/_inttypes.h> that conform to the XPG5 standard:
Here are some HP extensions:
The following code uses constructs that are not 64-bit clean: Before:
The after code has been made 64-bit clean by using <inttypes.h>: After:
Use the PRIxn constants defined in <inttypes.h> to select the correct printf() formatting option for the data types defined in <inttypes.h>. For example: Before:
After:
The before code prints a 32-bit number on 32-bit platforms and a 64-bit number on a 64-bit platform. The after code prints a 32-bit number, regardless of the platform. The macro PRIx32 from <inttypes.h> is used instead of the literal lx. PRIx32 is then defined as the appropriate formatting symbol. When execution speed of programs is important, use the intfastx_t data types in <inttypes.h>. You select data types based on the minimum integer size required by your application. The <inttypes.h> header file then maps the intfast data types to the fastest appropriate data type for the target hardware platform. These data types include:
Suppose you need a loop to execute as fast as possible on a target platform. You could change the following: Before:
to use the most efficient and portable integer data types as follows: After:
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||