2

I was looking on some examples and I don't know what this means:

if(FileExistsA("File.ext"), false)
{
     ....
}

Can someone explain me this please?

Quest
  • 2,764
  • 1
  • 22
  • 44
  • It has no special meaning in `if`, it's the same as its meaning in any other expression. – Barmar Feb 01 '14 at 12:56
  • 3
    Most likely this was done to "comment out" the `if` statement without removing the call to FileExistsA or significantly altering the formatting of the source. I think most folks would have used `&& false` instead, as it's a bit more obvious, and a more conscientious programmer would have added some sort of comment (with attention-getting flags) to indicate what had been done. – Hot Licks Feb 01 '14 at 13:16

1 Answers1

7

In C and C++ (but not C# or Java), the comma operator ',' evaluates both the left and right expressions, but only returns the right expression.

In this example:

bool x = (true, false);
// x == false

bool y = (false, false, true)
// y == true

In your case, if( FileExistsA("File.ext"), false ) will never follow its branch because the comma operator ensures that false is the result.

Update I forgot about the precedence of = and ,. I've wrapped the expressions above in parenthesis to prevent the expression from being evaluated as (bool x = true), false === false.

Dai
  • 141,631
  • 28
  • 261
  • 374
  • 1
    Rather stupid abuse of the operator in the OPs example, though. Better not use it... – PMF Feb 01 '14 at 12:57
  • 2
    And may be `, fasle` was added during during debugging, or to fix some bug – Grijesh Chauhan Feb 01 '14 at 13:01
  • +1 I can't accept your answer for now ... 8 min left. but who wants to use this stupid thing? – Quest Feb 01 '14 at 13:01
  • @Dai, branch can be executed but if before it we have line: #define false true :) – αλεχολυτ Feb 01 '14 at 13:02
  • 1
    @Quest you may like to read this [What is the proper use of the comma operator?](http://stackoverflow.com/questions/17902992/what-is-the-proper-use-of-the-comma-operator) – Grijesh Chauhan Feb 01 '14 at 13:04
  • *" ... but only returns the right expression."*. Not necessarily; it could return anything depending on the types of the operands. In case of built-in types, it returns the rightmost expression, but for user-defined types, the return value depends on how `,` is implemented. – Nawaz Feb 01 '14 at 13:07
  • @dai I removed my upvote for time being .. correct comment In `bool x = true, false;` `x` would be `true` *not* `false`, `x = (true, false);`give `false` read [my answer here](http://stackoverflow.com/a/17785232/1673391) correct it I will up-vote again. – Grijesh Chauhan Feb 01 '14 at 13:08
  • -1. Also, your examples are wrong. Please verify the results and your claim! – Nawaz Feb 01 '14 at 13:09
  • @GrijeshChauhan I've corrected my answer, thank you for pointing out the error. – Dai Feb 02 '14 at 05:46
  • @Dai upvoted but still I'm not at all convinced by your statement `",' evaluates both the left and right expressions."` – Grijesh Chauhan Feb 02 '14 at 08:43
  • @GrijeshChauhan consider `x = ( Foo(), Bar() );` with both `int Foo() { return 1; } int Bar() { return 2; }`. If you set a debugger breakpoint in both functions, you'll see both are called. – Dai Feb 02 '14 at 11:48