What’s the comma for?

Asked

Viewed 450 times

9

  • If it were in Python, it could be said that you are returning a Tuple.

1 answer

9


This holds 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 can form a list, which is still a larger expression. In fact the operator is binary. If you have other commas the first operand (left) is the result of the previous expression with two comma-separated expressions, which will always be the result of the last one, no matter what happens.

An important detail is that in a list the result of this larger expression will always be the resulting value of the last expression in the list (always the most to the right). Even each expression can even use results from previous expressions if they are stored in variables. Of course this occurs where a result is expected.

Note that cannot be used statements, where only the ; can separate them, and they do not form a list. Some expressions can be used as statements and it may seem like they’re just statements, but it is worth the fact that they are expressions.

You can use a list of items separated by , anywhere that expects 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 changes nothing, 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, which you should avoid, but it works.

A common example is in a for. Few people know that they can have multiple expressions in each part of it. A for wait three statements, one that is normally initialization of variables, another that is the completion condition of the loop and the third is the one that must 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 as well, but in this case it would make more sense to use both. If you used the operator , only the last boolean expression would be considered to determine whether or not the loop would end.

An example with condition:

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

Only the y > z will determine if you enter the if, but the previous expressions are fundamental to get at it. It’s ugly, but it works.

That’s why trying to return multiple values does not work, it returns only the last:

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

I put in the Github for future reference.

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 from 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 doesn’t have to do anything special with the last expression.

Can we say the same for a literal of array.

Interestingly not for the list of parameters that uses the comma to separate statements, then it should be separated by ;, maybe it is a failure of the syntax adopted by C.

Various creative constructions can be made with this operator.

You just have to watch out for unspecified behaviour and not execute in the expected order.

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

Wikipedia.

  • I didn’t understand : "Interestingly it doesn’t apply to the list of parameters, perhaps even for a syntax failure, after all the parameters are statements and not expressions, so it should be separated by ;" What does not apply to the list of parameters ?

  • The use of the comma as an expression evaluation operator. The comma there is used as if it were a ; and therefore not the operator.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.