0

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!! :)

4
Contributors
6
Replies
7
Views
4 Years
Discussion Span
Last Post by rithish
Featured Replies
  • 1
    WaltP 2,905   4 Years Ago

    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. Read More

0

Why should it be?

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

Post the results.

0

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 ??

Edited by Marcusflint

1
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.

Edited by WaltP

0

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.

0

@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 ??

0
    #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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.