22.2 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
Directive Meaning Example
# Preprocessor null directive, do nothing. #
#assert Generate error if condition false. #assert SIZE > 10
#define Define preprocessor macro.
#define SIZE (5)
#define FLAG
#define add(a,b) ((a)+(b))
#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
    c = process(10)
#else
    skip();
#endif
#ifdef Include source lines if preprocessor symbol defined.
#ifdef FLAG
    do_loop();
#elif SIZE == 5
    skip_loop();
#endif
#ifndef Include source lines if preprocessor symbol not defined.
#ifndef FLAG
    jump();
#endif
#include Include text file into source.
#include <stdio.h>
#include “project.h”
#line Specify 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
#pragma Compiler specific options. See the Pragma Directives section in this guide.
#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; 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.