🚀 Learn BLE on Linux (BlueZ) – Programming Part 1
Hands-on Bluetooth Low Energy course. Special price: ₹399
Enroll Now on Udemy →C Comma Operator Explained
Understand the C comma operator, its lowest precedence and the separator trap, with compiled examples in our free C programming course.
Welcome to Lecture 10 of our free C programming course. The comma operator is the most misunderstood symbol in C. You have already typed commas hundreds of times, in declarations and function calls, but those are not the comma operator. The real operator lets you join several expressions into one, evaluating them left to right. It is rare in application code, yet it is everywhere in embedded C programming, especially in for loops that walk two indices at once.
This lesson is part of a free C tutorial for C programming for embedded systems. Every program was compiled with GCC 13 (-Wall -Wextra -std=c11) and run, and the compiler warnings you see are the real ones.
What You Will Learn in This Free C Programming Course Lesson
- What the comma operator does and what value it produces
- Why its precedence is the lowest in C, and why parentheses matter
- The difference between the comma operator and the comma separator
- How to use it in
forloops with two variables - Why the comma swap trick works and why a plain
tempswap is still better - Which compiler warnings appear when you misuse it
Prerequisites
You should know assignment, arithmetic and relational operators, and have seen a for loop. If you are new to loops, do not worry: this lecture only uses the simplest forms, and loops are covered in depth in the next chapter of this free embedded C course.
What the C Comma Operator Does
The comma operator , lets you put several expressions where C expects just one. The expressions are separated by commas and evaluated from left to right. The type and value of the whole compound expression are the type and value of the rightmost expression. All the earlier values are computed, then thrown away, but their side effects (like assignments) stay.
How a = 8, b = 7, c = 9, a + b + c is evaluated
C guarantees a sequence point after the left operand of a comma operator. That is a strong promise: unlike the two operands of +, whose order is unspecified, the comma operator always finishes the left side completely before starting the right side.
Your First Comma Expression
Four expressions are joined into one. The assignments happen in order, and the last expression a + b + c supplies the value stored in sum. The parentheses are essential, as the next section shows.
#include <stdio.h>
int main(void)
{
int a, b, c, sum;
sum = (a = 8, b = 7, c = 9, a + b + c);
printf("a=%d b=%d c=%d sum=%d\n", a, b, c, sum);
return 0;
}
Build and run it:
gcc -Wall -Wextra -std=c11 -o ep_comma_basic ep_comma_basic.c
./ep_comma_basic
Expected output:
a=8 b=7 c=9 sum=24
Without the comma operator the same job needs four separate statements: a = 8; b = 7; c = 9; sum = a + b + c;. The comma version is more compact, though not necessarily clearer. Use it where it earns its keep.
Comma Operator Precedence: The Lowest in C
The comma operator has the lowest precedence of all C operators, even lower than assignment. This is why sum = (a = 8, b = 7, c = 9, a + b + c); needs the parentheses: without them the assignment would bind first and the rest would be separate comma-joined expressions.
#include <stdio.h>
int main(void)
{
int x, y;
x = 1, 2, 3; /* parsed as (x = 1), 2, 3 */
y = (1, 2, 3); /* comma runs first, 3 is assigned */
printf("x = %d\n", x);
printf("y = %d\n", y);
return 0;
}
The compiler warns that parts of these expressions do nothing:
ep_comma_prec.c: In function ‘main’:
ep_comma_prec.c:7:10: warning: right-hand operand of comma expression has no effect [-Wunused-value]
7 | x = 1, 2, 3; /* parsed as (x = 1), 2, 3 */
| ^
ep_comma_prec.c:7:13: warning: right-hand operand of comma expression has no effect [-Wunused-value]
7 | x = 1, 2, 3; /* parsed as (x = 1), 2, 3 */
| ^
ep_comma_prec.c:8:11: warning: left-hand operand of comma expression has no effect [-Wunused-value]
8 | y = (1, 2, 3); /* comma runs first, 3 is assigned */
| ^
ep_comma_prec.c:8:14: warning: left-hand operand of comma expression has no effect [-Wunused-value]
8 | y = (1, 2, 3); /* comma runs first, 3 is assigned */
| ^
Output:
x = 1
y = 3
Two ways to read the same tokens
Always Parenthesise When You Want the Value
If you want the last value of a comma expression to be assigned, put the whole comma list in parentheses. Forget them and only the first expression is assigned.
Comma Operator vs Comma Separator
This is the question interviewers love. A comma is not always an operator. In several places the comma is just punctuation that separates items:
| Where the comma appears | Role | Order guaranteed? |
|---|---|---|
int a, b, c; declarations | Separator | Not relevant |
add(p, q) function arguments | Separator | No, argument evaluation order is unspecified |
int arr[] = {1, 2, 3}; initialisers | Separator | Not relevant |
x = (p, q); inside an expression | Operator | Yes, left to right |
for (i = 0, j = 5; ...; i++, j--) | Operator | Yes, left to right |
Separator Versus Operator in a Function Call
In add(p, q) the comma separates two arguments. In add((p, q), 1) the extra parentheses turn the first argument into a comma operator expression whose value is q, so the call is add(20, 1):
#include <stdio.h>
static int add(int x, int y) { return x + y; }
int main(void)
{
int p = 10, q = 20;
printf("add(p, q) = %d\n", add(p, q));
printf("add((p, q), 1) = %d\n", add((p, q), 1));
return 0;
}
Build and run it:
gcc -Wall -Wextra -std=c11 -o ep_comma_args ep_comma_args.c
./ep_comma_args
Expected output:
add(p, q) = 30
add((p, q), 1) = 21
The warning tells you the left operand p did nothing. When you see that warning in real code it usually means a bug, such as a typo where you meant a different punctuation mark.
Order of Evaluation with Side Effects
Since the left side always completes first, you can chain operations whose later steps depend on earlier ones:
Left to Right, Every Time
In the first line n++ runs, then n * 10 is computed with the new n (4), giving 40. In the second line n is doubled to 8, the middle value is discarded, and the last expression n - 1 yields 7.
#include <stdio.h>
int main(void)
{
int n = 3, r;
r = (n++, n * 10);
printf("n=%d r=%d\n", n, r);
r = (n *= 2, n + 1, n - 1);
printf("n=%d r=%d\n", n, r);
return 0;
}
Build and run it:
gcc -Wall -Wextra -std=c11 -o ep_comma_order ep_comma_order.c
./ep_comma_order
Expected output:
n=4 r=40
n=8 r=7
Embedded C Programming Example: Two Indices in a for Loop
The most respectable use of the comma operator is in the initialisation and update parts of a for loop, where C allows only one expression in each slot. Two-pointer loops are everywhere in firmware: reversing a buffer, checking a palindrome, converting endianness, meeting in the middle of a sorted table.
Moving i Up and j Down at the Same Time
Each iteration prints the two indices; the loop ends when they cross:
#include <stdio.h>
int main(void)
{
int i, j;
for (i = 0, j = 5; i < j; i++, j--)
printf("i=%d j=%d\n", i, j);
printf("loop ended with i=%d j=%d\n", i, j);
return 0;
}
Build and run it:
gcc -Wall -Wextra -std=c11 -o ep_comma_for ep_comma_for.c
./ep_comma_for
Expected output:
i=0 j=5
i=1 j=4
i=2 j=3
loop ended with i=3 j=2
Reversing a Buffer In Place
Here is a realistic example. The indices start at both ends of an array and move toward each other, swapping as they go. The program reverses five values:
#include <stdio.h>
int main(void)
{
int data[] = {10, 20, 30, 40, 50};
int n = (int)(sizeof data / sizeof data[0]);
int i, j, tmp;
for (i = 0, j = n - 1; i < j; i++, j--) {
tmp = data[i];
data[i] = data[j];
data[j] = tmp;
}
for (i = 0; i < n; i++)
printf("%d ", data[i]);
printf("\n");
return 0;
}
Build and run it:
gcc -Wall -Wextra -std=c11 -o ep_comma_reverse ep_comma_reverse.c
./ep_comma_reverse
Expected output:
50 40 30 20 10
Indices meeting in the middle
The Comma Swap Trick
You can use commas to swap two variables on a single line without breaking the left-to-right rule:
Swapping with the Comma Operator
Each assignment completes before the next one starts, so the temporary variable holds the old a at the right moment.
#include <stdio.h>
int main(void)
{
int a = 8, b = 7, temp;
printf("before: a=%d b=%d\n", a, b);
temp = a, a = b, b = temp;
printf("after : a=%d b=%d\n", a, b);
return 0;
}
Build and run it:
gcc -Wall -Wextra -std=c11 -o ep_comma_swap ep_comma_swap.c
./ep_comma_swap
Expected output:
before: a=8 b=7
after : a=7 b=8
It works, but a three-line swap with separate statements is just as short in machine code and clearer on the screen. Show the trick in a quiz, not in production code.
Common Mistakes with the Comma Operator
- Forgetting the parentheses:
x = 1, 2, 3;stores 1, not 3. - Using a comma instead of a semicolon in a
forheader:for (i = 0, i < 10, i++)is a compile error or a bug. - Confusing separator and operator: arguments in a call are not evaluated left to right just because commas separate them.
- Comma in a return:
return a, b;returns onlyb. This is legal and almost always a mistake. - Putting commas in
ifconditions:if (x = read(), x > 5)hides the logic; split it across lines.
Best Practices
- Reserve the comma operator for
forloop headers and simple macros. - Wrap every comma expression you assign or pass in parentheses.
- If the code has more than two items, use separate statements instead.
- Compile with
-Wall -Wextraand treat “has no effect” warnings as real bugs. - Never rely on the order of function argument evaluation. If you need order, use temporary variables.
Safety Notes
Coding standards for safety-critical C (such as MISRA C) restrict the comma operator because it makes side effects harder to see during review. If your project follows one, keep comma use to the loop headers the standard allows.
Summary: Key Takeaways from This Free C Programming Course Lesson
| Topic | Rule |
|---|---|
| Evaluation | Left to right with a sequence point after each left operand |
| Value | Type and value of the rightmost expression |
| Precedence | Lowest of all operators; parenthesise when assigning |
| Separator | Commas in declarations, calls and initialisers are not operators |
| Best use | Two variables in a for header |
Conclusion
The comma operator is a small tool with a big reputation for confusing people. Now you know the two rules that matter: it evaluates left to right, and it returns the last value. In the next lecture of this free C programming course we look at sizeof, the operator that tells you how many bytes any type or object occupies, a question every embedded programmer asks daily.
Quiz: Test Your Understanding
Question 1. What is sum after sum = (a = 8, b = 7, c = 9, a + b + c);?
Show answer
24. The value of the last expression, 8 + 7 + 9, is assigned.
Question 2. What is x after x = 1, 2, 3;?
Show answer
1. Assignment binds tighter than the comma, so it parses as (x = 1), 2, 3;.
Question 3. What is y after y = (1, 2, 3);?
Show answer
3. The parentheses make the comma operator run first and yield its rightmost value.
Question 4. In add(p, q), is the comma an operator?
Show answer
No. It is a separator between function arguments, and the order in which arguments are evaluated is unspecified.
Question 5. How many times does the body of for (i = 0, j = 5; i < j; i++, j--) run?
Show answer
3 times, with (i, j) = (0,5), (1,4), (2,3). Then i = 3, j = 2 and the test fails.
Question 6. After n = 3; r = (n++, n * 10); what are n and r?
Show answer
n is 4 and r is 40, because n++ finishes before n * 10 is evaluated.
Interview Questions: Free C Programming Course Edition
Interview Question 1. What does the comma operator return?
Show answer
The value (and type) of its right operand, after evaluating the left operand and discarding its value.
Interview Question 2. Which operator has the lowest precedence in C?
Show answer
The comma operator.
Interview Question 3. Is there a sequence point at a comma?
Show answer
Yes for the comma operator: the left operand is fully evaluated first. No for the comma separating function arguments.
Interview Question 4. What does return a, b; do?
Show answer
It evaluates a, discards it, and returns b. Compilers may warn that a has no effect.
Interview Question 5. Where is the comma operator commonly used?
Show answer
In for loop headers to initialise or update two variables, and occasionally in macros that need to run several steps as one expression.
Interview Question 6. Why are parentheses needed in sum = (a = 8, b = 7, a + b);?
Show answer
Because assignment has higher precedence than the comma operator. Without parentheses only the first expression would be assigned to sum.
Frequently Asked Questions
What is the comma operator in C?
It is an operator that evaluates its left operand, discards the value, then evaluates its right operand and returns that value. Evaluation is strictly left to right.
Is a comma in a function call the comma operator?
No. In a function call, a declaration or an initialiser list, the comma is just a separator. Only the order of evaluation of the comma operator is guaranteed.
Why does x = 1, 2, 3; set x to 1?
Assignment has higher precedence than the comma operator, so it parses as (x = 1), 2, 3. Use y = (1, 2, 3) to assign 3.
Can I use the comma operator in a for loop?
Yes. It is the most common use, for example for (i = 0, j = n – 1; i < j; i++, j–).
Does the comma operator have the lowest precedence?
Yes, it is the lowest-precedence operator in C, lower than assignment.
Should I use the comma operator to swap two variables?
You can, but a normal temporary-variable swap on separate lines is clearer and compiles to the same code.
Is this comma operator lesson part of a free course?
Yes. It belongs to the free C programming course on EmbeddedPathashala for students of embedded C programming.
Keep Learning in the Free C Programming Course
Next up: the sizeof operator. Try changing the loop limits in today’s programs and predict the output first.

2 Comments