Comma in C++
Comma (,) in C++ can work in three different contexts:
- Comma as an operator
- Comma as a separator
- Comma operator in place of a semicolon
1. Comma as an operator
A comma operator in C++ is a binary operator. It evaluates the first operand & discards the result, evaluates the second operand & returns the value as a result. It has the lowest precedence among all C++ Operators. It is left-associative & acts as a sequence point.
Syntax:
data_type variable = (value1, value2);
Example 1:
C++
// C++ program to demonstrate // comma as a operator #include <iostream> using namespace std; int main() { // comma as a operator int num = (24, 78, 85); cout << num << endl; return 0; } |
85
As we can see, the values 24,78, and 85 have been enclosed in the parenthesis. This is because the assignment operator has higher precedence than the comma operator so if we don’t use parenthesis, we will encounter an error as the compiler will treat integer values 78 and 85 as an identifier in variable declaration according to C++ syntax. Let’s understand this with the help of the following example.
Example 2:
C++
// C++ program to demonstrate // comma as a operator #include <iostream> using namespace std; int main() { // Assignment Operator int num1 = 10, 24, 30; // Commma as a Operator int num2 = (10, 24, 30); cout << num1 << endl; cout << num2 << endl; return 0; } |
Output
error: expected unqualified-id before numeric constant int num1 = 10, 24, 30; ^
2. Comma as a separator
A comma as a separator is used in multiple variable declarations and multiple arguments in a function call.
Syntax:
data_type var_name1, var_name2; //in variable declaration function_call(argument_1, argument_2); // in function call
Example 1:
C++
// C++ program to demonstrate // the use of comma as a // separator #include <iostream> using namespace std; int main() { // using comma as a separator int num1 = 34, num2 = 45, num3 = 65; cout << num1 << " " << num2 << " " << num3 << endl; return 0; } |
34 45 65
In the above program, we declare multiple variables declarations in a single line as separated by a comma. A comma acts here as a separator, not an operator.
Another example of multiple arguments is in a function call, where we used commas as a separator.
Example 2:
C++
// C++ program to demonstrate the use of comma as a // separator #include <iostream> using namespace std; // created a function for addition of numbers int add( int num1, int num2, int num3) { int result = num1 + num2 + num3; return result; } int main() { // variable declaration int number1 = 5, number2 = 10, number3 = 15; // function calling using multiple arguments separated by // a comma int addition = add(number1, number2, number3); cout << addition << endl; return 0; } |
30
In the main() function, add() function is called where we used a comma to separate multiple arguments.
3. Comma Operator in Place of a Semicolon
The comma operator in C++ can be also used for terminating statements of the C++ Program. To use a comma operator in place of a semicolon, the following conditions must be satisfied.
- The variable declaration statements must be terminated with a semicolon.
- The statements after the declaration statement can be terminated by the comma operator.
- The last statement of the program must be terminated by a semicolon.
C++
// C++ program to demonstrate // the use of comma as a // semicolon #include <iostream> using namespace std; int main() { // variable declaration int num1 = 54; int num2 = 34; int num3 = 45; cout << num1 << endl, cout << num2 << endl, cout << num3 << endl; return 0; } |
54 34 45
In the above program all the variable declarations num1, num2 & num3 are ended with semicolons and the last statement of output is ended with a semicolon.
Please Login to comment...