Answered # code on #define pls post how we get the output

Ancient Dragon 5,243 lionaneesh -4 Write a C program that should create a 10 element array of random integers (0 to 9). The program should total all of the numbers in the odd positions of the array and compare them with the total of the numbers in the even positions of the array and indicate ...

0

The result is undefined behavior because of the ++ operator. max() evaluates both variables a and b twice, so the ++ operator gets executed twice. Exactly what the final result will be is compiler implementation dependent. Your compiler may produce one value while another compiler may produce something else.

*Edited 6 Years Ago by Ancient Dragon*: n/a

0

```
#include<stdio.h>
#define prn(a) printf("%d",a)
#define print(a,b,c) prn(a), prn(b), prn(c)
#define max(a,b) (a<b)? b:a
int main()
{
int x=1, y=2;
printf("First Print Call :");
print(max(x++,y),x,y); // here x is incremented after the max call returns then value of x becomes 2(remember we used x++ not ++x so the max call wil recieve 1 as x's(a) value and 2 as y's value (b) so the output becomes prn(y),prn(x),prn(y) ....Therefore the output is 222
printf("\n2nd print call : ");
prn(max(x++,y));// Now here the value of x=2,y = 2 call = max(x++,y) here a(x++) is not < b(y) therefore it returns a(x++ = x+1) the #define max statement increments x once in the call also the it returns x then x is incremented and we get 3 as output
prn(x);
prn(y);
printf("\n");
}
```

Your code was not so impressive so i made some changes for better understanding..

a bit tricky part is here

`prn(max(x++,y));`

What max call does is that:-

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

So the max call here gets input 2 integers...x++ and y

as a is not less that b the max returns a which is x++

Here x increments one time...Its value becomes 3...

Now after the max call the x++ in the parenthesis increments now the value of x becomes 4...So the output 4...y's value is 2 so output=2

I hope you find it little bit easy now... Mess with your code ...Try with different variables and you'll get a better understanding of this....

Use code tags...or someone will vote you down...

This question has already been answered. Start a new discussion instead.

Recommended Articles

Hi. so this is actually a continuation from another question of mineHere but i was advised to start a new thread as the original question was already answered.

This is the result of previous question answered :

code for the listbox - datagridview interaction

At the top of the code ...

the function that I created to find the ...