Mikroelektronika MIKROE-738 Datenbogen
mikroC PRO for PIC32
MikroElektronika
259
A macro call results in two sets of replacements. First, the macro identifier and the parenthesis-enclosed arguments
are replaced by the token sequence. Next, any formal arguments occurring in the token sequence are replaced by the
corresponding real arguments appearing in
actual_arg_list
. Like with simple macro definitions, rescanning occurs
to detect any embedded macro identifiers eligible for expansion.
Here is a simple example:
/* A simple macro which returns greater of its 2 arguments: */
#define _MAX(A, B) ((A) > (B)) ? (A) : (B)
/* Let’s call it: */
x = _MAX(a + b, c + d);
/* Preprocessor will transform the previous line into:
x = ((a + b) > (c + d)) ? (a + b) : (c + d) */
It is highly recommended to put parentheses around each argument in the macro body in order to avoid possible
problems with operator precedence.
Undefining Macros
The
#undef
directive is used to undefine a macro.
#undef macro_identifier
The directive
#undef
detaches any previous token sequence from
macro_identifier
; the macro definition has been
forgotten, and
macro_identifier
is undefined. No macro expansion occurs within the
#undef
lines.
The state of being defined or undefined is an important property of an identifier, regardless of the actual definition. The
#ifdef
and
#ifndef
conditional directives, used to test whether any identifier is currently defined or not, offer a
flexible mechanism for controlling many aspects of a compilation.
After a macro identifier has been undefined, it can be redefined with
#define
, using the same or different token
sequence.
File Inclusion
The preprocessor directive
#include
pulls in header files (extension
.h
) into the source code. Do not rely on
preprocessor to include source files (extension
.c
) — see Add/Remove Files from Project for more information.
The syntax of the
#include
directive has two formats:
#include <header_name>
#include “header_name”