6

One of my friend showed me this program and asked me why is i variable getting incremented twice.

According to my understanding MAX(i++, ++j); in this line i is first send as a parameter and then incremented, so if the initial value of i is 10 then the incremented value should be 11, but it shows the incremented value of i as 12.

Program :

#include<stdio.h>

#define MAX(x,y) (x)>(y)?(x):(y)

void main(void)
{
    int i = 10;
    int j = 5;
    int k = 0;

    k = MAX(i++, ++j);

    printf("%d %d %d",i,j,k);
}

Output :

12 6 11

Can someone please explain me how is the value incremented to 12 ?

Thanks.

Searock
  • 6,278
  • 11
  • 62
  • 98

8 Answers8

8

MAX is a macro, not a function. In your use case, it expands to:

k = (i++) > (++j) ? (i++) : (++j);
Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
6

Your macro substitution means that you write (i++)>(++j)?(i++):(++j).

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
5

Your macro definition contains the arguments twice

#define MAX(x,y) (x)>(y)?(x):(y)

Which makes

 k = MAX(i++, ++j);

expand to

k = (i++)>(++j)?(i++):(j++);

And thus, increments twice.

rubenvb
  • 74,642
  • 33
  • 187
  • 332
4

MAX is not a function. i is not send as a parameter.

MAX is a macro. It is text-replaced where it's used:

k = (i++)>(j++)?(i++):(j++)

Now you know why it is incremented twice.

Didier Trosset
  • 36,376
  • 13
  • 83
  • 122
2

Macro do simple text substitution, so after macro expansion, the k = MAX(i++, ++j); line is seen by the compiler as :

k = (i++)>(++j)?(i++):(++j);
Sylvain Defresne
  • 42,429
  • 12
  • 75
  • 85
2

Your MAX macro expands to

(i++)>(++j)?(i++):(++j)

showing why you get a double increment.

gspr
  • 11,144
  • 3
  • 41
  • 74
2

The macro will be expanded to something like in pseudo-C code :

if( i++ > j++)  // i is incremented the first time,  j is incremented once
   return i++;  // i is incremented the second time
else
   return j++;  // we never go there, so j is not incremented twice
Xavier T.
  • 40,509
  • 10
  • 68
  • 97
1

When using MAX(i++, ++j), the generated code will be :

(i++) > (++j) ? (i++) : (++j)

Using preprocessor macro just expand the code and copy/paste the arguments in place. You might want to use a function for this case.

int max(int x, int y)
{
  return (x > y ? x : y);
}

Modern compiler will inline it while respecting the original behavior of the function call.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Nekresh
  • 2,948
  • 23
  • 28