The comma operator has relatively limited use cases. Because it discards its first operand, it is generally only useful where the first operand has desirable
side effects that must be
sequenced before the second operand. Further, because it is rarely used outside of specific idioms, and easily mistaken with other commas or the semicolon, it is potentially confusing and error-prone. Nevertheless, there are certain circumstances where it is commonly used, notably in for loops and in
SFINAE. For embedded systems which may have limited
debugging capabilities, the comma operator can be used in combination with a macro to seamlessly override a function call, to insert code just before the function call.
For loops The comma operator allows multiple
assignment statements without using a block statement, for example in the initialization and the increment expressions of a
for loop. In the following example, the order of the loop's initializers is significant: void rev(char *s, size_t len) { char *first; for (first = s, s += len; s >= first; --s) { putchar(*s); } } 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.
Macros The comma can be used in preprocessor macros to perform multiple operations in the space of a single syntactic expression. 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: • include • include int main ( void ) { for (int i = 0; i Output: i = 0 i = 1 i = 2 i = 3 i = 4 assert: assert.c:6: test_assert: Assertion `("i is too big!", i However the assert macro is usually disabled in production code, so use it only for debug purposes.
Condition The comma can be used within a condition (of an if, while, do while, or for) to allow auxiliary computations, particularly calling a function and using the result, with
block scoping: if (y = f(x), y > x) { ... // statements involving x and y } A similar idiom exists in
Go, where the syntax of the if statement explicitly allows an optional statement.
Complex return The comma can be used in return statements, to assign to a
global variable or out parameter (passed by reference). This idiom suggests that the assignments are part of the return, rather than auxiliary assignments in a block that terminates with the actual return. For example, in setting a global error number: if (failure) return (errno = EINVAL, -1); This can also be written as: if (failure) { errno = EINVAL; return -1; }
Avoid a block For brevity, the comma can be used to avoid a block and associated braces, as in: if (x == 1) y = 2, z = 3; instead of: if (x == 1) { y = 2; z = 3; } ==Other languages==