Preprocessor DirectivesPreprocessor directives begin with the # symbol.
Strings here do not have quotations.
NULL DIRECTIVE
#
FILE INCLUSION
#include "filename"
or
#include <filename>
- Makes a direct substitution of the file specified by filename into the source file.
- When quotations are used, the first directory searched for the included file is the one associated with the source file.
- If the included file is not found or angle brackets are used, the search path is implementation dependant.
- File inclusion can be nested.
- This is typically used for header files.
DEFINITION DIRECTIVES
#define token
- Defines the token for other preprocessor directives.
Example:
#define DEBUG
#if DEBUG
printf("Debug/n");
#endif
#undef token
- Undefines the specified token for further preprocessor directives.
#define token string
- Any place that the token exists is replaced by the string.
- The exception to this is strings surrounded by quotes.
- The string can cover multiple lines if the lines to be continued end in '\'.
- Be very careful about using parenthesis as these are direct substitutions.
#define token(token-parameter-list) string-including-tokens-from-parameter-list
- This is what is refered to as a macro.
- The token parameter list acts similar to variables for a function.
- The passed in parameters are replaced into the string then the string replaces the token(token-parameter-list) directive in the source code.
- Be very careful about using parenthesis as these are direct substitutions.
Example:
#define MULTIPLY(a,b) (a*b) //incorrect
#define MULTIPLY(a,b) ((a)*(b)) //correct
The first results in this:
MULTIPLY(3+2,5+3) -> (3+2*5+3) -> (3+10+3) -> 16
The second does the correct operation:
MULTIPLY(3+2,5+3) -> ((3+2)*(5+3)) -> (5*8) -> 40
- Using # in front of a parameter in the executable string will result in the value of the parameter being surrounded by quotes.
Example:
#define QUOTEME(str) #str "additional string"
Result:
QUOTEME(text) -> "text" "additional string" -> "textadditional string"
- Using ## between two parameters will cause a concatenation.
Example:
#define CATME(str1, str2) str1 ## str2
Result:
CATME(text1, text2) -> text1text2
- Macros make it hard to debug as most debugers will not jump to the macro while stepping through operations.
- Instead use inline functions if available.
CONDITIONAL DIRECTIVES
#if expression | if true |
#ifdef token | if token defined |
#ifndef token | if token not defined |
#elif expression | else if |
#else | else |
#endif | ends the if/elif/else section |
LINE CONTROL
#line linenumber ["filename"]
- Causes the preprocessor to believe that the next line number is equal to linenumber.
- Also, optionally filename is the currently compiled file.
PRAGMA
#pragma tokens
- Completely preprocessor dependant.
PREDEFINED TOKENS
__LINE__ | Constant containing the current line number |
__FILE__ | Constant containing the current file name |
__DATE__ | Constant containing the date of compilation in the form "Mmm dd yyyy" |
__TIME__ | Constant containing the time of compilation in the form "hh:mm:ss" |
__STDC__ | The constant 1. |
ERROR GENERATION
#error string
- Processor writes a diagnostic message including the string.
|