Pre-defined Standard Macros

Table of Contents - Standards

Table of Contents - Standards
Language Standards
Example: C Standards
Example: Pre-C89
Unix Standards
Example: Unix Standards

Language Standards

Language standards requires the existance of pre-defined macros.

Name Macro Standard
C89 __STDC__ ANSI X3.159-1989
C90 __STDC_VERSION__ ISO/IEC 9899:1990
C94 __STDC_VERSION__ = 199409L ISO/IEC 9899-1:1994
C99 __STDC_VERSION__ = 199901L ISO/IEC 9899:1999
C++98 __cplusplus = 199707L ISO/IEC 14882:1998
C++/CLI __cplusplus_cli = 200406L ECMA-372
EC++ __embedded_cplusplus Embedded C++

Example: C Standards

#if defined(__STDC__)
# define PREDEF_STANDARD_C_1989
# if defined(__STDC_VERSION__)
#  define PREDEF_STANDARD_C_1990
#  if (__STDC_VERSION__ >= 199409L)
#   define PREDEF_STANDARD_C_1994
#  endif
#  if (__STDC_VERSION__ >= 199901L)
#   define PREDEF_STANDARD_C_1999
#  endif
# endif
#endif

Please note that not all compliant compilers provides the correct pre-defined macros. For example, Microsoft Visual C++ does not define __STDC__, or Sun Workshop 4.2 supports C94 without setting __STDC_VERSION__ to the proper value. Extra checks for such compilers must be added.

Example: Pre-C89

Pre-C89 compilers do not recognize certain keywords. Let the preprocessor remove those keywords for those compilers.

#if !defined(PREDEF_STANDARD_C_1989) && !defined(__cplusplus)
# define const
# define volatile
#endif

Unix Standards

Unix standards require the existance macros in the <unistd.h> header file.

Name Macro Standard
POSIX.1-1988 _POSIX_VERSION = 198808L .
POSIX.1-1990 _POSIX_VERSION = 199009L ISO/IEC 9945-1:1990
POSIX.2 _POSIX2_C_VERSION = 199209L ISO/IEC 9945-2:1993
POSIX.1b-1993 _POSIX_VERSION = 199309L IEEE 1003.1b-1993
POSIX.1-1996 _POSIX_VERSION = 199506L IEEE 1003.1-1996
POSIX.1-2001 _POSIX_VERSION = 200112L IEEE 1003.1-2001
XPG3 _XOPEN_VERSION = 3 X/Open Portability Guide 3 (1989)
XPG4 _XOPEN_VERSION = 4 X/Open Portability Guide 4 (1992)
SUS _XOPEN_VERSION = 4 && _XOPEN_UNIX X/Open Single UNIX Specification (UNIX95)
SUSv2 _XOPEN_VERSION = 500 X/Open Single UNIX Specification, Version 2 (UNIX98)
SUSv3 _XOPEN_VERSION = 600 Open Group Single UNIX Specification, Version 3 (UNIX03)

Example: Unix Standards

The following examples assumes the definition of these macros.

#if defined(unix) || defined(__unix__) || defined(__unix)
# define PREDEF_PLATFORM_UNIX
#endif
#if defined(PREDEF_PLATFORM_UNIX)
# include <unistd.h>
# if defined(_XOPEN_VERSION)
#  if (_XOPEN_VERSION >= 3)
#   define PREDEF_STANDARD_XOPEN_1989
#  endif
#  if (_XOPEN_VERSION >= 4)
#   define PREDEF_STANDARD_XOPEN_1992
#  endif
#  if (_XOPEN_VERSION >= 4) && defined(_XOPEN_UNIX)
#   define PREDEF_STANDARD_XOPEN_1995
#  endif
#  if (_XOPEN_VERSION >= 500)
#   define PREDEF_STANDARD_XOPEN_1998
#  endif
#  if (_XOPEN_VERSION >= 600)
#   define PREDEF_STANDARD_XOPEN_2003
#  endif
# endif
#endif

Please note that not all compliant compilers provides the correct pre-defined macros. For example, IBM xlC supports Unix without setting any of the __unix__ macros. Extra checks for such compilers must be added.