top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration

Facebook Login
Site Registration

What precautions should be taken while using macros with arguments?

+7 votes
896 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.
Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...