Hello,
I just cant figure out why i get this output. When i run this program, i get the output as 11 11 11, how ? shouldn't it be 11 10 10 ?? Please explain...

#include<stdio.h>
#define MAX(x,y) (x)>(y)?(x):(y)

int main()
{
int i=10,j=9,k=0;
k=MAX(i++,++j);
printf("%d %d %d",i,j,k);
}

Thanks!! :)

Recommended Answers

All 6 Replies

Why should it be?

Remove MAX and put the macro code directly in your code and see what happens.

Post the results.

You mean like this...

#include<stdio.h>
#define MAX(x,y) (x)>(y)?(x):(y)
int main()
{
 int i=10,j=9,k=0;
 i++,++j;
 k = i>j? i: j;
 printf("%d %d %d",i,j,k);
 return 0;
}

I get the output as 11 10 11... Yeah i understand why i get 11 10 11, but why 11 11 11 in the previous code ??

i++,++j;
k = i>j? i: j;

That's not your macro. The macro is only 1 line.
Try it again.

Study the definition of a magro again.

Member Avatar for I_m_rude

since i is in brackets, then i will be incremented, also j will be incremneted. then 11>10, then ++j will again executed, so k is assigned 11. so all are 11 at this time.

thanks.

@nitin1 cant get you..

That's not your macro. The macro is only 1 line.

@WaltP
Okay, is this one correct ??

#include<stdio.h>
#define MAX(x,y) (x)>(y)?(x):(y)

int main()
{
int i=10,j=9,k=0;
k = (i++) > (++j) ? i++ : ++j;
printf("%d %d %d",i,j,k);
return 0;
}

Yes, i get 11 11 11, im begining to understand, i++ is done first, followed by ++j since they are given in braces, 10 is not greater than 10 so k=++j, so k=11, in the next step the incremented value of i and j,k are displayed as 11 11 11, am i right ??

    #include<stdio.h>
   #define MAX(x,y) (x)>(y)?(x):(y)

    int main()
    {
    int i=11,j=9,k=0;
    k=MAX(i,j);
    printf("the max is %d",k);
 return 0;
   }

why dont u try like this

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.