

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Comma operator in C/C++
The purpose of comma operator is to string together several expressions. The value of a commaseparated list of expressions is the value of the right-most expression. Essentially, the comma's effect is to cause a sequence of operations to be performed.
The values of the other expressions will be discarded. This means that the expression on the right side will become the value of the entire comma-separated expression. For example
var = (count = 19, incr = 10, count+1);
Here first assigns count the value 19, assigns incr the value 10, then adds 1 to count, and finally, assigns var the value of the rightmost expression, count+1, which is 20. The parentheses are necessary because the comma operator has a lower precedence than the assignment operator.
To see the effects of the comma operator, try running the following program.
Example Code
#include <iostream> using namespace std; int main() { int i, j; j = 10; i = (j++, j+100, 999+j); cout << i; return 0; }
Output
1010
Here is the procedure how the value of i gets calculated: j starts with the value 10. j is then incremented to 11. Next, j is added to 100. Finally, j (still containing 11) is added to 999, which yields the result 1010.
- Related Questions & Answers
- What is Comma operator in C++?
- A comma operator question in C/C++ ?
- How does the Comma Operator work in C++
- Why do we use comma operator in C#?
- What is Comma Operator (,) in JavaScript?
- When is the comma operator useful in JavaScript?
- Comma separated argument applicable for IN operator in MySQL?
- Comma in C and C++
- deque::operator= and deque::operator[] in C++ STL
- Unary operator in C++
- Ternary Operator in C#
- Operator Functions in C#
- Default operator in C#
- Division Operator in C#
- Sizeof operator in C