# Thread: What different in macros?

1. ## What different in macros?

Code:
```#define max(x,y) (x)>(y)?(x):(y)
.........
.............
.........
int a=5,b=3;
int k=max(a++,++b);
cout<<k;
...............```
if it's gonna be a function the call is like:
Code:
`max(5,4)`
but when its, macro how the call ll be and how the proccesing will be?

a detailed explanation pls

2. Perform the macro expansion yourself then reason about the resulting code:
Code:
```int a=5,b=3;
int k=(a++)>(++b)?(a++):(++b);
cout<<k;```
Consequently, it would be better to use an inline function template instead of a macro.

3. Originally Posted by laserlight
Perform the macro expansion yourself then reason about the resulting code:
Code:
```int a=5,b=3;
int k=(a++)>(++b)?(a++):(++b);
cout<<k;```
Consequently, it would be better to use an inline function template instead of a macro.
yes i know.but the answer is different from what we have in function...
The called number is not 5,4 basically

4. take for example:
Code:
```#define MAN(x,y) (x)>(y)?(x):(y)
.............
int i=10,j=5,k=0;
k= MAN(i++,++j);
printf("%d %d %d",i,j,k);```
i thought the answer would be: 11,6,10
how is it

5. . . . think about it for one moment, here. laserlight has suggested you expand it out yourself, which s/he has done kindly for you.

How many times is a going to be incremented? How about b?

6. Originally Posted by dpp
yes i know.but the answer is different from what we have in function...
If you know, then there should be no "but"

There is a sequence point at the '?'. So, the post-increment would have been effective by the time control reaches the second or third operands of the ternary operator, whereas with a function call the first argument would have the original value of a.

Originally Posted by dpp
The called number is not 5,4 basically
What do you mean by "called number"? There is no function call.

7. yeah thanks

8. That's exactly why macros are evil and should be avoided whenever possible.

9. What laserlight and cpjust are pointing out:

Post and pre incrementing on the same line bring out demons in your code, especially if macros are used in tandem with increment operators. Your macro will just expand this out. Troubleshooting these are a pain as well. Caution is very much needed when you define those. I have done something similar to the following:

Code:
```/* Some universal defines to aid in swapping out two keys of int types */
typedef int Item;      /*can easily be modified as necessary */
#define key(A)  (A)
#define less(A, B) (key(A) < key(B))
#define exch(A, B) {Item t = A; A = B; B = t;}

/* more defines built from above definitions */
#define compexch(A, B) if (less(B, A)) exch(A, B)```