The comma operator is an operator that evaluates its left-hand side, discards the result, then evaluates to the right-hand side. This is used in multiple languages, including C, C++ and JavaScript.
Questions tagged [comma-operator]
259 questions
211
votes
8 answers
194
votes
9 answers
How does the Comma Operator work
How does the comma operator work in C++?
For instance, if I do:
a = b, c;
Does a end up equaling b or c?
(Yes, I know this is easy to test - just documenting on here for someone to find the answer quickly.)
Update: This question has exposed a…

Joe Schneider
- 9,179
- 7
- 42
- 59
175
votes
7 answers
What does i = (i, ++i, 1) + 1; do?
After reading this answer about undefined behavior and sequence points, I wrote a small program:
#include
int main(void) {
int i = 5;
i = (i, ++i, 1) + 1;
printf("%d\n", i);
return 0;
}
The output is 2. Oh God, I didn't see the…

gsamaras
- 71,951
- 46
- 188
- 305
113
votes
15 answers
When is the comma operator useful?
I read this question about the "comma operator" in expressions (,) and the MDN docs about it, but I can't think of a scenario where it is useful.
So, when is the comma operator useful?

gdoron
- 147,333
- 58
- 291
- 367
108
votes
8 answers
How do I put two increment statements in a C++ 'for' loop?
I would like to increment two variables in a for-loop condition instead of one.
So something like:
for (int i = 0; i != 5; ++i and ++j)
do_something(i, j);
What is the syntax for this?

Peter Smit
- 27,696
- 33
- 111
- 170
106
votes
5 answers
What does the comma operator do in JavaScript?
If I use:
1.09 * 1; // returns "1.09"
But if I use:
1,09 * 1; // returns "9"
I know that 1,09 isn't a number.
What does the comma do in the last piece of code?
More Examples
if (0,9) alert("ok"); // alert
if (9,0) alert("ok"); // don't…

Topera
- 12,223
- 15
- 67
- 104
93
votes
20 answers
Uses of C comma operator
You see it used in for loop statements, but it's legal syntax anywhere. What uses have you found for it elsewhere, if any?

pythonic metaphor
- 10,296
- 18
- 68
- 110
89
votes
4 answers
Different behaviour of comma operator in C++ with return?
This (note the comma operator):
#include
int main() {
int x;
x = 2, 3;
std::cout << x << "\n";
return 0;
}
outputs 2.
However, if you use return with the comma operator, this:
#include
int f() { return 2, 3;…

xyz
- 3,349
- 1
- 23
- 29
49
votes
9 answers
What is the proper use of the comma operator?
I saw this code:
if (cond) {
perror("an error occurred"), exit(1);
}
Why would you do that? Why not just:
if (cond) {
perror("an error occurred");
exit(1);
}

Kam
- 5,878
- 10
- 53
- 97
48
votes
18 answers
C++ -- return x,y; What is the point?
I have been programming in C and C++ for a few years and now I'm just now taking a college course in it and our book had a function like this for an example:
int foo(){
int x=0;
int y=20;
return x,y; //y is always returned
}
I have never seen…

Earlz
- 62,085
- 98
- 303
- 499
41
votes
3 answers
Why do 'return', 'continue' or 'break' not work with the comma operator?
I can write the code if(1) x++, y++; instead of if(1) {x++; y++;}, but in some cases it does not work (see below). It would be nice if you tell me about this.
int x = 5, y = 10;
if (x == 5) x++, y++; // It works
if (x == 5) x++, return 0; //…

Nayem
- 519
- 4
- 6
40
votes
8 answers
What does the comma operator do?
What does the following code do in C/C++?
if (blah(), 5) {
//do something
}

Matt
- 2,226
- 16
- 18
38
votes
3 answers
Comma operator in c
#include
int main(void) {
int a;
a = (1, 2), 3;
printf("%d", a);
return 0;
}
output: 2
Can any one explain how output is 2?

Kr Arjun
- 249
- 3
- 9
38
votes
6 answers
How does the compiler know that the comma in a function call is not a comma operator?
Consider the function call (calling int sum(int, int))
printf("%d", sum(a,b));
How does the compiler decide that the , used in the function call sum(int, int) is not a comma operator?
NOTE: I didn't want to actually use the comma operator in…

haccks
- 104,019
- 25
- 176
- 264
37
votes
7 answers
sizeof taking two arguments
In C.1.3 of the C++ IS (2003. It's in the C++11 IS, too), the standard points out a difference between ISO C and C++; namely, for
char arr[100];
sizeof(0, arr) returns sizeof(char*) in C, but 100 in C++.
I can find no documentation for sizeof…

John
- 2,326
- 1
- 19
- 25