Skip to content
Tutorials Center
My learning

Topics

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).

C
int x = (puts("first"), 42);   /* x is 42 */
for (i = 0, j = 9; i < j; i++, j--) { /* ... */ }

Try it

C Value of a comma expression

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.

C Two variables in a for loop

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

C
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, 2 parses as (x = 1), 2, so x is 1. Write x = (1, 2) if you want 2.
  • for headers 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 example a[1, 2] written instead of a[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 for headers, prefer separate statements. In if (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 sizeof or another unevaluated context.