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

Run
#include <stdio.h>  // header files

int main()
{
int var;
int num;

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

Output:

50

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription