Getting Started

(lily) #1

Chapter 6: C Functions and Program Structures


e possible source of problems occurs when you reuse a token. You might write:

efine Up 0

d come back a month later, when your header file has 500 lines and forget that
d Up and add:

1

and you won’t get any warnings, other than
me mysterious bugs that you’ll blame on the
see what you’ve done and apply a well-
serve

tion
a complex, or frequently
ine the larger of two

) : (y) )

7;
t c = 0;

( a, b);

ompiler sees.

bstituted
a function is located in only one place
e big difference from a microcontroller
at nothing is pushed on the stack when a macro is used, unlike

On


#d


An
you have already define


#define Up


The preprocessor uses the last #define
this one. But you probably will get so
hardware until much later you finally
de d boink to your own head.


Macro Substit
e can se #de


u
W u fine to make a simple token that replaces
used expression. For example we may want to determ
variables:


#define larger( x, y) ( (x)>(y)? (x


Which we wou use ald s:


int a=
nt b =


9;
i
in


c = larger


The preprocessor replaces the last statement with:


c = ( (a)>(b)? (a) : (b) );


Which is what the c


The expression larger( a, b) looks like a function but isn’t. A macro is su
in the code anywhere that it is used, wh
ch time it is used. Th


ile
and is called ea
perspective is th

Free download pdf