From WikiChip
c/preprocessor
< c
Revision as of 23:44, 4 January 2014 by David (talk | contribs) (Include directive)

The C preprocessor (CPP) is a program that implements the macro language used to transform C programming language program before they are compiled. Some CPPs can be used as a stand-lone utilities which are called by the C compiler during the first steps of the translation process. The C preprocessor provides the ability to include additional files, perform basic substitutions, generate errors. The C preprocessor supports conditional statements which allows conditional compilation based on some previously defined configurations.

Role

The C preprocessor is invoked during the first four phases of translation. The preprocessor's job is:

  1. Perform Trigraph replacement
  2. Perform escaped new-line characters removal
  3. Perform tokenization; replace comments with a single space character
  4. Perform macro expansion

Directives vs text

The source text is separated into two parts: directives and text lines. Directives must start with a the hash character (#).

Include directive

The include directive the syntax #include <char-sequence> new-line or #include "char-sequence". The include directive causes the preprocessor to replace that entire line with the entire contents of the file specified.[1]

The locations where the files are located, or how the preprocessor identifies standard headers is implementation defined. In the case where the preprocessor does not support the include directive with the double quotes, the preprocessor should treat it as if it was in angled brackets (< and >).[2] Some of the common implementations search system include directories for headers specified with angled brackets while searching the current directory for headers specified with double quotes.[3]

The most common application for the include directive is to include standard headers. For example,

#include <stdio.h>

References

  1. C11 §6.10.2 Source file inclusion
  2. C11 §6.10.2 Source file inclusion p3
  3. C11 §6.10.2 Source file inclusion p1-2