Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
A comma operator question in C/C++ ?
The comma (,) in C/C++ programming language has two contexts −
-
As a separator − Used to separate variable declarations, function arguments, and initializer values. In this context, the comma is not an operator.
-
As an operator − The comma operator is a binary operator that evaluates the first expression, discards its result, then evaluates and returns the value of the second expression. This operator has the lowest precedence of all C/C++ operators ? even lower than the assignment operator.
Syntax
result = (expr1, expr2); // evaluates expr1, discards it, returns expr2
The parentheses are important. Without them, the assignment operator (=) binds tighter than the comma operator, which changes the behavior entirely.
Comma as Separator (Compilation Error)
When the comma appears in a declaration statement, it acts as a separator, not an operator. The following code tries to declare ch and then has stray character literals, causing a compilation error −
Example
#include <stdio.h>
int main(void) {
char ch = 'a', 'b', 'c'; // ERROR: comma is a separator here
printf("%c", ch);
return 0;
}
The output of the above code is −
error: expected identifier or '(' before 'b'
char ch = 'a', 'b', 'c';
^~~
The compiler treats the comma as a separator between variable declarations. After ch = 'a', it expects another variable name but finds the character literal 'b', which is invalid.
Comma as Operator (Without Parentheses)
When the comma appears in an expression statement (not a declaration), it acts as an operator. However, since the assignment operator (=) has higher precedence than the comma operator, the assignment happens first −
Example
#include <stdio.h>
int main(void) {
char ch;
ch = 'a', 'b', 'c'; // = binds tighter than comma
printf("%c\n", ch);
return 0;
}
The output of the above code is −
a
The expression is parsed as (ch = 'a'), 'b', 'c'. The assignment ch = 'a' executes first, then 'b' and 'c' are evaluated and discarded. So ch holds 'a'.
Comma as Operator (With Parentheses)
When the comma expression is enclosed in parentheses, all expressions are evaluated left to right, and the result of the last expression is returned −
Example
#include <stdio.h>
int f1() {
printf("f1() called\n");
return 43;
}
int f2() {
printf("f2() called\n");
return 123;
}
int main(void) {
int a;
a = (f1(), f2());
printf("a = %d\n", a);
return 0;
}
The output of the above code is −
f1() called f2() called a = 123
Both f1() and f2() are called. The comma operator discards the return value of f1() (43) and assigns the return value of f2() (123) to a. The parentheses ensure the comma is treated as an operator, not a separator.
Comma Operator vs Separator
| Feature | Comma as Separator | Comma as Operator |
|---|---|---|
| Context | Declarations, function arguments | Expressions |
| Returns a value? | No | Yes (value of the last expression) |
| Precedence | N/A | Lowest of all operators |
| Example | int a, b, c; |
a = (1, 2, 3); ? a = 3 |
Conclusion
The comma in C/C++ behaves differently depending on context ? as a separator in declarations and function calls, and as an operator in expressions. As an operator, it evaluates all expressions left to right and returns the last value. Since its precedence is lower than assignment, parentheses are needed when the comma operator result should be assigned to a variable.
