in Programming in C edited by
2,268 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

0 votes
0 votes
answer is:

           A

 because first value of is 55

after increasing 1 its value finally will be 56

and first value of y=57+36=97

1 comment

Option d is the correct answer.

If u think like this x=y++ + x++ is nothing but x=y+x (35+20=55) and after y++(36) x++(56).

Y=++y + ++x is nothing but ++y(37) ++x(57) then y=y+x (37+57=94).
1
1
0 votes
0 votes
Intially x=20 and y=35

for the expression x=y++ + x++;

++ has percedence over + and is right associative so

x=20+35

x=55

since y is post incremented y=36 after statment excution is completed

for the expression y=++y+ ++x

both x and y are pre-incremented so y=37+56

so finally x=55 and y=93
0 votes
0 votes

answer - Undefiened behaviour

AT certain specified points in the execution sequence called sequence points, all side effects of previous evaluations are guaranteed to be complete. A sequence point defines any point in a computer program’s execution at which it is guaranteed that all side effects of previous evaluations will have been performed, and no side effects from subsequent evaluations have yet been performed. Following are the sequence points listed in the C standard:

— The end of the first operand of the following operators:
a) logical AND &&
b) logical OR ||
c) conditional ?
d) comma ,

0 votes
0 votes

Some types of program in C/C++ exhibits “undefined behavior”. This question is one of them

The compilers (implementing C/C++ standard) are free to do anything as these are undefined by the C and C++ standards.

Here we are modifying  variable  multiple times before a defined  sequence point which is responsible for “undefined behavior”. The output may be different with different compilers and different machines. 

So its output is undefined.

References: 

  1. https://www.geeksforgeeks.org/undefined-behavior-c-cpp/
  2. https://www.geeksforgeeks.org/sequence-points-in-c-set-1/
edited by

Related questions