1

Extending the question What happens if the first part of an if-structure is false?

for C programming

if((NULL!=b)&&(query(&b)))

In the above code will the part "query(&b)" is executed if b=Null? And is the behavior consistent across compilers?

Community
  • 1
  • 1
Ginu Jacob
  • 1,588
  • 2
  • 19
  • 35
  • Look at this: http://stackoverflow.com/questions/3958864/c-is-there-lazy-evaluation-when-using-operator-as-in-c. In a word: the operator will short-circuit and it is standard C behavior. – wap26 Mar 10 '14 at 10:56
  • 1
    The language used in this question is inaccurate and ambiguous. I would have assumed that the "second part" of an if *construct* or *statement* (not *structure*) was the `else` part. Here you are merely talking about the *left-hand side of a boolean operation* - the `if` construct is irrelevant. – Clifford Mar 10 '14 at 11:04
  • 1
    Thanks for accepting my answer, but the credit should really go to someone else (like paxdiablo who posted first) as I merely wanted to add a reference for clarity and others put a lot more time into their answers. – jpw Mar 10 '14 at 11:12
  • 1
    @jpw, the credit should go to the _best_ answer, not the first. It's just unfortunate that I got called away before getting to the standards doc. The best answer for language questions like this is almost _always_ the one that quotes the standard, so yours is well deserved. In any case, it's not like I need the rep :-) – paxdiablo Mar 10 '14 at 13:36
  • @paxdiablo +1 to you :) – jpw Mar 10 '14 at 13:42

4 Answers4

4

No, logical operators in C will short-circuit.

If you attempt to evaluate a && b when a is false, b is not evaluated.

But keep in mind that you're not checking b with that original statement, rather you're checking a. Also note that it's safe to take the address of a variable that's NULL, you just can't dereference it.

I suspect what you should be writing is:

if ((b != NULL) && (query (*b)) ...
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
2

To add to the correct answers already posted this is what the C99 standard says (section 6.5.13 Logical AND operator, paragraph 4):

Unlike the bitwise binary & operator, the && operator guarantees left-to-right evaluation; there is a sequence point after the evaluation of the first operand. If the first operand compares equal to 0, the second operand is not evaluated.

jpw
  • 44,361
  • 6
  • 66
  • 86
1

In the above code will the part "query(&b)" is executed if b=Null?

Simple answer NO. It will not be executed. && is short circuited. So if you try to do so, this will break the short circuit evaluation, and is therefore not reccomended.

Check FAQ3.6

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
1

It is not specific to the if statement, but rather to boolean expressions in general.

In a && b, b is not evaluated if a is false.

Similarly in c || d, d is not evaluated if c is true.

This is known as short-circuit evaluation and is part of the definition of the language, so will be consistent across compliant compilers.

Clifford
  • 88,407
  • 13
  • 85
  • 165