Example
#define Echo(a) a
#define Echo(a) (a)
I realize there probably isn’t a significant difference here, but why would you ever want to include the a
within parenthesis inside the macro body? How does it alter it?
Example
#define Echo(a) a
#define Echo(a) (a)
I realize there probably isn’t a significant difference here, but why would you ever want to include the a
within parenthesis inside the macro body? How does it alter it?
Just for the record, I landed from Here How to fix mathematical errors while using macros and I will try to expand this Answer here to fit the Other one.
You are asking about the difference about:
which is fine as long as you do not understand the macro it self (I am not an expert too :) ).
First of all you already (probably) know that there is Operator Precedence, so there is a huge difference of this two programs:
1):
Output:
and:
Output:
15
Now lets preplace
+
with*
:The compiler treats
a * b
like for examplea == 5
andb == 10
which does5 * 10
.But, when you say:
ADD ( 2 + a * 5 + b )
Like here:You get
105
, because the operator precedence is involved and treats2 + b * 5 + a
as
( 2 + 5 ) * ( 5 + 10 )
which is
( 7 ) * ( 15 )
==105
But when you do:
you get
37
because ofwhich means:
which means:
Short answer, there is a big difference between:
and
Suppose you have
What happens if I say:
Now if I slighlty change the macro:
Remember, the arguments aren't evaluated or anything, only textual substitution is performed.
EDIT
For an explanation about having the entire macro in parentheses, see the link posted by Nate C-K.