20.2 Preprocessing Directives
The compiler accepts several specialized preprocessor directives in addition to the standard directives. All of these are listed in the following table.
Directive | Meaning | Example |
---|---|---|
#define |
Define preprocessor macro | #define SIZE 5
|
#elif |
Short for #else #if |
see #ifdef |
#else |
Conditionally include source lines | see #if |
#endif |
Terminate conditional source inclusion | see #if |
#error |
Generate an error message | #error Size too big |
#if |
Include source lines if constant expression true | #if SIZE < 10
|
#ifdef |
Include source lines if preprocessor symbol defined | #ifdef FLAG
|
#ifndef |
Include source lines if preprocessor symbol not defined | #ifndef FLAG
|
#include |
Include text file into source | #include <stdio.h>
|
#line |
Specify line number and file name for listing | #line 3 final |
#pragma |
Compiler-specific options | #pragma config
WDT=OFF |
#undef |
Undefines preprocessor symbol | #undef FLAG |
#warning |
Generate a warning message | #warning Length not
set |
Macro expansion using arguments can use the #
character
to convert an argument to a string, and the ##
sequence to concatenate
arguments. If two expressions are being concatenated, consider using two macros in case
either expression requires substitution itself, so for example
#define paste1(a,b) a##b
#define paste(a,b) paste1(a,b)
lets you use the paste
macro to concatenate two
expressions that themselves may require further expansion. Remember that once a macro
identifier has been expanded, it will not be expanded again if it appears after
concatenation.
For implementation-defined behavior of preprocessing directives, see the 22.13 Preprocessing Directives section.