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;
}


Output

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;
}


Output

30

In the main() function, add() function is called where we used a comma to separate multiple arguments.

Comma in C++

Comma (,) in C++  can work in three different contexts:

  1. Comma as an operator
  2. Comma as a separator
  3. Comma operator in place of a semicolon

Similar Reads

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....

2. Comma as a separator

...

3. Comma Operator in Place of a Semicolon

...