Some confusion about how commas work in C / C ++

I used SO for a while as a link, but never asked a question before. I'm currently in C ++ class at college, and I also read Bjarne Stroutstrup's Programming: Principles and Practice for my own benefit, as I saw the answer to a question that really recommended it.

We are closing statements at the moment in my class, and I just can’t imagine how the statement operator works in the statement. One example is the sample question for the online part of the class, which I continue to make mistakes even if I write a program in C and use GDB to get the result. The question arises:

Assuming x == 16 before the next expression, what is the meaning of the next expression (not necessarily the value of x)?

x ++, ++ x, x + = x

I am not interested in the correct answer, how to get the correct answer. I read a couple of answers to similar questions, such as here , but it seems to me that I am lacking in how it is applicable when there is actually no binding operator, This is the same as saying

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

or

 int y = x++, ++x, x+=x; 

or not? Can someone explain how the comma operator works, in particular with respect to statements without assignments?

+5
source share
2 answers

The comma operator is simple - so easy that it is complicated. It has the lowest priority for all operators; its priority is even lower than assignment operators. Note that function arguments are not separated by a comma operator.

The comma operator calculates the left operand, generates a sequence point and discards the result, and then evaluates the right operand.

In the context:

 x++, ++x, x += x; 

is equivalent to:

 x++; ++x; x += x; 

except that the total value is the result of x += x; .

Given that x starts at 16, it increases to 17, then 18, and then doubles to 36. Thus, the total value is 36.

Note that due to points in the sequence, it does not fulfill the rules foul not to increment the same variable more than once between points in the sequence.

The only reason for using the comma operator is that there are contexts in which you cannot use separate instructions, but you can use commas. For instance:

 for (i = 0, j = n; i < j; ++i, --j) 

Instead of these commas, you cannot use semicolons.


There are two examples in this question:

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

The first is legal (albeit overly distorted) and initializes y to 36 (and sets x to 36).

The second is not legal and will not compile; commas are not comma operators and must be separated by different declarators, but ++x and x += x are not declarators. However, if it was changed to:

 y = x++, ++x, x+=x; 

then it would be legal. First member:

 y = x++ 

which assigns a value from 16 to y and increases x to 17. The second term increases x to 18; the third term changes x to 36.

+8
source

The comma operator evaluates the first operand, discards it, and then evaluates with the second, with a sequence point between the evaluations.

In your case, this means that you evaluate this sequence of expressions

 x++ // evaluates to 16, increments x to 17 ++x // increments x to 18, evaluates to 18 x+=x // increments x by x, ie by 18, evaluates to 36. 

And the full expression is evaluated by the third subexpression. The first expression evaluates to 16, but increases from x to 17. The second increases x to 18 and evaluates this number. The third increases x by its value of 18 and evaluates to x , that is, to 36.

+4
source

Source: https://habr.com/ru/post/1212942/


All Articles