Comma Operator in C with Examples

What is a Comma operator in C?

The comma operator , in C is used to evaluate multiple expressions in a single statement. All expressions are executed from left to right, but only the result of the last expression is returned.

Syntax:

expression1, expression2, ..., expressionN;

Only the last expression is used as the value of the entire statement.

Example:

int x;
x = (5 + 3, 10 + 2);  // x will be assigned 12
  • First, 5 + 3 is evaluated → result 8 (but discarded).
  • Then, 10 + 2 is evaluated → result 12 (used).
  • So, x = 12.

Use Cases of the Comma Operator in C

1. Multiple Expressions in a Single Statement

advertisement

Evaluating several expressions in one line where only the last result is assigned or returned.

#include <stdio.h>
 
int main()
{
    int x;
    // Multiple expressions separated by commas
    x = (3 + 2, 4 * 5, 100);  
 
    printf("Value of x: %d\n", x);
 
    return 0;
}

Output:

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
Value of x: 100

This C program shows how the comma operator works. It sets x using (3 + 2, 4 * 5, 100). All expressions are evaluated, but only the last one (100) is used. So, x becomes 100. The program then prints: “Value of x: 100” and ends.

2. for Loop Initialization and Update Sections

Executing multiple initializations or updates in a for loop using the comma operator.

for (int i = 0, j = 10; i < j; i++, j--)
{
    printf("i = %d, j = %d\n", i, j);
}
  • Both i and j are initialized and updated using commas.
  • In the update section, i++ and j– both execute in each iteration.

3. Macros and Inline Compact Logic

Packing multiple expressions into one line inside a macro.

#define SQUARE_AND_PRINT(x) ((x) *= (x), printf("%d\n", (x)))
 
int a = 4;
SQUARE_AND_PRINT(a); // Outputs 16
  • First a is squared, then printed.
  • The comma neatly separates two actions in one line.

4. Compact Return Statements

Use in return statements when you want to execute some code before returning a value.

advertisement
int getValue()
{
    int x = 10;
    return (x++, x + 5); // x becomes 11, then returns 16
}

Comma Operator vs Comma Separator

It’s important to distinguish the comma operator from the comma separator.

Feature Comma Operator Comma Separator
Usage Evaluates expressions Separates variables/arguments
Returns Value of last expression Nothing
Example (a = 2, b = 3, a + b) int a, b, c;

FAQs about Comma Operator in C

1. What is the comma operator in C?
The comma operator allows you to write multiple expressions in a single statement, separating them with commas. All expressions are evaluated from left to right, but only the result of the last expression is used.

2. How is the comma operator different from a regular comma?
In C, commas are also used to separate items (like function arguments or variable declarations), but the comma operator is used inside expressions to control evaluation.

3. Where is the comma operator commonly used?
You’ll often find it in loops, especially in for loops, where multiple variables might be updated at once.

4. Is the comma operator useful in decision making?
Not directly. It’s more useful when you need to perform several actions in a single place, like initializing or updating multiple variables.

5. Is the comma operator the same in all programming languages?
No. While some languages like C++ also support it, others might not, or they might use commas only as separators, not operators.

6. Where should I avoid using the comma operator?
Avoid using it in complex expressions where it can make the code hard to read or maintain.

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

If you wish to look at all C Tutorials, go to C Tutorials.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.