in Programming in C reopened by
584 views
1 vote
1 vote
#include<stdio.h>

int main()
{
    int x, y = 7;
    x = ++y + ++y + y--;
    printf("%d\n", x);
    return 0;
}

What is the output of this code snippet ?

A. 27

B. 26

C. 25

D. Compilation error

 

in Programming in C reopened by
584 views

3 Comments

https://www.geeksforgeeks.org/sequence-points-in-c-set-1/

See here might it will help you to understand. It is a sequence point problem

0
0
A). 27
0
0
0
0

2 Answers

–2 votes
–2 votes
Answer is A. 27

4 Comments

@talha hashim

Then according to you,

if x = ++y + y-- ;

then it should be y => 8 ==> 7

and x = 8 + 8 = 16...right?

But it gives 15. Why?
0
0

please avoid discussion on Undefined Behavior in C

1
1
There are 10 kinds of people in world - one who understand undefined behaviour and one who doesn't :)
4
4
–3 votes
–3 votes
(y=7)

y--  -->y=7( y=y-1 will be perform at last)

++y -->y=y+1=7+1=8

(y=8, updated)

++y  -->y=8+1=9

(y=9, updated)

now x=y+y+y ----->(++y + ++y +y--)

x=9+9+9=27

now y=y-1 will be implemented

y=9-1=8

 

x=27,y=8

2 Comments

@balaganesh

Then according to you,

if x = ++y + y-- ;

then it should be y => 8 ==> 7

and x = 8 + 8 = 16...right?

But it gives 15. Why?
0
0
After your question i have checked with various examples, I came to know that its depends upon the compiler.
0
0

Related questions