In C, comma ( , ) can be used in three 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.
// 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
#include <stdio.h>
int main()
{
int x = 10;
int y = 15;
printf ( "%d" , (x, y));
getchar ();
return 0;
}
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Example 2:
C
#include <stdio.h>
int main()
{
int x = 10;
int y = (x++, ++x);
printf ( "%d" , y);
getchar ();
return 0;
}
|
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
#include <stdio.h>
int main()
{
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
#include <stdio.h>
int main()
{
int x = 10, y;
y = (x++, printf ( "x = %d\n" , x), ++x,
printf ( "x = %d\n" , x), x++);
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
#include <stdio.h>
int main()
{
for ( int i = 1, j = 2; i < 10 && j < 10; i++) {
if (i == 5) {
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
#include <stdio.h>
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
Last Updated :
26 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...