# Comma in C

In C, comma ( , ) can be used in three contexts:

1. Comma as an operator
2. Comma as a separator
3. 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.

```// 10 is assigned to i
int i = (5, 10);

// f1() is called (evaluated)
// first followed by f2().
// The returned value of f2() is assigned to j
int j = (f1(), f2());```

Example 1:

## C

 `// C Program to Demonstrate` `// comma as operator` `#include ` `int` `main()` `{` `    ``int` `x = 10;` `    ``int` `y = 15;`   `    ``// using comma as an operator` `    ``printf``(``"%d"``, (x, y));` `    ``getchar``();` `    ``return` `0;` `}`

Output

`15`

Time Complexity: O(1)
Auxiliary Space: O(1)

Example 2:

## C

 `// C Program to Demonstrate` `// Comma as operator` `#include `   `int` `main()` `{` `    ``int` `x = 10;`   `    ``// Using Comma as operator` `    ``int` `y = (x++, ++x);`   `    ``printf``(``"%d"``, y);` `    ``getchar``();` `    ``return` `0;` `}`

Output

`12`

Time Complexity: O(1)
Auxiliary Space: O(1)

In the above examples, we have enclosed all the operands inside the parenthesis. The reason for this is the operator precedence of the assignment operator over comma as explained below.

### Property of Precedence in Comma as an Operator

Consider the following expression:

```x = 12, 20, 24;

// Evaluation is as follows
(((a = 12), 20), 24);```

The main reason is that the assignment operator has high precedence over the comma operator.

Example:

## C

 `// C program to demonstrate the ` `// use of comma as an operator` `#include `   `int` `main()` `{` `    ``// using comma without parenthesis` `    ``int` `x = 12, 20, 24;`   `    ``printf``(``"%d"``, x);` `    ``return` `0;` `}`

Output

```error: expected identifier or '(' before numeric constant
int x = 12, 20, 24;
^```

## 2. Comma as a Separator

A comma as a separator is used to separate multiple variables in a variable declaration, and multiple arguments in a function call. It is the most common use of comma operator in C.

```// comma as a separator
int a = 1, b = 2;
void fun(x, y);```

The use of a comma as a separator should not be confused with the use of an operator. For example, in the below statement, f1() and f2() can be called in any order.

```// Comma acts as a separator here
// and doesn't enforce any sequence.
// Therefore, either f1() or f2()
// can be called first

void fun(f1(), f2());```

Example 1:

## C

 `// C Program to Demonstrate` `// comma as separator and ` `// as operator` `#include `   `int` `main()` `{` `    ``// Comma separating x=10 and y` `    ``int` `x = 10, y;`   `    ``// Comma acting as operator` `    ``// y= (x+2) and x=(x+3)` `    ``y = (x++, ``printf``(``"x = %d\n"``, x), ++x,` `         ``printf``(``"x = %d\n"``, x), x++);`   `    ``// Note that last expression is evaluated` `    ``// but side effect is not updated to y` `    ``printf``(``"y = %d\n"``, y);` `    ``printf``(``"x = %d\n"``, x);`   `    ``return` `0;` `}`

Output

```x = 11
x = 12
y = 12
x = 13```

Time Complexity: O(1)
Auxiliary Space: O(1)

Example 2:

## C

 `// C Program to` `// demonstrate Use of` `// comma as a separator`   `#include `   `int` `main()` `{`   `    ``// i=1 and j=2 are initialized` `    ``// uses comma as separator` `    ``for` `(``int` `i = 1, j = 2; i < 10 && j < 10; i++) {`   `        ``if` `(i == 5) {` `            ``// using comma as separator` `            ``i = 6, j = 10;` `        ``}`   `        ``printf``(``"%d %d\n"``, i, j);` `    ``}`   `    ``return` `0;` `}`

Output

```1 2
2 2
3 2
4 2
6 10```

Time Complexity: O(1)
Auxiliary Space: O(1)

## 3. Comma Operator in Place of a Semicolon

We know that in C, every statement is terminated with a semicolon but the comma operator is also used to terminate the statement after satisfying the following rules.

• The variable declaration statements must be terminated with a semicolon.
• The comma operator can terminate the statements after the declaration statement.
• The last statement of the program must be terminated by a semicolon.

Example:

## C

 `// C Program to illustrate` `// the use of comma operator ` `// in the place of semicolon`   `#include `   `int` `main(``void``)` `{` `    ``printf``(``"First Line\n"``),` `    ``printf``(``"Second Line\n"``),` `    ``printf``(``"Third Line\n"``),` `    ``printf``(``"Last line"``);` `    ``return` `(0);` `}`

Output

```First Line
Second Line
Third Line
Last line```

Time Complexity: O(1)
Auxiliary Space: O(1)

## Comma as a Separator vs Comma as an Operator

There is a slight difference between a comma as a separator and a comma as an operator. Let us observe using an example:

```// Wrong Method
// Comma acts as separator during initialization
// Will generate error
int a = 4, 3;

// Correct Method
// Comma acts as operator
int a;
a = 4,3;```

Now the value stored in a will be 3. Also, the following is valid,

`int a =(4, 3);     // value of a is 3`