You are on page 1of 15

m 

§ Perhaps the most useful aspect of the C


preprocessor is the ability to create and
use macros.
§ Macros enable you to assign short names
to source code blocks.
§ When you use the macro name in your
source file, the preprocessor replaces it
with the source code block specified in the
macro definition.
Õ   
§ ñ macro definition includes the name of the
macro, the macro body, and may include
macro arguments.
§ Simple Macros require no arguments and are
the simplest macros to define.
§ Complex Macros accept one or more
arguments and may be used like functions.
§ Macro Operators lists special macro operators
that may be used in macro definitions.
§ Predefined Macros lists macros that are
defined by the compiler at compile-time.
 m 
§ ñ simple macro is merely an abbreviation
for a fragment of code.
§ It is often called a manifest constant
because it defines a name for a constant
value.
§ Macros must be defined using the
À  directive before they can be
used. For example:
÷ 
§ Àdefine LEN 128 defines a macro named
LEN. When LEN is used in your program
(or in preprocessor directives) it is
replaced with the text 128.
§ So, a C statement like char buffer[LEN];is
expanded by the preprocessor into char
buffer[128]; and is subsequently compiled
by the compiler.
ô  
§ ñ complex macro accepts arguments and
generates a fragment of code using the
values of those arguments.
§ Macros that accept arguments appear to
be functions. However, arguments are not
typed as in a C function. They are merely
replaced by the text passed to the macro
when expanded.
§ Macros with arguments must be defined
using the À  directive before they
can be used.
§ The argument list is enclosed in parentheses
and must immediately follow the macro
name. Spaces are not allowed between and
macro name and open parenthesis.
Example:-
§ Àdefine Mñ (x,y) ((x) > (y) ? (x) : (y))
§ defines a macro named m  that takes two
arguments (x and y). When m  is used in
your program it is replaced with the text 
 . If x and y are numeric
constants, the preprocessor can determine
the result of the macro and substitute the
greater value.
§ The number of arguments passed to a
macro must match the number of
arguments specified in the macro
definition.
§ It is common practice to surround
arguments used in a macro definition with
parentheses. This is done so that
compound expressions, when passed to a
macro, do not cause unwanted side-
effects.
§ Macros may be defined with a null or
empty argument list. For example:
Àdefine MYMñCRO() (func();)
§ To call such a macro, you must specify the
macro name along with an empty
argument list. For example:MYMñCRO()
§ To pass an empty argument to a macro,
you must include at least one whitespace
character in the place of that argument.
m  
§ Three preprocessor operators may be
used in À  or À and À
preprocessor directives.
§ Stringize Operator (À)
§ Token-Pasting Operator (ÀÀ)
§ Defined Operator
    
§ The stringize or number-sign operator ('À'),
when used within a macro definition, converts
a macro parameter into a string constant.
This operator may be used only in a macro
that has a specified argument or parameter
list.
§ When the stringize operator immediately
precedes the name of one of the macro
parameters, the parameter passed to the
macro is enclosed within quotation marks and
is treated as a string literal. For example:
Àdefine stringizer(x) printf (Àx "\n")
stringizer(text)
Õ
  
§ The token-pasting operator (ÀÀ) within a
macro definition combines two arguments. It
permits two separate tokens in the macro
definition to be joined into a single token.
§ If the name of a macro parameter used in the
macro definition is immediately preceded or
followed by the token-pasting operator, the
macro parameter and the token-pasting
operator are replaced by the value of the
passed parameter. Text that is adjacent to
the token-pasting operator that is not the
name of a macro parameter is not affected.
§ For example:
Àdefine tokenpaster(n) printf ("token" Àn " =
%d",tokenÀÀn)
tokenpaster(34);
£ 
§ The preprocessor  operator is used in
constant expressions to determine if an
   has been defined (by the À 
preprocessor directive).
§ If the specified   is defined, the value
is true (non-zero). If the symbol is not
defined, the value is false (zero). The
 operator is specified as follows:
 (   )

   
§ The  operator may be used in À or
À directives only.
 m 
§ The C Compiler provides the following predefined
constants you may use in preprocessor directives
and C code to create portable programs.
§ There are two (2) leading and trailing underscore
characters ('_') used for the predefined
constants.
ô  
  
ô rersion number of the compiler (for example, 101 for version 1.01).


E Date when the compilation was started in ñNSI format (month dd yyyy).

LE Name of the file being compiled.

LO
Defined to 1 to indicate that the LO
directive is active.

EL Defined to 1 to indicate the Keil CñRM Compiler is used.

LNE Current line number in the file being compiled.


m CPU mode selected:
 0 for m mode,
 1 for
m mode.


mE Time when the compilation was started.


ô Defined to 1 to indicate full conformance with the ñNSI C Standard.

You might also like