What is the comma for?

8

The comma is what? Just a construction of language? An operator? Why does it exist?

This question is based on what you saw in Returning or extracting more than one value from a function? .

return base2, base3, base4
    
asked by anonymous 06.01.2017 / 12:08

1 answer

8

This is true for most languages that were based on the same C syntax principles.

The comma is an operator that serves to separate and evaluate expressions and can form a list that does not stop being a greater expression. Actually the operator is binary. If you have other commas, the first operand (left) is the result of the previous expression with two expressions separated by commas, which will always be the result of the last, no matter what happens.

An important detail is that in a list the result of this greater expression will always be the value resulting from the last expression in the list, always the rightmost one). Even each expression can even use results from the previous expressions if they are stored in variables. Of course this is where you expect a result.

Note that you can not use statements , where only ; can separate them, and they do not make a list. Some expressions can be used as statements and it may appear that they are only statements , but worth the fact that they are expressions.

You can use a list with items separated by , anywhere you expect an expression. So this works:

int x = 1, 2, 3, 4; //o valor de x será 4, o resto seria apenas processado, mas não usado 

In practice nothing changes, but as the operator is binary this is the same as:

int x = ((1, 2), 3), 4; //neste exemplo específico os números anteriores foram ignorados

Of course this is of little use. It gets interesting when there are more complex expressions that generate side effects.

A common example is in for . Few people know that they can have multiple expressions in every part of it. A for expects three statements , one that is usually initialization of variables, another that is the end condition of the loop, and the third is what should be executed at the end of each loop iteration. Example:

for (int x = 1, y = 1; x < 10 && y < 20; x++, y *= 2)

The condition could have the comma too, but in this case it made more sense to use both. If you used the operator only the last boolean expression would be considered to determine whether the loop would terminate.

An example with condition:

if ((x = calcula(1, 2)), (y = x - 10), y > z)

Only y > z will determine whether to enter if , but previous expressions are key to getting there.

Why trying to return multiple values does not work, it returns only the last:

return 1, x, abs(x + 5), x * 2, x > 5;

will return 0 or 1, because only the Boolean expression will be returned, the rest will be executed but discarded.

You may be thinking that a call to a function where you pass arguments is different. It is not. It is exactly this operator that is used there to separate the expressions that form the arguments. Of course he does not have to do anything special with the last expression.

We can say the same for an array literal.

Interestingly, this does not apply to the list of parameters that uses the comma to separate statements , then it should be separated by ; , maybe syntax failure.

Several creative constructions can be done with this operator.

Just be careful about unspecified behavior and do not execute in the expected order.

In C ++ the operator can be overloaded and change this semantics, although it should not.

Wikipedia .

    
06.01.2017 / 12:08