top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What precautions should be taken while using macros with arguments?

+7 votes
1,945 views
What precautions should be taken while using macros with arguments?
posted Dec 17, 2013 by Prachi Agarwal

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+2 votes

You have to take care for operator precedence, if you are using macro with arguments.

Proper use of parentheses inside the macro definition will avoid trouble. For example:

#define Square(x) ((x)*(x))

and

 #define Square(x) x*x

This above two will give different result.

answer Dec 18, 2013 by Satyabrata Mahapatra
Thanks  Satyabrata. Adding Example.

If 1st one is used
#define Square(x) ((x)*(x))
then Square(2+2) ((2+2)*(2+2)) = 16

If 2nd one is used
#define Square(x) x*x
then Square(2+2) 2+2*2+2 = 8

The answer 8 is wrong.
...