5.14.1 Preprocessor Directives
The XC8 accepts several specialized preprocessor directives, in addition to the standard directives. All of these are tabulated below.
Directive | Meaning | Example |
---|---|---|
# | Preprocessor null directive, do nothing. | # |
#define | Define preprocessor macro. |
|
#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. |
|
#ifdef | Include source lines if preprocessor symbol defined. |
|
#ifndef | Include source lines if preprocessor symbol not defined. |
|
#include | Include text file into source. |
|
#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 #
operator to
convert an argument to a string and the ##
operator 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 might require further expansion. Remember that once a macro
identifier has been expanded, it will not be expanded again if it appears after
concatenation.
__VA_ARGS__
to expand this argument list in the macro replacement
text. For example, the following PIC18 code places a variable number of words into program
memory. Note that the arguments were converted to strings using the #
operator so as to be usable in the __asm()
statement.#define PLACE_WORDS(...) \
__asm("PSECT cdata,class=CODE,reloc=2,noexec"); \
__asm("DW " #__VA_ARGS__)
Call this macro in the usual way, passing it as many
arguments as are required, for
example:PLACE_WORDS(0x100, 1, 223, 0x30F0);