21.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.

Table 21-1. Preprocessor Directives
Directive Meaning Example
#defineDefine preprocessor macro#define SIZE 5

#define FLAG

#define add(a,b) ((a)+(b))

#elifShort for #else #ifsee #ifdef
#elseConditionally include source linessee #if
#endifTerminate conditional source inclusionsee #if
#errorGenerate an error message#error Size too big
#ifInclude source lines if constant expression true#if SIZE < 10

c = process(10)

#else

skip();

#endif

#ifdefInclude source lines if preprocessor symbol defined#ifdef FLAG

do_loop();

#elif SIZE == 5

skip_loop();

#endif

#ifndefInclude source lines if preprocessor symbol not defined#ifndef FLAG

jump();

#endif

#includeInclude text file into source#include <stdio.h>

#include "project.h"

#lineSpecify line number and file name for listing#line 3 final
#pragmaCompiler-specific options#pragma config WDT=OFF
#undefUndefines preprocessor symbol#undef FLAG
#warningGenerate 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 thePreprocessing Directivessection.