A structured lesson workspace with readable content, hands-on examples, and a clean path to completion.
The comma operator evaluates its left operand, discards the result, evaluates its right operand, and returns that. a = (1, 2, 3) assigns 3 to a. The 1 and 2 are evaluated and thrown away. This is not the same as the commas in a variable declaration or a function argument list. Those are separators, not operators. The comma operator appears in for loop headers, in macro definitions, and in code that is trying too hard to be clever.
#include <stdio.h>int main(void) {int a = (10, 20, 30);printf("a = %d\n", a);int x = 0;int y = (x = 5, x * 2);printf("x = %d, y = %d\n", x, y);for (int i = 0, j = 10; i < j; i++, j--) {printf("i=%d j=%d ", i, j);}printf("\n");return 0;}
In the for header, i = 0, j = 10 is two declarations separated by a comma separator, not the comma operator. But i++, j-- in the increment slot is the comma operator: it evaluates i++, discards the result, evaluates j--, and the whole expression's value is discarded anyway. The for loop is the one place where the comma operator is idiomatic and readable.
The semicolon separates statements. The comma operator sequences expressions within a single statement. You cannot put a semicolon inside a for header's increment slot. You can put a comma. That is the practical reason the comma operator exists: it lets you do two things in a context that syntactically requires one expression.
Do not use the comma operator to chain assignments or function calls in general code. int a = 1, b = 2; is a declaration with two declarators, not the comma operator. printf("%d", a), printf("%d", b); is two statements separated by a comma operator, and it is unreadable. Use semicolons. Use separate lines. The comma operator is for for-loops and macros. Everywhere else, it is a code smell.
The comma operator is rarely needed outside of for loops. When you see it elsewhere, read carefully. It is probably doing something subtle, or it is a bug where someone meant to write a semicolon. Next: lvalues and rvalues, the distinction that determines what can appear on the left side of an assignment.
Finish the lesson once you have worked through the material. This awards ★ 20 XP.