1

I am referring to this question

#define max(a,b) ((a<b)?b:a)

this will have a some side effect as stated in the answer;

The side effects appear if you use max(a++,b++) for example (a or b will be incremented twice)

I am not able to understand this side effect; why a or b will be incremented twice when we use max(a++,b++) ?

Community
  • 1
  • 1
user25108
  • 383
  • 5
  • 15

2 Answers2

7

If you use max(a++,b++) in your code like this,

x = max(a++,b++);

a text replacement happens as

x = ((a++<b++)? b++ : a++);
      ^   ^     ^---------Increment if condition is true
      |---|---------Increment

So you will be incrementing either a or b twice...

1

max(a++, b++) will be expanded as ((a++ < b++) ? b++ : a++). While evaluating from the left the expression (a++ < b++) get precedence and will increment both a and b. This is the first increment. Then depending the output of < operator, either a or b will get incremented again (this is the second increment).

yasouser
  • 5,113
  • 2
  • 27
  • 41