C
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Functions
-
Section 4: Arrays and Strings
-
Section 5: Pointers
-
Section 6: Memory Management
-
Section 7: Structures and Unions
-
Section 8: The Preprocessor and Build Process
-
Section 9: Standard Library: stdio.h
-
Section 10: Standard Library: stdlib.h
-
Section 11: Standard Library: string.h
-
Section 12: Standard Library: ctype.h and wctype.h
-
Section 13: Standard Library: math.h, complex.h, fenv.h, tgmath.h
-
Section 14: Standard Library: Type and Limit Headers
-
Section 15: Standard Library: Error Handling and Debugging
-
Section 16: Standard Library: Localization and Encoding
-
Section 17: Standard Library: time.h
-
Section 18: Standard Library: Concurrency (C11)
-
Section 19: POSIX and System Programming (unistd.h)
-
Section 20: More Data Structures
-
Section 21: Algorithms in C
-
Section 22: Bitwise Operations
-
Section 23: Command-Line Programs
-
Section 24: Debugging and Best Practices
-
Section 25: Compiler and Language Internals
-
Section 26: Embedded and Cross-Platform Considerations
-
Section 27: Networking Basics
-
Section 28: Practical Projects
-
Section 29: Interview Practice
-
Section 30: C23 Modern Features
-
Section 31: More Practice and Review
15: The Comma Operator
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 = 0andright = 5. - Use the comma operator in the
forloop to incrementleftand decrementright. - Swap the elements at these indices until the indices meet in the middle.
- Print the final reversed array to verify it works.
There are no comments for now.