The comma operator

Operator Symbol Form Operation
Comma , a, b Evaluate a then b, return value of b

The comma operator (,) lets you evaluate multiple expressions where a single expression is expected. It evaluates the left operand first, then the right operand, and finally returns the result of the right operand.

Example:

#include <iostream>

int main()
{
    int health{100};
    int mana{50};

    std::cout << (++health, ++mana) << '\n'; // increment both, return right operand

    return 0;
}

First, the left operand evaluates, incrementing health from 100 to 101. Next, the right operand evaluates, incrementing mana from 50 to 51. The comma operator returns the right operand's result (51), which gets printed.

The comma operator has the lowest precedence of all operators—even lower than assignment. This creates different behaviors:

result = (a, b); // evaluates (a, b) first to get b's value, then assigns to result
result = a, b;   // evaluates as "(result = a), b", so result gets a's value, and b is evaluated and discarded

This low precedence makes the comma operator somewhat dangerous.

Nearly every use of the comma operator is better written as separate statements:

#include <iostream>

int main()
{
    int health{100};
    int mana{50};

    ++health;
    std::cout << ++mana << '\n';

    return 0;
}

Most programmers avoid the comma operator entirely, with one exception: inside for loops, where it's commonly used. We discuss for loops in the For statements lesson.

Best Practice
Avoid using the comma operator, except within for loops.

Comma as a separator

In C++, commas often serve as separators rather than operators. These separator commas don't invoke the comma operator:

void processData(int value1, int value2) // separator comma between parameters
{
    calculate(value1, value2); // separator comma between arguments
    constexpr int a{10}, b{20}; // separator comma between variable definitions (avoid this)
}

No need to avoid separator commas (except when defining multiple variables on one line, which you should avoid anyway).

Summary

  • Comma operator (,): Evaluates the left operand, then the right operand, and returns the result of the right operand
  • Lowest precedence: The comma operator has lower precedence than all other operators, including assignment
  • Precedence danger: result = a, b evaluates as (result = a), b (assigns a to result, then evaluates and discards b)
  • Use parentheses: result = (a, b) evaluates the comma expression first, then assigns b to result
  • Better alternatives: Nearly every use of the comma operator is clearer when written as separate statements
  • One valid use: The comma operator is acceptable within for loops for initializing or updating multiple variables
  • Comma as separator: Commas between function parameters, function arguments, or variable declarations are separators, not comma operators

The comma operator is rarely useful and often dangerous due to its low precedence. Most C++ programmers avoid it entirely, with the exception of using it within for loops. When you see a comma in code, it's almost always a separator rather than the comma operator.