Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the comma operator do?

What does the following code do in C/C++?

if (blah(), 5) {     //do something } 
like image 960
Matt Avatar asked Sep 29 '08 16:09

Matt


People also ask

What does comma operator do in C?

The comma operator in c comes with the lowest precedence in the C language. The comma operator is basically a binary operator that initially operates the first available operand, discards the obtained result from it, evaluates the operands present after this, and then returns the result/value accordingly.

What is comma operator in C++ with example?

1) Comma as an operator: The comma operator (represented by the token, ) is a binary operator that evaluates its first operand and discards the result, it then evaluates the second operand and returns this value (and type). The comma operator has the lowest precedence of any C operator, and acts as a sequence point.

How does comma operator work in Python?

On the left-hand side of an assignment, the comma indicates that sequence unpacking should be performed according to the rules you quoted: a will be assigned the first element of the tuple, b the second.


1 Answers

Comma operator is applied and the value 5 is used to determine the conditional's true/false.

It will execute blah() and get something back (presumably), then the comma operator is employed and 5 will be the only thing that is used to determine the true/false value for the expression.


Note that the , operator could be overloaded for the return type of the blah() function (which wasn't specified), making the result non-obvious.

like image 142
itsmatt Avatar answered Oct 02 '22 12:10

itsmatt