Comma operator in c

Comma operator in c

The comma operator ( , ) is used to permit different expression or separate difference expression to appear in situation where only one expression will be used. The every expression are separated by the comma operator. And these separated expression are evaluated from left to right.

Let us understand the concept of comma operator with example-

a=8, b=5, c=9, d=10, e=a+b;

in the above example we have combined 5 expressions. in the right or in the initially 8 assign to variable a, then 5 is assign to b, 9 is assign to c, 10 is assign to d, and in the last e=a+b is expression evaluated which assign value of whole expression to the variable c.

result=(a=8, b=5, c=9, d=10, a+b+c+d);
The value of above expression will be 32. Now consider this statement

In the above example whole expression assigned to the right variable result. so the variable result assigned value 32.so we know precedence of comma operator is lower than the assignment operator. so here used of comma operator make the code more compact. so in this example show without the comma operator the above statement have been in five statements

a=8;
b=5;
c=9;
d=10;
result=a+b+c+d;

/* program to understand the use of comma operator*/
#include< stdio.h >
int
main()
{
int a,b,c,d,e,result;
result=(a=8, b=5, c=9, d=10, a+b+c+d);
printf(“result=%d\n”,result);
return 0;
}

output
result = 32