C/C++ Programmer's Guide (G06.27+, H06.08+, J06.03+)

Writing Portable Programs
A portable application is an application designed using open, industry-standard languages,
application program interfaces (APIs), and middleware, such as the C language and POSIX.1 API.
A portable application can be moved between hardware systems and middleware environments
from different vendors. This subsection provides guidelines for writing portable C programs.
Complying With Standards
Writing programs to international standards enables you to move them between different hardware
and software environments with little effort. Write your C programs to comply with the ISO/ANSI
C standard: ISO/IEC 9899:1990, Programming Languages–C plus features from 1999 update
to this standard (see Appendix G: c99 Selected Features (C99LITE) or, for TNS/E programs on
H06.21 and later H-series RVUs or for J06.10 and later J-series RVUs, see “c99 Full Support”
(page 461)). For a complete description of ANSI C, see ANSI X3.159.
If you are writing a program to run in the Open System Services (OSS) environment, you should
also comply with these standards:
X/Open Common Applications Environment (CAE) Specification, System Interfaces and
Headers, Issue 4, Version 2, 1994.
X/Open Common Applications Environment (CAE) Specification, System Interface Definitions,
Issue 4, Version 2, 1994.
ISO/IEC 9945-1:1990, Information Technology—Portable Operating System Interface (POSIX)
— Part 1: System Application Program Interface (API) [C Language].
ISO/IEC DIS 9945-2:1992, Information Technology—Portable Operating System Interface
(POSIX) — Part 2: Shell and Utilities.
The OSS environment provides all the function calls specified in the POSIX.1 standard, and many
of the function calls specified in the POSIX.2 standard and the XPG4 specification. Before using
a function that is not part of the ISO/ANSI C standard, check to make sure that the OSS environment
provides that function.
Feature-test macros enable you to check how well a program complies with these standards. The
C header files contain definitions required by the ISO/ANSI C standard, the POSIX.1 standard,
the POSIX.2 standard, the XPG4 specification, and the XPG4.2 specification. You control the
visibility of these definitions with feature-test macros. If a program does not conform to the standard
or specification controlled by a feature-test macro, the compiler issues error and warning messages.
The five feature-test macros that apply to standards compliance are:
Makes visible identifiers required or permitted by the POSIX.1 standard._POSIX_C_SOURCE=1
Makes visible identifiers required or permitted by the POSIX.1 and POSIX.2
standards.
_POSIX_C_SOURCE=2
Makes visible identifiers required or permitted by the XPG4 specification.
Represents the OSS compile default.
_XOPEN_SOURCE
Makes visible identifiers specified in the XPG4.2 specification as extensions to
the XPG4 specification.
_XOPEN_SOURCE_EXTENDED=1
Makes visible the identifiers required or permitted by extensions made by HP.
Represents the Guardian compile default for the TNS C compiler. Includes some
_TANDEM_SOURCE
of the identifiers required by the XPG4 specification, in addition to POSIX.1 and
POSIX.2 standards.
The XPG4 specification includes the identifiers required by the POSIX.1 and POSIX.2 standards.
Therefore, specifying the _XOPEN_SOURCE macro automatically includes the identifiers included
by the _POSIX_C_SOURCE=2 macro.
For more details about feature-test macros, see Feature-Test Macros (page 162).
Writing Portable Programs 45