22.1 Preprocessor Directives

The XC32 accepts several specialized preprocessor directives, in addition to the standard directives. All of these are tabulated below.

Table 22-1. Preprocessor Directives
DirectiveMeaningExample
#Preprocessor null directive, do nothing.#
#assertGenerate error if condition false.#assert SIZE > 10
#defineDefine preprocessor macro.
#define SIZE (5)
#define FLAG
#define add(a,b) ((a)+(b))
#elifShort for #else #if.see #ifdef
#elseConditionally include source lines.see #if
#endifTerminate conditional source inclusion.see #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 filename for listing#line 3 final
#nn filename(where nn is a number, and filename is the name of the source file) the following content originated from the specified file and line number.#20 init.c
#pragmaCompiler specific options.See the Pragma Directives section in this guide.
#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; 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 can require further expansion. Remember, that once a macro identifier has been expanded, it will not be expanded again if it appears after concatenation.