Comma Operator in C
What is a comma operator?
The comma operator in the C programming language has the least priority. The comma operator is essentially a binary operator that operates on the first operand that is available, discards the result that is obtained from it, evaluates the operands that are present after this, and then returns the result/value by those evaluations.
Why comma operator is used?
In the C programming language, the comma symbol primarily serves the dual functions of being an operator and a separator. Thus, depending on where we use it or place it in a program, its behaviour will vary greatly.
C token for the comma (,)
- In the C programming language, the comma (,) is used as both an operator and a separator.
- In variable declarations, function calls, and function definitions, commas serve as separators.
- The comma operator only takes two operands since it is a binary operator.
- The Expressions are separated by a comma operator. Expressions that have been split are assessed from left to right.
Comma as an operator
The comma operator evaluates its first operand in a binary operator, tosses the outcome out, and then evaluates its second operand in a binary operator, returning the result. In either C or C++, the comma operator has the lowest precedence.
When we wish to assign numerous numbers of values to any variable in a program with the aid of a comma, we utilize the comma in the form of an operator.
for example
x = 10, 20, 30, 40, 50, 60;
y = (10, 20, 30, 40, 50, 60);
The variable p will have the value 10 in the first of the aforementioned statements. The reason for this is that the equal to assignment operator (=) has a greater priority than the comma operator (,). As a result, the program will assign the value 10 to the variable x.
The value of y will be comparable to 60 in the second sentence. The reason for this is that the numbers 10, 20, 30, 40, 50, and 60 are wrapped in braces (), which have precedence over the equal to (=) assignment operator. Therefore, the program will treat the right-most value as the result or output of this expression when we supply the variable y with several values (using the comma operator and the braces). As a result, the program variable y will be given the value of 60.
Example code
#include<stdio.h>
int main()
{
int x = (10, 20);
int y = (f1(), f2());
}
In this case, x will receive the value 20. The first function in the following statement, f1(), will be run before the second one.
Example program using comma as an operator
#include <stdio.h>
int main()
{
int x,y;
x = 10,20,30;
y = (10,20,30);
printf(“x= %d, y= %d\n”,x,y);
return 0;
}
Output
x = 10, y= 30
Comma as aseparator
The comma is used as a separator in programs when several variables need to be declared and distinct arguments need to be provided for functions.
For example,
x, y, and z;
The comma serves as a separator and informs the compiler that the x, y, and z variables are three distinct types of variables in the aforementioned statement.
Example code
#include<stdio.h>
int main()
{
int x = 5, y = 10;
void function(x, y);
}
Here, x and y will both be used by the program as function parameters. To learn how to utilize comma operators in the C language, look at the following program.
Example program using comma as a separator
#include<stdio.h>
main()
{
int x = 8;
int y = (x++, ++x);
printf("%d", y);
}
Output
10
Example programs of comma operator in c
Example 1
#include<stdio.h>
int main()
{
int x = 10, y = 20, z ; // comma as a Separator
z = x , y ; // comma as a Operator
printf(“x = %d\n y = %d\n z = %d”,x,y,z);
return 0;
}
Output
x = 10
y = 20
z = 10
The comma operator is used in the programme statement z = x, y; above. z is initially given a value. Z becomes 10 as a result, and y is subsequently ignored.
Example 2
#include<stdio.h>
int main()
{
int x = 10, y = 20, z ; // comma as a Separator
z = (x , y) ; // comma as a Operator
printf(“x = %d\n y = %d\n z = %d”,x,y,z);
return 0;
}
Output
x = 10
y = 20
z = 20
In the above program because of the parenthesis around x and y, c = (x, y), the output of the aforementioned program, z, contains 20, which denotes the value of y;
Example 3
#include <stdio.h>
int main()
{
int x = 4, 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 = 5
x = 6
y = 6
x = 7
Example 4
#include <stdio.h>
int main()
{
int x =4, 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 = 5
x = 4
y = 4
x = 3