0

So, let's say, I have this piece of code:

int value = 256;
if( check_value_1(value) && check_value_2(value) )
{
    // .. do stuff ..
}

If function check_value_1() returns FALSE will check_value_2() be called?

Rob Avery IV
  • 3,562
  • 10
  • 48
  • 72
  • 5
    How long did you spend searching for an answer to this question? – Kerrek SB Jun 29 '16 at 21:50
  • Consult your trusty K&R book. If you don't have one, get it. It's the best programming book I ever read, and it's short. – yellowantphil Jun 29 '16 at 21:51
  • 1
    How difficult is this for you to **test this out yourself**? – Amit Jun 29 '16 at 21:51
  • in case you don't know what to google: `short circuit evaluation in c` – Bryan Chen Jun 29 '16 at 21:52
  • @Amit He doesn't even need to do that. You never know if your compiler is just being lazy, but this is specified in the standard. – yellowantphil Jun 29 '16 at 21:52
  • @yellowantphil - ha?? lazy compiler?? let me quote (your answer): "*this is specified in the standard*" - so.. a standard compliant compiler will do what it should – Amit Jun 29 '16 at 21:53
  • 4
    @Amit My point is that if you don't read the standard but just try it in your compiler, you don't know if your compiler is doing its own optimization or following the standard. Just trying it in your compiler is not enough. – yellowantphil Jun 29 '16 at 21:54
  • 1
    Although many will say `No`, I believe the answer is deeper. If the compiler can determine that the second function has no impact then it could be called, but since it has no impact on the _state_ of the code, it make no difference, This allows certain optimizations so both halves can evaluate in parallel or in either sequence. But this is a very limited optimization as the compiler must be able to determine the 2nd half does not change the code _state_. – chux - Reinstate Monica Jun 29 '16 at 21:56
  • @yellowantphil - if you use a stable version of a standard compiler, it does what the standard says - that's as good as reading anything, but simpler. – Amit Jun 29 '16 at 21:56
  • 1
    @yellowantphil: Exactly. It's easy to imagine a C-like language in which the right operand of `&&` *may or may not* be evaluated if the LHS is false. Testing will not tell the OP that C is not such a language. Similarly, testing can't tell you that `printf("%d %d\n", i++, i++)` has undefined behavior. – Keith Thompson Jun 29 '16 at 21:57
  • @chux Why should a function with no impact called? Just because it won't be noticed? – Eugene Sh. Jun 29 '16 at 21:58
  • @ Eugene Sh. Consider `int a,b,c,d; ... a = b + c; if (d || a+1)...` Evaluation of `a+1` may happen before `d` is read as the previous line of code has `a` at the ready. Some compilers well understand the effect of library functions and can do this at a functional level. – chux - Reinstate Monica Jun 29 '16 at 22:01
  • @Amit people following this philosophy are coming here with question like "why ` i++ + i++` return `x` and not `y` as expected". – Eugene Sh. Jun 29 '16 at 22:02
  • 1
    @chux OK, got it. Even simpler example would be if the second operand is a constant expression. – Eugene Sh. Jun 29 '16 at 22:04
  • @Eugene Sh. Unfortunately I have not found in the C spec that words that allow this apparent violation of C11, 6.5.13. Compiler optimizations and processor pipelining are very tricky these days. – chux - Reinstate Monica Jun 29 '16 at 22:12
  • @EugeneSh. - Asking *why* something happens is entirely different from asking *what will* happen. If you want to know what will happen, test it. If you want to know why that happened, read or ask. If you suspect 2 "simple" function calls on either sides of an operator might trigger UB, then you probably never saw a single program, but read or ask. – Amit Jun 29 '16 at 22:27

3 Answers3

1

The short answer: no. uses short circuit evaluation, so if the left hand argument of an && operator is FALSE, it's obvious there's no way the entire expression can be anything but FALSE, and the right hand argument isn't even evaluated.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
1

If check_value_1(value) returns FALSE, the second portion of the if-statement will be skipped and will not execute. This is known as short circuit evaluation.

Karl Nicoll
  • 16,090
  • 3
  • 51
  • 65
1

No, C does a lazy evaluation and return FALSE.

read more about it https://en.wikipedia.org/wiki/Short-circuit_evaluation

printfmyname
  • 983
  • 15
  • 30