Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Some confusion with how commas work in C/C++ [duplicate]

I've used SO for a while as a reference, but never asked a question before. I'm currently in a college C++ class and also reading Programming: Principles and Practice by Bjarne Stroutstrup just for my own benifit, as I saw an answer to a question here that really recommended it.

We're covering operators at the moment in my class, and I just can't seem to wrap my head around how the comma operator works in a statement. One example is a sample question for the online portion of the class that I keep getting wrong, even if I write a C program and use GDB to get the result. The question is:

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

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

I'm not interested in the correct answer so much as how to get the correct answer. I've read through a couple answers to similar questions, such as this one here, but it seems like I'm missing how this applies when there is actually no assigment operator. Is this the same as saying

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

or

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

or neither? Could someone please explain how the comma operator works, specifically in relation to a statement without an assignment?

like image 731
Joseph Morgan Avatar asked Feb 09 '15 08:02

Joseph Morgan


People also ask

How does comma operator work in C?

The comma operator in c comes with the lowest precedence in the C language. The comma operator is basically a binary operator that initially operates the first available operand, discards the obtained result from it, evaluates the operands present after this, and then returns the result/value accordingly.

How does comma operator work?

The comma operator ( , ) evaluates each of its operands (from left to right) and returns the value of the last operand. This lets you create a compound expression in which multiple expressions are evaluated, with the compound expression's final value being the value of the rightmost of its member expressions.

Can we use comma in while loop in C?

Sure, with proper understanding of, separately, the while loop syntax and comma operator syntax and functioning one would be able to understand this problem without external help.

Is comma a special character in C?

1) Comma as an operator: The comma operator (represented by the token, ) is a binary operator that evaluates its first operand and discards the result, it then evaluates the second operand and returns this value (and type). The comma operator has the lowest precedence of any C operator, and acts as a sequence point.


1 Answers

The comma operator is easy — so easy it is hard. It has the lowest priority of all operators; its priority is even lower than the assignment operators. Note that arguments to functions are not separated by the comma operator.

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

In context:

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

is equivalent to:

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

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

Given that x starts at 16, it is incremented to 17, then 18, then doubled to 36. The overall value is therefore 36.

Note that because of the sequence points, it does not run foul of the rules about not incrementing the same variable more than once between sequence points.

The only reason for using a comma operator really is that there are contexts where you can't use separate statements but you can use comma operators. For example:

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

You can't use semicolons in place of those commas.


In the question, there are two samples:

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

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

The first is legitimate (though unnecessarily contorted), and initializes y to 36 (and sets x to 36).

The second is not legitimate and won't compile; the commas are not comma operators and should be separating distinct declarators, but the ++x and x += x are not declarators. However, if it was changed to:

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

then it would be legitimate. The first term is:

y = x++

which assigns 16 to y and increments x to 17. The second term increments x to 18; the third term changes x to 36.

like image 80
Jonathan Leffler Avatar answered Sep 23 '22 12:09

Jonathan Leffler