1

some body please tell me what will be the value of x after(in c language)

x=1;

x=x--&&++x;

I think it should be 0 because x&&++x will give 1 and post decrement will make it 0.

But when I entered this on computer result was 1. Why post decrement is not working here.

I am thinking like this:

precedence of pre increment is above && so both x should be treated as 2 (Boolean value true ) so x&&++x will give 1 and the post-decrement should decrement it to 0.

This is not a duplicate question as this is not the case of undefined behavior its about how post-decrement works.

Dhruva Mehrotra
  • 123
  • 1
  • 12
  • name the language you programmed in ? – Sachin Nov 02 '15 at 09:51
  • 1
    If this is about C, then the expression yields undefined behavior and `x` the compiler is allowed to make demons fly out of your nose. And if it's about C, then this question has been asked way too many times here already. – DarkDust Nov 02 '15 at 09:53
  • 7
    See [Why are these constructs (using ++) undefined behavior?](http://stackoverflow.com/questions/949433/why-are-these-constructs-using-undefined-behavior) – DarkDust Nov 02 '15 at 09:55
  • 1
    To avoid undefined behavior, you need to write `int y = x--&&++x; x=y;` – Paul Hankin Nov 02 '15 at 10:25
  • 1
    @oo_miguel: It's a problem if your `some_expression_with_x` modifies the value of `x`. Short circuit evaluation is a red herring, here. `x = something` modifies `x`, and so does `x--`, so `x = x--` modifies the value of `x` twice within the same expression without a sequence point between the modifications. – Crowman Nov 02 '15 at 10:48
  • @PaulGriffiths thank you for your explanation, and sorry for deleting my quesiton (before I saw your answer). – oo_miguel Nov 02 '15 at 10:53

3 Answers3

9
x=x--&&++x;

This causes undefined behaviour as value of x is changed more than once between two sequence points.

Expression x-- && ++x is well defined as it has internal sequence point due to && , but when you assign it to x , it causes undefined behaviour.

Therefore ,expression exhibits undefined behaviour.

C99 §6.5: “2. Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be read only to determine the value to be stored.”

ameyCU
  • 16,489
  • 2
  • 26
  • 41
  • I have tried it many times and on many compiler result is always 1. – Dhruva Mehrotra Nov 02 '15 at 10:15
  • 1
    This would be a better answer if it explained where the double modification occurs. For there _is_ a sequence point between the left and right side of the `&&`, just not between the `++x` and the assignment to `x`. – Paul Hankin Nov 02 '15 at 10:20
  • 1
    @DhruvaMehrotra It doesn't means that correct or similar answer on compiler means it is a well defined behaviour . It could also be under the circumstances of undefined behaviour . – ameyCU Nov 02 '15 at 11:19
  • @PaulHankin I thought it would be enough but thanks I have mentioned it in answer :-) – ameyCU Nov 02 '15 at 11:35
1

While the result of (x--)&&(++x); is well defined, due to short circuit evaluation.

the result of your assignment x = (x--)&&(++x); is not.

A simpler example would be:

x = x--;

which, as Paul and Art note in the comments:

modifies the value of x twice within the same expression without a sequence point between the modifications.


EDIT: fixed my initial errornous answer, which stated that the result of the assignment is defined.

oo_miguel
  • 2,374
  • 18
  • 30
  • If x=1; and x-- return 1 how? – Mohan Nov 02 '15 at 10:24
  • haha, yes. my initial thought was the same, however then I noticed it is in fact **not undefined** :) – oo_miguel Nov 02 '15 at 10:30
  • 1
    How do you figure it's not undefined. To me it looks undefined. There's no sequence point between `++x` and `x = 1`. – Art Nov 02 '15 at 10:41
  • @Art: The expression that is assigned to `x` is the result of `&&`. It's a sequence point. So the left side of `&&` is evaluated, then the right side, then the result (`&&` always evaluates to either integer 0 or 1) is assigned to `x`. – DarkDust Nov 02 '15 at 10:46
  • 1
    @DarkDust There's a sequence point between `x--` and `++x`, but there's no sequence point between `++x` and `x = 1`. – Art Nov 02 '15 at 10:48
  • 1
    @oo_miguel `x = x++` is the textbook example of undefined behavior. See the highest upvoted answer to this question, it cites the relevant part of the standard. An object may not be modified more than once between sequence points. In this expression there's one sequence point and three modifications of the same object. Why is a different matter. It has to do with optimizations and what we're allowing compilers get away with. The rules for sequence points are loose enough to allow compilers get away with murder, but also weird enough to confuse even seasoned C programmers. – Art Nov 02 '15 at 10:58
  • @Art: thank you very much. fixed my answer now I hope. – oo_miguel Nov 02 '15 at 10:59
-2

Inspite of the fact that there are exams in my university I wasn't able to stop thinking about this question and I think I have finally found the solution.

Firstly, post decrement operator will be executed as it is in the left of &&, it will decrement the value of x to 0 but x-- will be 1(previous value of x ) as left side is 1 right side will be executed here ++x will assign value 1 to x and value of ++x will also be 1 so && operator will return value 1.Now, although there is no sequence point between pre-increment operator and assignment operator both are assigning value 1 to x so this is totally defined that value of x will be 1 after the whole code is executed.

Dhruva Mehrotra
  • 123
  • 1
  • 12
  • Please tell me if I am still wrong. – Dhruva Mehrotra Nov 02 '15 at 10:35
  • The precedence here is _left side of `&&`_ before _right side of `&&`_. – DarkDust Nov 02 '15 at 10:40
  • @DarkDust I have studied a precedence chart in c it doesn't say any thing about left side or right side of logical and – Dhruva Mehrotra Nov 03 '15 at 10:09
  • See C99 §6.5.13(4): _"Unlike the bitwise binary & operator, the && operator guarantees left-to-right evaluation; there is a sequence point after the evaluation of the first operand. If the first operand compares equal to 0, the second operand is not evaluated."_ A vital point here is that there's a sequence point: so the left side is evaluated, all side-effects are resolved and only then is the right side evaluated. – DarkDust Nov 03 '15 at 10:51
  • @DarkDust : Now see I have proved that this is not undefined behaviour. – Dhruva Mehrotra Nov 06 '15 at 09:32
  • No, you have not. The expression in your question is undefined behavior, as explained in the answer you've accepted. Do you still not understand what _undefined behavior_ is? It's an expression that the _standard_ says is invalid. Whether your compiler accepts it has nothing to do with it. Please show this question and the accepted answer to your professor/instructor and have him/her explain it to you. – DarkDust Nov 06 '15 at 09:45
  • `x--&&++x` itself is legal. `x=x--&&++x;` on the other hand is not. – DarkDust Nov 06 '15 at 09:51
  • @DarkDust My professor gave me this question to solve. And if both assignment will assign 1 to x then it doesn't matter which will happen first – Dhruva Mehrotra Nov 06 '15 at 09:53
  • Which should mean (s)he knows the answer. Go ask him/her. If not, show him/her the accepted answer and let him/her explain it to you. – DarkDust Nov 06 '15 at 09:55
  • @DarkDust look in x=1; x=x++; post increment is assigning 2 to x and assignment operator is assigning 1 and because there is no sequence point compiler gets confuse which should be perform first so it is undefined. but in my question both are assigning 1 to x so it is defined – Dhruva Mehrotra Nov 06 '15 at 10:07
  • No! There are three modifications but only one sequence point which makes it undefined behavior. It's as simple as that! There is no sequence point between the `x=` part and the `x++` part. Which should occur first? The assignment of the return value of `&&` to `x`, or the post decrement? That is not defined by the C standard. This is explained in the other two answers as well (oo_miguel's is correct even though it has low score). This has been explained here over and over again, in answers and comments, yet you don't want to believe us. Please ask your professor to explain it to you. – DarkDust Nov 06 '15 at 10:25
  • @DarkDust IT DOESN'T MATTER WHICH WILL OCCUR FIRST IF BOTH ARE DOING THE SAME THING.Both are assigning 1 to x. – Dhruva Mehrotra Nov 06 '15 at 10:31
  • @DarkDust I get that there is no sequence point so compiler is allowed to execute any one of them first but if both are assigning 1 to x then both path will lead to the same result i.e, value of x is 1. The x=1; x=x++; is example of undefined behavior because some compiler will print 1 and some will 2 but in my case every compiler in the world will print 1 – Dhruva Mehrotra Nov 06 '15 at 10:34
  • And what if `x` was initially 42? What should happen then? You can discuss as much as you want, the C99 standard says in §6.5 paragraph 2 that it's illegal. And this will be the last comment from me on this subject. I repeat: go talk to your professor about this! Really! Do it! This is a complicated expert-level issue we're talking about here and it's probably better explained in-person. – DarkDust Nov 06 '15 at 10:41
  • Allright, one more comment, because it might be insightful: I recommend to read [What Every C Programmer Should Know About Undefined Behavior](http://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html) which is very insightful stuff from people who really know about these things. – DarkDust Nov 06 '15 at 10:44