C Reference ยท Operators and Expressions
The comma operator in C
Reference for the C comma operator: left-to-right evaluation, the value of a comma expression, use in for loops, and how it differs from argument commas.
The comma operator evaluates its left operand, throws the value away, and then evaluates its right operand. The value and type of the whole expression are those of the right operand. Between the two evaluations there is a sequence point, so every side effect of the left side is complete before the right side starts.
It has the lowest precedence of all operators. Its main uses are packing two updates into one for header and, rarely, writing several steps where only a single expression is allowed (for example in a macro).
int x = (puts("first"), 42); /* x is 42 */ for (i = 0, j = 9; i < j; i++, j--) { /* ... */ }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
expression1 , expression2
| Part | Description |
|---|---|
expression1 |
Evaluated first, as a void expression. Its value is discarded. |
expression2 |
Evaluated second. Its value and type become the result of the whole expression. |
Result: the value of expression2. The result is not an lvalue.
Errors / undefined behavior: none of its own. Because of the sequence point, i = 0, i++ is well defined (unlike unsequenced uses of i inside one operand).
Usage notes
- Not the same comma everywhere. In an argument list (
f(a, b)), in an initializer list ({1, 2}) and in a declaration (int x, y;) the comma is just punctuation. It is the operator only inside an expression. To pass a comma expression as an argument, add parentheses:f((a, b))passes one argument. - Lowest precedence.
x = 1, 2parses as(x = 1), 2, soxis 1. Writex = (1, 2)if you want 2. forheaders are the one place where it is common:for (i = 0, j = n - 1; i < j; i++, j--). Declarations in the first clause use the declaration comma, not the operator.- Compiler warnings: a left operand with no side effect, as in
(a, b), is usually flagged by-Wall("left-hand operand of comma expression has no effect") because it is almost always a typo, for examplea[1, 2]written instead ofa[1][2]. - Macros: it lets a macro contain several steps and still be an expression, but a
do { ... } while (0)block or a small function is usually clearer and safer. - Readability: outside
forheaders, prefer separate statements. Inif (x > 0) a = 1, b = 2;both assignments are guarded, because the whole comma expression is one statement, but it reads as if only the first one were. - Constant expressions: a comma operator may not appear in an integer constant expression (such as an array size) unless it is inside
sizeofor another unevaluated context.