Comma Operator in C

Comma Operator

Comma Operator has Lowest Precedence i.e it is having lowest priority so it is evaluated at last.

The comma operator (also called  token, ) is a binary operator that works on first operand and discards the result, it then evaluates the second operand and returns their value (and type)

comma operator in C

Comma Operator

First of all we know already Comma operator can be used as a “separator” .

eg; int a=3, b=4, c=7 is nothing but multiple definition a in single line. we can  also write,

int a=3;                  

int b=4;

int c=7;

but is in that combination.

Comma operator can be used as a operator, eg;

int a =(3,4,8);

printf(“%d”,a);

Comma operator returns the right most operand in the expression and it simple evaluates the rest of the operands and finally reject them.

Comma operator is having least precedence among all the operators available in c language.

Symbol of Comma Operator is (,)

Implementation of Comma operator

Comma Operator has Lowest Precedence i.e it is having lowest priority so it is evaluated at last.

So Firstly ‘=’ operator priority is high,then initially var =15 was evaluated and the second operand becomes var+35 == 15+35

which results 50. now 50 will be printed.

Code for Comma operator in C

#include<stdio.h>

int main()
{
int var;
int num;

num= (var=15, var+35);
printf("%d",num);
}


Output
50