
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Hello World
- C++ Omitting Namespace
- C++ Tokens
- C++ Constants/Literals
- C++ Keywords
- C++ Identifiers
- C++ Data Types
- C++ Numeric Data Types
- C++ Character Data Type
- C++ Boolean Data Type
- C++ Variable Types
- C++ Variable Scope
- C++ Multiple Variables
- C++ Basic Input/Output
- C++ Modifier Types
- C++ Storage Classes
- C++ Numbers
- C++ Enumeration
- C++ Enum Class
- C++ References
- C++ Date & Time
- C++ Operators
- C++ Arithmetic Operators
- C++ Relational Operators
- C++ Logical Operators
- C++ Bitwise Operators
- C++ Assignment Operators
- C++ sizeof Operator
- C++ Conditional Operator
- C++ Comma Operator
- C++ Member Operators
- C++ Casting Operators
- C++ Pointer Operators
- C++ Operators Precedence
- C++ Unary Operators
- C++ Control Statements
- C++ Decision Making
- C++ if Statement
- C++ if else Statement
- C++ Nested if Statements
- C++ switch Statement
- C++ Nested switch Statements
- C++ Loop Types
- C++ while Loop
- C++ for Loop
- C++ do while Loop
- C++ Foreach Loop
- C++ Nested Loops
- C++ break Statement
- C++ continue Statement
- C++ goto Statement
- C++ Strings
- C++ Strings
- C++ Loop Through a String
- C++ String Length
- C++ String Concatenation
- C++ String Comparison
- C++ Functions
- C++ Functions
- C++ Multiple Function Parameters
- C++ Recursive Function
- C++ Return Values
- C++ Function Overloading
- C++ Function Overriding
- C++ Default Arguments
- C++ Arrays
- C++ Arrays
- C++ Multidimensional Arrays
- C++ Pointer to an Array
- C++ Passing Arrays to Functions
- C++ Return Array from Functions
- C++ Structure & Union
- C++ Structures
- C++ Unions
- C++ Pointers
- C++ Pointers
- C++ Dereferencing
- C++ Modify Pointers
- C++ Class and Objects
- C++ Object Oriented
- C++ Classes & Objects
- C++ Class Member Functions
- C++ Class Access Modifiers
- C++ Static Class Members
- C++ Static Data Members
- C++ Static Member Function
- C++ Inline Functions
- C++ this Pointer
- C++ Friend Functions
- C++ Pointer to Classes
- C++ Constructors
- C++ Constructor & Destructor
- C++ Default Constructors
- C++ Parameterized Constructors
- C++ Copy Constructor
- C++ Constructor Overloading
- C++ Constructor with Default Arguments
- C++ Delegating Constructors
- C++ Constructor Initialization List
- C++ Dynamic Initialization Using Constructors
- C++ Object-oriented
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
- C++ Virtual Function
- C++ Pure Virtual Functions & Abstract Classes
- C++ File Handling
- C++ Files and Streams
- C++ Reading From File
- C++ Advanced
- C++ Exception Handling
- C++ Dynamic Memory
- C++ Namespaces
- C++ Templates
- C++ Preprocessor
- C++ Signal Handling
- C++ Multithreading
- C++ Web Programming
- C++ Socket Programming
- C++ Concurrency
- C++ Advanced Concepts
- C++ Lambda Expression
- C++ unordered_multiset
C++ Comma Operator
The purpose of comma operator is to string together several expressions. The value of a comma-separated 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.
How Comma Operator Works?
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.
Example of Comma Operator
To see the effects of the comma operator, try running the following program −
#include <iostream> using namespace std; int main() { int i, j; j = 10; i = (j++, j+100, 999+j); cout << i; return 0; }
When the above code is compiled and executed, it produces the following result −
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.
Use Cases of Comma Operator
1. Using Comma Operator in Variable Initialization
In the following example, we use the comma operator to initialize multiple variables in a single statement.
#include <iostream> using namespace std; int main() { int a, b, c; // Using comma operator for initialization a = (b = 10, c = 20); cout << "Value of a: " << a << endl; cout << "Value of b: " << b << endl; cout << "Value of c: " << c << endl; return 0; }
When executed, this program outputs:
Value of a: 20 Value of b: 10 Value of c: 20
2. Using Comma Operator in Loops
The comma operator can be used in loops to update multiple variables.
#include <iostream> using namespace std; int main() { // Using comma operator in a for loop for (int i = 0, j = 5; i < 5; i++, j--) { cout << "i: " << i << ", j: " << j << endl; } return 0; }
When executed, this program outputs:
i: 0, j: 5 i: 1, j: 4 i: 2, j: 3 i: 3, j: 2 i: 4, j: 1
3. Using Comma Operator in Function Arguments
The comma operator can be used in function calls where multiple expressions are evaluated, but only the last value is passed as an argument.
#include <iostream> using namespace std; // Function that takes an integer parameter void display(int x) { cout << "Received value: " << x << endl; } int main() { int a = 5, b = 10; // Using comma operator in function arguments display((a += 5, b += 20)); return 0; }
When executed, this program outputs:
Received value: 30
4. Using Comma Operator in if Statements
The comma operator can be useful in conditional statements when multiple expressions need to be checked.
#include <iostream> using namespace std; int main() { int x = 10, y = 20; // Using comma operator in if condition if (x += 5, y -= 10, x > y) { cout << "x is greater than y" << endl; } else { cout << "y is greater than or equal to x" << endl; } return 0; }
When executed, this program outputs:
x is greater than y