22

I had this problem when I accidentally deleted the method name. The code went from

bool bRet = MethodName(pData, pOutFilename);

to

bool bRet = (pData, pOutFilename);

but still compiled? What does this code do? What does it mean? It seems to return true, is this always the case (even if pData is null)?

Any ideas are welcome!

Danny Birch
  • 603
  • 4
  • 16

3 Answers3

37

it is the "comma operator" which

evaluates its first operand and discards the result, and then evaluates the second operand and returns this value (and type).

Stefano Falasca
  • 8,837
  • 2
  • 18
  • 24
  • 3
    For a “sort of practical” use, `if (condition) return fprintf(stderr, "Debug message\n"), FALSE;` If you want to temporarily add a debug message without also having to add braces. – Robert Fisher Jul 22 '13 at 15:45
  • @RobertFisher interesting.., it can also be `i = DEBUG_VALUE, j;` – Grijesh Chauhan Jul 22 '13 at 16:39
  • It's worth mentioning that the value of `pOutFilename` is implicitly converted to `bool`. Assuming `pOutFilename` is a pointer, the result is `false` if `pOutFilename` is a null pointer, `true` otherwise. – Keith Thompson Jul 22 '13 at 20:09
17

Your expression bool bRet = (pData, pOutFilename); is a valid expression, and it's equivalent to the expression bool bRet = pOutFilename;

In bool bRet = (pData, pOutFilename);, first expression pData is evaluated, then the second expression pOutFilename is evaluated, then value of second expression is assigned to bRet (this is how , operator works from left-to-right).

Read: Comma Operator: ,

The comma operator , has left-to-right associativity. Two expressions separated by a comma are evaluated left to right. The left operand is always evaluated, and all side effects are completed before the right operand is evaluated.

To understand the importance of parenthesis ( ) in your expression, consider my example below. Observe the output in this example (I have C example):

int main () {
   int i = 10, b = 20, c= 30;
   i = b, c;   // i = b
   printf("%i\n", i);

   i = (b, c); // i = c
   printf("%i\n", i);
}

output:

20
30

To understand the output: look at precedence table , have lower precedence than =. In you expression you have overwrite the precedence using parenthesis.

cmaster - reinstate monica
  • 38,891
  • 9
  • 62
  • 106
Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
  • 6
    Except if `pData` has side effects, I guess. – Joey Jul 22 '13 at 10:29
  • which it could have only if it were a macro – Stefano Falasca Jul 22 '13 at 10:30
  • @StefanoFalasca If `pData` is uninitialized, it is possible to interpret the various applicable standards as meaning that `pData` in `pData, pOutFilename` is sometimes undefined behavior. Something may also happen if `pData` is a floating-point variable containing a signaling NaN, but I don't know much about that. – Pascal Cuoq Jul 22 '13 at 10:37
4

Its a , comma operator. If you have an expression like this:

i = (a, b);        

b will be stored into i .

So in your case:

bRet = pOutFilename;

pOutFilename will be stored into bRet.

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
Santhosh Pai
  • 2,535
  • 8
  • 28
  • 49