Quiz Entry - updated: 2026.07.10
What is the comma operator in C?
The comma operator evaluates its left and right expressions in order and yields the value of the rightmost one — letting you cram several side effects into one expression slot.
int a, b, c;
// Comma in for loop (common use):
for (i = 0, j = 10; i < j; i++, j--) {
// i starts at 0 going up, j starts at 10 going down
}
// Comma as operator (returns last value):
// c = 3
c = (a = 1, b = 2, a + b);
// Evaluates: a=1, then b=2, then returns a+b
// Different from comma in declarations:
// This is NOT the comma operator
int x = 1, y = 2;
Why it exists:
- Initialize multiple loop variables
- Execute multiple side effects
- Macros that need to do multiple things
Warning: The comma operator has the lowest precedence:
// Means: (c = a), b; -- NOT c = (a, b)
c = a, b;
// Means: c = b (after evaluating a)
c = (a, b);