I don't understand the output of this code [duplicate] - c

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 9 years ago.
I'm currently learning C, and my teacher gave us some homework. We had to identify the output of some code. I don't understand how y=4.
The code is as follows
int main() {
int w = 3, y = 3;
printf("w = %i\n", --w + w--);
printf("y = %i\n\n", y = w + y++);
return 0;
}

The behaviour is undefined since you modify the value of a variable twice between two sequence points.
You should read more about sequence points http://en.wikipedia.org/wiki/Sequence_point .This is what the Standard says about it:
At certain specified points in the execution sequence called sequence points, all side effects of previous evaluations shall be complete and no side effects of subsequent evaluations shall have taken place.

Let me preface this with an important public service announcement:
When your compiler gives you a warning, any warning, you would do well to pay attention to it. The code example in your question gives rise to Undefined Behavior - in other words, the C standard does not guarantee that every compiler will give the same results, given your code. That is Always a Bad Thing.
With that out of the way, here is an explanation of what is going on.
I modified your code slightly to look at the intermediate value of w:
#include <stdio.h>
int main(void) {
int w = 3, y = 3;
printf("w = %i\n", --w + w--);
printf("w is actually %d\n", w);
printf("y = %i\n\n", y = w + y++);
return 0;
}
My compiler complains about this code with the following warnings:
order.c:6:24: warning: multiple unsequenced modifications to 'w' [-Wunsequenced]
printf("w = %i\n", --w + w--);
^ ~~
order.c:8:35: warning: multiple unsequenced modifications to 'y' [-Wunsequenced]
printf("y = %i\n\n", y = w + y++);
~ ^
2 warnings generated.
And the output is
w = 4
w is actually 1
y = 4
What is happening? Since compilers parse the statement left-to-right, the statement
--w + w--
seems to result in the following steps (for my compiler):
--w : decrement w, it is now 2
w-- : use the value of '2', but decrement w when you are done
The result is that the sum is 2+2 and the first line prints 4, but after the sum is evaluated w is decremented to 1. However you cannot rely on this behavior - which is why the compiler threw a warning. As Eric Postpischil pointed out there may be situations where the order of execution might be different. "Don't do it" is the bottom line - you are courting Undefined Behavior.
Note that the fact that your program printed w equals 4 doesn't mean that this was true at any time. As you can see, when you actually print out w it is 1.
Assuming the compiler executed these statements in the above order, we go to the next line:
y = w + y++
We start with w = 1 and y = 3. The sum of these two is 4, and that is the value of the expression
y = w + y++
which is therefore what is printed (4).
As a side effect, y is incremented. As it happens, either way the value of y is 4 at the end. If you change the code so you start with w = 5, you end up with y = 6. In other words, the effect of y++ got overridden by the y = assignment.

Undefined Behavior
There is undefined behavior (as described in Why are these constructs (using ++) undefined behavior?) in this code in (at least) these two parts:
--w + w--
y = w + y++
This means that the results you're getting aren't portable or guaranteed.
Lying Code
On top of the undefined behavior, the output actually lies to you. printf("w = %i\n", --w + w--); looks like it's printing the value of w (in that it prints w = …), but it's not printing the value of w, it's printing the value of --w + w-- (which is undefined, so it's actually printing the value of whatever the compiler compiled --w + w-- to).
So what the value of w?
The code your compiler has generated for --w + w-- has the side effect of making w's value be 1, since it is originally 3, and both --w and w-- decrement w. However, the value of --w + w-- (in the code that your compiler generated is, based on the output that you're getting, 4. What could cause this? Well, --w could be evaluated first, which decrements w from 3 to 2, and returns the new value of w, which is 2. Then w-- could be evaluated, which decrements w from 2 to 1, but returns the original value of w, which is 2. The sum of 2 and 2, of course, is 4.
So, the value of --w + w-- is 4, and the new value of w is 1.
And what happens with y?
w's new value is 1, so when you execute (recalling that the value of y is 3):
printf("y = %i\n\n", y = w + y++);
The side effect of y++ is that y is incremented (but it's undefined whether this happens before or after the assignment), but the value of the expression is the previous value of y, which in this case is 3. Therefore, you're adding w, whose value is 1, and and the value of y++, which is 3. You're then assigning 4 to y, and the value of that assignment is 4, so you get 4 as the output. There's still behavior that might be undefined in y = w + y++ because it's not clear when the increment of y should be happening: before or after the assignment?

Your teacher is assuming a specific order of evaluation which is not guaranteed by the language.
Assuming strict left-to-right evaluation, and assuming that side effects are applied immediately, then given the starting values for w and y, the following things happen:
--w evaluates to w-1 (2), and as a side effect decrements w; w is now 2
w-- evaluates to w (2), and as a side effect decrements w; w is now 1
As a result of 1 and 2, --w + w-- evaluates to 4
y++ evaluates to y, and as a side effect increments y; y is now 4
As a result of 4, w + y++ evaluates to 1 + 3, or 4
As a result of 5, y = w + y++ assigns 4 to y.
BUT...
As I said above, this behavior is not guaranteed by the language; except in a few specific cases, the order of evaluation within an expression is left unspecified, so y being 4 is only one of many possible results, all of which are considered equally correct as far as the langauge is concerned.
The behavior of any expression that takes on the following forms (both ++ and --, both prefix and postfix):
x++ + x++
a[i++] = i
y = y++
is undefined. Similarly, the behavior of statments like
foo(y++, y++);
is also undefined.

Related

When does the increment operation happen exactly for x++?

I know that x++ refers to post increment. It first uses the original value and then resorts to the incremented value.
But when does that actually happen?
Does it happen when the next immediate line in code is executed?
Does it happen when original value is returned once?
It happens between sequence points.
Other than that, it's unspecified when it happens.
Imagine this
x = y = z = 42
; // sequence point A
n = x++ + y++ + z++
; // sequence point B
At seq. point A x, y, and z are all 42; at sequence point B they are all 43. For all you care, the compiler could emit code to update all 3 variables at the same time.
// pseudo code
n = 126;
[vector increment][x,y,z];
//n = 126;

In which order are the increment expressions on the right evaluated in the assignment statement? Is it undefined?

I recently learnt about undefined behaviour in C, but this particular code was used in a site as an example for 'comma as an operator', and while I understand how y = x++ in line 2, I dont understand in what order the sub expressions in line 2 are evaluated. I think it is undefined behaviour, but I'm not sure,because the site didn't mention anything as such.
int main()
{
int x = 10, y;
y = (x++, printf("x = %d\n", x), ++x, printf("x = %d\n", x), x++);
printf("y = %d\n", y);
printf("x = %d\n", x);
return 0;
}
Output:
x = 11
x = 12
y = 12
x = 13
It is not undefined behaviour.
You first increase x to 11, the print it, then increase it to 12 and print it, then increase it after evaluation, so x will be 13 and the whole expression will evaluate to 12.
This is caused due to the comma operator in C being a sequence point, which means it is guaranteed all side effects of previous evaluations will have been performed, and no side effect from subsequent evaluations have yet been performed.

C, pre and post increment, different answers in different programs [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 6 years ago.
testing some code when studying C language,
#include <stdio.h>
#include <math.h>
#define hypotenusa(x, y) sqrt((x) * (x) + (y) * (y))
int main(void) {
int a, x;
x = 2;
a = hypotenusa(++x, ++x);
printf("%d\n", a);
}
And I am getting the answer
6 in one program(dosbox gcc compiler)
7 in codelight gcc compiler and
8 on codeChef online compiler
can anyone explain this behaviour?
my logic says it should be 6 (sqrt(42)) but...
It's undefined behaviour.
After the macro replacement
a = hypotenusa(++x, ++x);
becomes:
a = sqrt((++x) * (++x) + (++x) * (++x));
As you can see x is modified multiple times without any intervening sequence point(s). See What Every C Programmer Should Know About Undefined Behavior.
hypotenusa(++x, ++x) is undefined behavior.
It is up to the compiler which of the parameters gets incremented (and pushed) first - after the macro expansion, there are a total of four instances, and the sequence is not defined.
You should never increment the same variable multiple times in the same statement, to avoid this kind of issues. Using a variable twice in a macro can hide this error and make it really nasty.
The behavior of your macro is undefined, meaning any result is possible.
Chapter and verse
6.5 Expressions
...
2 If a side effect on a scalar object is unsequenced relative to either a different side effect
on the same scalar object or a value computation using the value of the same scalar
object,
the behavior is undefined. If there are multiple allowable orderings of the
subexpressions
of an expression, the behavior is undefined if such an unsequenced side
effect occurs in any
of the orderings.84)
84) This paragraph renders undefined statement expressions such as i = ++i + 1;
a[i++] = i;
while allowing i = i + 1;
a[i] = i;
Basically, if you modify the value of an object more than once in an expression, or both modify an object and use its value in a computation in an expression, the result will not be well-defined unless there is a sequence point in between those operations. With a few exceptions, C does not force left-to-right evaluation of expressions or function parameter evaluations, nor does it mandate that the side effect of ++ or -- be applied immediately after evaluation. Thus, the result of an expression like x++ * x++ will vary from platform to platform, program to program, or even potentially from run to run (although I've never seen that in practice).
For example, given the expression y = x++ * x++, the following evaluation sequence is possible:
t0 <- x // right hand x++, t0 == 2
t1 <- x // left hand x++, t1 == 2
y <- t0 * t1 // y = 2 * 2 == 4
x <- x + 1 // x == 3
x <- x + 1 // x == 4
which doesn't give you the result you expect if you assume left-to-right evaluation.

post increment and pre increment operator [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 6 years ago.
Please explain me the outcome of this code.
//code a
when I run this code on my laptop, value of y is 4. And I think, logically value of y should be 5 because by doing x++ it should return 2 without incrementing as it is post increment and then when we add x which now contains an incremented value ie 3. So 2+3 is 5, according to me. But according to my turbo c++ 3.0 answer is 4.
#include <stdio.h>
void main() {
int x = 2,y;
**int y = x++ + x;** // ans 4
printf("y is :%d", y);
}
// code B
When I run this code, the answer is 6. Here turbo c++ 3.0 in ++x is picking up an incremented value of x++, which is the opposite of above mention code. Logically here answer is correct but in the above code it's not.
#include <stdio.h>
void main() {
int x = 2,y;
**int y = x++ + ++x;** //and 6
printf("y is :%d", y);
}
Firstly, assignment operator that is = works from right to left, which means if you write x = 2 + 4 + 1; your compiler starts reading it from rightmost digit that is 1 then it add 4 to it and so on and then it assigns that value to x.
So, in your case statement y = x++ + x; compiler starts seeing it from right that is it first sees x i.e. 2 and then sees x++ i.e. also 2 as it is post increment operator finally it adds them and assigns y as 2 + 2 that is 4.
In the second case, that is y = x++ + ++x;, compiler first looks at ++x and as it is pre increment operator it increases x with one, i.e. x is now 3. After that x++ is seen and as stated above because it is post operator it would be treated as x in this operation and that value is 3 (remember we incremented x by one earlier) and hence, compiler assigns 3 + 3 i.e. 6 to y.
First program where the result is 4
#include <stdio.h>
void main() {
int x = 2,y;
**int y = x++ + x; // x=2, so y=2+2=4
printf("y is :%d", y); // after post increment x=3
}
Since a variable only gets it post incremented value only after the execution of the statement is completed so y=2+2
Second program where the result is 6.
Associativity of ++ operator is "right to left"
void main() {
int x = 2,y;
**int y = x++ + ++x; //Since associativity is left to right so y=3+3=6
printf("y is :%d", y);// after the post increment x=4
}
Here the pre-increment is performed first since because of the associativity rule so y=3+3

How to use post/pre increment operators in addition problems?

main(){
int x = 256, y = 4;
printf("%d\n\n", x++ + ++y); //output = 261
printf("%d\n\n", x); // output = 257
printf("%d", y); // output = 5
}
Is the final answer 261, because 256 -> 257 (post operator) and 5 -> 5 (pre operator) cause 256 + 5 = 261?
Given:
int x = 256, y = 4;
printf("%d\n\n", x++ + ++y);
In short: The x++ returns the value 256 and then increments x to 257. The ++y increments y to 5 and returns the value 5. The addition, therefore, adds 256 and 5 yielding 261.
Long windedly: The x++ evaluates to the current value of x which is 256 and schedules an increment of x to 257. Similarly ++y schedules the increment of y to 5 and evaluates to the incremented value 5. The addition, therefore, adds 256 and 5 yielding 261. The order in which the terms involving x and y are evaluated is not defined, but both have to be evaluated before the addition (though the increments may not be complete when the addition is evaluated). Because there is a 'sequence point' when the arguments (and the expression denoting the function) have been evaluated but before the function is called, the increments must be complete when printf() is called.
The next two statements print x and y as 257 and 5.
Note that those two printf() operations could be combined into one. Neither could be combined with the first without invoking undefined behaviour. (See Multiple increments and undefined behaviour for more information on this topic.)
So, allowing for the fact that I would not express it quite the way you wrote it, you seem to have the correct explanation.
Also, Standard C has required a return type on all functions for over 15 years now (since C99 was standardized). You should write:
int main(void)
for a main() function that takes no arguments. (See What should main() return in C and C++? for the full details.)
Note that this question only invokes fully defined behaviour (at least, in the printf() statements). It is not asking about multiple increments on a single variable between sequence points.
It will show UNSPECIFIED behavior
In your case, we can't tell whether x++ will be evaluated first or ++y will be evaluated first. It is compiler dependent.
So don't use expressions involving a combination of post-increment or pre-increment operators in C or C++.
For more information refer to the link:
https://www.quora.com/What-does-an-expression-involving-multiple-post-pre-decrement-increment-operators-evaluate-to-in-C-and-C++#

Resources