天天看點

C Reference Manual Reading Notes: 009 Preprocessor Lexical Conventions

    The preprocessor does not parse the source text, but it does break it up into tokens for the purpose of locating macro calls. The lexical conventions of the preprocessor are somewhat different from the compiler proper; the preprocessor recognize the normal C tokens, and additionally recognize as "tokens" other characters that would not be recognized as valid in C proper. This enables the preprocessor to recognize file names, the presence and absence whitespace, and the location of end-of-line markers.

    A line beginning with # is treated as a preprocessor command; the name of the command must follow the # character. Standard C permits whitespace to precede and follow the # character ont the same source line, but some older compilers do not. A line whose only non-whitespace character is a # is termed a null directive in standard C and is treated the same as a blank line. Older implementations may behave differently.

    The remainder of the line following the command name may contain arguments for the command if appropriate. If a preprocessor command takes no arguments, then the remainder of the command line should be empty except perhaps for whitespace characters or comments. Many pre-ISO compilers silently ignore all characters following the expected arguments(if any); this can lead to portability problems. The arguments to preprocessor commands are generally subject to macro replacement.

    Preprocessor lines are recognized before macro expansion. Therefore, if a macro expands into something that looks like a preprocessor command, that command will not be recognized by the preprocessors in Standard C or in most other C compilers.(Some old UNIX implementations violate this rules.) For example, the result of the following code is not to include the file math.h in the program being compiled:

                     #define GETMATH #include <math.h>

                     GETMATH

                Instead, the expanded token sequences

                      #include <math.h>

                is merely passed through and compiled as (erroneous) C code

    All source lines (including preprocessor command lines) can be continued by preceding the end-of-line markers by a backslash character, /. This happens before scanning for preprocessor commands. Such as the follows:

                The preprocessor

                      #define err(flag,msg) if(flag)/

                                             printf(msg)

                 is the same as

                      #define err(flag,msg) if(flag) printf(msg)

                 If the backslash character below immediately precedes the end-of-line marker, these two lines

                      #define BACKSLASH /

                      #define ASTERISK *

                 will be treated as the single preprocessor command

                      #define BACKSLASH #define ASTERISK *

                 and expanded token sequences is

                      #define ASTERISK *

    The preprocessor treats comments as whitespace, and line breaks within comments do not terminate preprocessor commands. For example:

                      #define COMMENT "comment"

                  will be treated as:

                      #define COMMENT  "comment"

繼續閱讀