Datasheet

MACROS
Macros provide a mechanism for a token replacement, prior to compilation, with or
without a set of formal, function-like parameters.
Defining Macros and Macro Expansions
The
#define directive defines a macro:
#define macro_identifier <token_sequence>
Each occurrence of macro_identifier in the source code following this control line
will be replaced in the original position with the possibly empty token_sequence
(there are some exceptions, which are discussed later). Such replacements are
known as macro expansions.token_sequence is sometimes called the body of a
macro. An empty token sequence results in the removal of each affected macro
identifier from the source code.
No semicolon (
;) is needed to terminate a preprocessor directive. Any character
found in the token sequence, including semicolons, will appear in a macro expan-
sion.
token_sequence terminates at the first non-backslashed new line encoun-
tered. Any sequence of whitespace, including comments in the token sequence, is
replaced with a single-space character.
After each individual macro expansion, a further scan is made of the newly expand-
ed text. This allows the possibility of using nested macros: the expanded text can
contain macro identifiers that are subject to replacement. However, if the macro
expands into something that looks like a preprocessing directive, such directive will
not be recognized by the preprocessor. Any occurrences of the macro identifier
found within literal strings, character constants, or comments in the source code will
not be expanded.
A macro won’t be expanded during its own expansion (so
#define MACRO MACRO
won’t expand indefinitely).
Here is an example:
/* Here are some simple macros: */
#define ERR_MSG "Out of range!"
#define EVERLOOP for( ; ; )
/* which we could use like this: */
main() {
EVERLOOP {
...
if (error) { Lcd_Out_Cp(ERR_MSG); break; }
...
}
}
220
MIKROELEKTRONIKA - SOFTWARE AND HARDWARE SOLUTIONS FOR EMBEDDED WORLD
Language Reference
mikroC PRO for AVR
CHAPTER 5