In C and C++, the comma operator is a binary operator that:
- Evaluates the first operand, and discards the result
- Evalutes the second operand, and returns this value
So:
if (a,b) {
}
Here, a
is evaluated first and then discarded. Next, b
is evaluated and returned to the condition. If there are more than two operands, then the last expression will be returned.
Remember this doesn't ensure both conditions are true. For that, you need &&
The comma operator is handy when you don't need the results (side effect) of a particular operation. For example:
if (numeric_read(str, &err), !err) {
// Handle ...
}