Why is “i” variable getting incremented twice in m

2019-01-15 19:56发布

问题:

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.

回答1:

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



回答2:

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

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


回答3:

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.



回答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.



回答5:

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);


回答6:

Your MAX macro expands to

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

showing why you get a double increment.



回答7:

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


回答8:

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.



标签: c macros