In the C and C++ programming languages, the comma operator (represented by the token ,) is a binary operator that evaluates its first operand and discards the result, and then evaluates the second operand and returns this value (and type). There is a sequence point between these evaluations.
The use of the comma token as an is distinct from its use in and definitions, variable declarations, Enumerated type declarations, and similar constructs, where it acts as a .
The comma operator has the lowest precedence of any C operator, and acts as a sequence point. In a combination of commas and semicolons, semicolons have lower precedence than commas, as semicolons separate statements but commas occur within statements, which accords with their use as ordinary punctuation: a, b; c, d is grouped as (a, b); (c, d) because these are two separate statements.
As of C++20, the comma operator has been deprecation in subscripting expressions, to reduce confusion, and open up the future possibility of repurposing the syntax for multidimensional array indexing. In C++23, the ability to overload operator[] with multiple arguments was added making unparenthesised comma expressions unusable in subscripts. The comma operator is still usable and not deprecated in this context if the comma expression is surrounded by parentheses (as in a[(b,c)]).
| During initialization the comma acts as a separator, but in the second line it is an operator. The parentheses are required. Without them the second line would be equivalent to , which results in being defined twice, which is illegal. | |
| since is evaluated before is evaluated and assigned to . | |
| The comma is an operator, not a separator, since is a keyword and does not act as a function. |
char *first;
for (first = s, s += len; s >= first; --s) {
putchar(*s);
}
}
Note that declaring a variable in the initializer (e.g.,
An alternative solution to this problem in other languages is parallel assignment, which allows multiple assignments to occur within a single statement, and also uses a comma, though with different syntax and semantics. This is used in Go in its analogous for loop. Effective Go: for, "Finally, Go has no comma operator and ++ and -- are statements not expressions. Thus if you want to run multiple variables in a for you should use parallel assignment (although that precludes ++ and --)."
One common use is to provide custom error messages in failed assertions. This is done by passing a parenthesized expression list to the assert macro, where the first expression is an error string and the second expression is the condition being asserted. The assert macro outputs its argument verbatim on an assertion failure. The following is an example:
assert(("i is too big!", i <= 4));
printf("i = %i\n", i);
}
// statements involving x and y
}
return (errno = EINVAL, -1);
}
y = 2, z = 3;
This allows for DSL-style APIs, like the following:
JavaScript, Perl and D utilize the comma operator in the same way C/C++ does.
Some other languages with C-like syntax, such as Java and C#, do not have this feature, though C# supports a comma-separated list of expressions in the initializer and iterator of a statement, thus enabling an equivalent of this in this specific context.
|
|