Assigning multiple integers separated by comma to int in C - Why does this work? What for?

I saw it on the exam, and when I tried it, I was surprised. I tried it online and it works too. So I think this is C.

Why does it work? What is the use case for destination syntax?

#include <stdio.h> #include <stdlib.h> int main(int argc, char** argv) { int i = (1,2,3,4,5); printf("%d", i); return 0; } 
+7
c variable-assignment
source share
5 answers

These are not "plural integers," but a comma. The entire bracketed part is one expression with each subexpression (separated by commas), which is evaluated strictly from left to right. The results of all but the highest subexpression are ignored. The result of the whole expression is the result of the last (rightmost) expression. Here it is an integer value of 5 .

Please note that this operator is mainly used where only one expression is allowed to add additional side effects. For example. in the form of a loop:

 int cnt = 0; for ( const char *cp = "Hello" ; *cp != '\0' ; cp++, cnt++ ) ; 

This counts the number of characters in the C string, increasing the pointer and cnt after each iteration. Here the results are ignored.

So this is in no way related to tuples or the like in Python. In fact, there are no cases when the use of this operator is inevitable, and it should be used with caution - and some coding standards prohibit its use.

+9
source share

That the comma operator is working. It evaluates the expression on the left side, creates a sequence point, discards the value of the expression, then evaluates the expression on the right side and returns it as a value. When there are several expressions, as in the example, each of them is evaluated in turn and only the last is saved. With a sample, the compiler makes an assessment because each value is known at compile time. Note that the argument list of the function is not used by the comma operator.

This is not a valid use case for the comma operator. As a more suitable use case, there may be some operations with side effects (such as function calls) that need to be ordered and the final value assigned:

 int i = (getchar(), getchar(), getchar()); 

This sets i third character in the standard input, or EOF if there are three characters left on the standard input that you want to read. Still not a realistic use case, but better than assigning a list of constants.

+7
source share

In addition to the other answers, you need to look at instances where , is the comma operator, not when it is a delimiter. For example, the following is not valid:

 int i = 1,2,3,4,5; 

In this case , is a separator between variable declarations. It declares i as an int and initializes it to 1, then tries to parse 2 as the name of a variable that fails.

+3
source share

This works because you use the “comma operator”, which evaluates the subexpressions on the left and right and has the value from the right expression.

So, in (1,2,3,4,5) , 1 is evaluated and the result is discarded, then 2,3,4,5 ... in which (due to the next comma) 2 is evaluated and the result is discarded, then 3,4,5 ... in which 3 is evaluated and discarded, then 4,5 ... in which 4 is evaluated and discarded, then 5 , which becomes the result of the expression.

As for when it is useful, mainly as a shortcut, when you need to evaluate a few (sub) expressions for their side effects, but they are not interested in their meanings (except, perhaps, the latter). Sometimes this is convenient in for loop expressions, for example, when adding two variables:

 for (i=0,j=1; j < len; i++,j++) { 

.. where it appears both in the initialization expression and in the loop expression.

+2
source share

Why does it work?

Because its valid C syntax. The comma in (1,2,3,4,5) is the comma operator

C11: 6.5.17 Comma operator

Syntax
one:

 assignment-expression expression , assignment-expression 

Semantics
2 The left operand of the comma operator is evaluated as an expression in the form of a void; between its evaluation and the point of the correct operand there is a sequence point. Then the right operand is evaluated; The result has its type and meaning. 114)


What is the use case for such an assignment syntax?

See example below.

3 EXAMPLE As indicated in the syntax, the comma operator (as described in this subclause) cannot be displayed in contexts where the comma is used to separate items in a list (for example, arguments to functions or initializer lists). On the other hand, it can be used in an expression in brackets or in the second expression of a conditional statement in such contexts. In function call

 f(a, (t=3, t+2), c) 

the function has three arguments, the second of which has a value of 5.

+1
source share

All Articles