The Gateway to Computer Science Excellence
+1 vote
404 views

Please explain the solution.The output is 11 7 0

#define MAX(x,y) (x) > (y) ? (x) : (y)
main()
{
int i = 10, j = 5, k = 0;
k == MAX(i++, ++j);
printf("%d %d %d", i,j,k);
}

What will the values of i , j and k?
 

in Programming by (19 points) | 404 views

1 Answer

+6 votes
Best answer

Given i = 10, j = 5, k = 0

Lets see how line (5) k == MAX(i++, ++j); works..

  • Macro will replace MAX(i++, ++j) with (i++)>(++j):(i++):(++j);
  • line (5) becomes k==(i++)>(++j):(i++):(++j);

The order of evaluation will be [[k==(i++)]>(++j)]:(i++):(++j);

  • Put the values, the condition becomes
  • ((0==10)>6)     ->    0>6   --> Condition returns false!
  • So false part of ternary operator works  (++j) = 7

printf("%d %d %d", i,j,k); prints 11, 7 ,0

by Boss (33k points)
selected by
Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,737 questions
57,405 answers
198,627 comments
105,465 users