3

I have this question for a practice test in C that I don't understand. Show the output of the following program

x = y = 3;
y = x++, x++;
printf("x = %d, y = %d\n", x, y);

Answer: 1 x = 5, y = 3

I dont understand what

x++, x++ 

Does x++ mean to implement the value of x into y then add one to it, but why is there a comma ? Would it just first add the value of x in y, and do x=x+1 twice?

I tried putting it in a compiler, found some struggles.

dbush
  • 205,898
  • 23
  • 218
  • 273
  • Does this compile? gcc on my system balked on the second 'x++'. – John Carter Nov 03 '22 at 15:42
  • 2
    The expression `l, r` is [the *comma* operator](https://en.cppreference.com/w/c/language/operator_other#Comma_operator) which evaluates `l` and throws away the result, then evaluates `r` and returns its result. The increment operator is part of the evaluation of `x++` which means `x` is increased twice. – Some programmer dude Nov 03 '22 at 15:45
  • More on the comma operator https://stackoverflow.com/q/52550/ – General Grievance Nov 03 '22 at 15:46
  • 2
    It should go without saying that you should never actually write code like this. All it's good for is fodder for test questions. – Robert Harvey Nov 03 '22 at 16:06

2 Answers2

4

In this statement:

y = x++, x++;

It contains both the assignment operator and the comma operator as well as the postfix increment operator. Of these the postfix increment operator has the highest precedence followed by the assignment operator, then the comma operator. So the expression parses as this:

(y = (x++)), (x++);

The comma operator first evaluates its left operand for any side effects and discards the value of that operand. There is a sequence point here before it then evaluates its right operand, which means evaluating left side and all side effect are guaranteed to occur before those on the right side.

So let's first look at the left operand of the comma operator:

y = (x++)

The subexpression x++ evaluates to the current value of x and increments x as a side effect. At this point x has the value 3, so y gets assigned the value 3 and x is incremented to 4.

Now for the right side:

x++

Since x currently has the value 4, it is incremented to now contain the value 5.

So at the end x is 5 and y is 3.

dbush
  • 205,898
  • 23
  • 218
  • 273
  • Thank you, this makes a lot more sense. I was feeling there was something like that but couldn't get to a proper explanation. Thanks a lot once again. – Sebastien Fnt Nov 03 '22 at 17:02
1

There is a wikipedia page explaining the comma operator in C and C++: https://en.wikipedia.org/wiki/Comma_operator

It basically evaluates the two expressions left-to-right and returns the value of the second one.

Here you can see the precedence of operators in c++: https://en.cppreference.com/w/cpp/language/operator_precedence

The comma operator has the lowest precedence.

In your example. The first line sets both x and y to 3. The second line increments x twice: x++, x++. However, since the assignment operator has higher precedence than the comma operator, y gets the value returned by the first increment. The statement is evaluated as (y = x++), x++. The first x++ is executed and x gets the value of 4 and returns 3. This value is assigned to y: y = x++. Then, the last x++ is evaluated and x gets the value 5.

EnMag
  • 73
  • 5