Skip to Content
Course content
Click on the "Edit" button in the top corner of the screen to edit your slide content.

Wait, I thought commas were just for separating things. How is it actually an "operator"?

You've been using commas since day one to separate variables in a declaration or arguments in a function call. In those cases, the comma is just a separator. But the comma operator is something different entirely. It's a binary operator that evaluates its first operand, discards the result, and then evaluates the second operand and returns that value.

It sounds useless at first—why do work just to throw it away? Look at this:

int x = (5, 10); 
// x is now 10

In the expression (5, 10), the 5 is evaluated and ignored, and the 10 is returned. I rarely use it this way in standalone assignments because it's confusing, but it's a critical concept for how C handles expressions.

If it just throws away the previous values, why would I ever use it?

The power of the comma operator isn't in the value it returns, but in the side effects of the expressions that come before it. The most common place you'll see this in professional code is inside a for loop header. Sometimes you need to update two different variables every iteration, but the for loop syntax only gives you one spot for the increment expression.

Instead of hacking a second loop inside your first, you use the comma operator:

int i = 0, j = 10;
while (i < j) {
    // Do something with i and j
    i++;
    j--;
}

// Or, more cleanly with a for loop:
for (i = 0, j = 10; i < j; i++, j--) {
    printf("i: %d, j: %d\n", i, j);
}

In i++, j--, the comma operator tells C: "Increment i, then decrement j, and the final result of this whole expression is whatever j-- evaluates to." Since the for loop doesn't actually care about the return value of the increment step, it's the perfect tool for the job.

Is there a difference between the comma operator and the commas in printf?

Yes, and this is where a lot of people get tripped up. The commas in a function call like printf("%d %d", a, b) are not comma operators; they are separators. They tell the compiler where one argument ends and the next begins.

The distinction becomes obvious if you try to wrap things in parentheses. Check this out:

int a = 1, b = 2;
printf("%d\n", (a++, b)); // This prints 2
printf("%d\n", a++, b);    // This is a compiler error (too many arguments)

In the first printf, the parentheses force the comma to act as an operator. The expression (a++, b) is evaluated first, resulting in the value of b, which is then passed as a single argument to the function. I'd advise against doing this in production code—it's "clever" in a way that makes your teammates want to hunt you down—but knowing the difference is key to reading complex C libraries.




📋 Practical Task

Coordinating Dual-Index Array Traversal

Write a program that takes an array of integers and reverses it in-place. To do this, you must use a single for loop where the "update" section (the third part of the loop header) uses the comma operator to move a 'start' index forward and an 'end' index backward simultaneously.

  • Create an array: int arr[] = {10, 20, 30, 40, 50, 60};
  • Initialize left = 0 and right = 5.
  • Use the comma operator in the for loop to increment left and decrement right.
  • Swap the elements at these indices until the indices meet in the middle.
  • Print the final reversed array to verify it works.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.