Previous Page
Next Page

14.6. The #pragma Directive

The #pragma directive is a standard way to provide additional information to the compiler. This directive has the following form:

    #pragma [tokens]

If the first token after #pragma is STDC, then the directive is a standard pragma. If not, then the effect of the #pragma directive is implementation-dependent. For the sake of portability, you should use #pragma directives sparingly.

If the preprocessor recognizes the specified tokens, it performs whatever action they stand for, or passes information on to the compiler. If the preprocessor doesn't recognize the tokens, it must ignore the #pragma directive.

Recent versions of the GNU C compiler and Microsoft's Visual C compiler both recognize the pragma pack(n), for example, which instructs the compiler to align structure members on certain byte boundaries. The following example uses pack(1) to specify that each structure member be aligned on a byte boundary:

    #if defined( _  _GNUC_  _ ) || defined( _MSC_VER )
      #pragma pack(1)                              // Byte-aligned: no padding.

Single-byte alignment ensures that there are no gaps between the members of a structure. The argument n in a pack pragma is usually a small power of two. For example, pack(2) aligns structure members on even-numbered byte addresses, and pack(4) on four-byte boundaries. pack( ) with no arguments resets the alignment to the implementation's default value.

C99 introduced the following three standard pragmas:

    #pragma  STDC  FP_CONTRACT  on_off_switch
    #pragma  STDC  FENV_ACCESS  on_off_switch
    #pragma  STDC  CX_LIMITED_RANGE  on_off_switch

The value of the on_off_switch must be ON, OFF, or DEFAULT. The effects of these pragmas are discussed in "Mathematical Functions" in Chapter 16.

Previous Page
Next Page