Ready to practice?
Sign up to access interactive coding exercises and track your progress.
Comma Operator
Evaluate multiple expressions in sequence and understand when the comma operator is useful.
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.
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, bevaluates as(result = a), b(assignsatoresult, then evaluates and discardsb) - Use parentheses:
result = (a, b)evaluates the comma expression first, then assignsbtoresult - 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.
Comma Operator - Quiz
Test your understanding of the lesson.
Practice Exercises
Understanding the Comma Operator
Learn how the comma operator works and when it might be useful (though it should generally be avoided for clarity).
Lesson Discussion
Share your thoughts and questions
No comments yet. Be the first to share your thoughts!