in Programming in C edited by
2,274 views
1 vote
1 vote

Find the output of the following $“C”$ code :

Main()
{ int x=20, y=35;
      x=y++ + x++;
      y=++y + ++x;
    printf (“%d, %d\n”, x, y);
}    
   
  1. $55, 93$
  2. $53, 97$
  3. $56, 95$
  4. $57, 94$
in Programming in C edited by
2.3k views

8 Answers

1 vote
1 vote
Correct answer is x=56 and y=93 which is not given in option.
1 vote
1 vote

correct Answer is option D

 x=y++ + x++; ----------- statement1

first calculation will be done then incrementation will happen so

x= 20+35

then due to post-increment that is  x++ and y++, the value of x will be 56 and y will be 36

 y=++y + ++x; -----------statement 2

now due to pre-increment that is ++x and ++y, the value of x will be 57 and y will be 37

y=57+37

then after executing the statement 2  value of y will become 94

so x is 57 and y is 94

 

1 vote
1 vote
The Answer is Option D

Explanation:

As x = 20, y = 35;

So x = y++ + x++; // as the expression of y and x are post increment so first their will be addition of y + x and then it will do the work of increment of x and y var.

x = 56  and y = 36

Now for y = ++y + ++x; // here the incrementing is done first then addition of x and y expression

y = 94

Therefore, x = 57, y = 94
0 votes
0 votes
Initial value of x and y are 20, 35 respectively.

Now, in next expression, x and y both are getting post incremented and assigned to x.

So, as you might be knowing that whenever there is an post increment it will first get assigned and then it will get incremented.

So, as it's a '+' operator and it's left associative.

x++ will be first assigned 20 and y++ will be 35. And both will get added and assigned to x. So, now x will be 55 and y will be 36. Now, as y was post incremented it will increment after assignment same x also will get incremented but as at last 55 will get assigned to x.

So, after 1st expression x will be 55, y will be 36.

2nd expression is having addition of 2 pre increment. ++x will be 56 and ++y will be 37 and addition of both will be 93.

So, final answer will have x=56, y=93.

Correct option is not given. For confirmation I have also ran this program on online C compiler and it's also giving me same answer.
by

2 Comments

What's a sequence point in C? You can google it and undefined behaviour in C.
0
0
I've not heard about sequence point in C. Will definitely check if out. Thanks.
0
0

Related questions