18

I would like to know if this looks correct :

while((next !=NULL) && (strcmp(next->name, some_string) < 0) {
    //some process
}

I mean, if next is NULL, then the second part of the expression won't be ever tested by the compiler? I have heard that in C++ it's the case (but I'm not even sure of it).

Can someone confirm me that I won't get strange errors on some compilers with that?

fat
  • 6,435
  • 5
  • 44
  • 70
Pac0
  • 21,465
  • 8
  • 65
  • 74
  • 5
    @Sean: Testing doesn't necessarily equal "guaranteed by the standard". – Oliver Charlesworth Oct 18 '10 at 12:02
  • 4
    Incidentally this not always true in C++. The expression: `(next != NULL)` may use an overloaded `!=` operator on whatever type `next` is. That operator may return another type on which `&&` is overloaded. And for overloaded `&&` there is no built-in short-circuiting, so the expression on the RHS will be evaluated regardless of the LHS. – Daniel Earwicker Oct 18 '10 at 12:08
  • 1
    @pmg: Why is this not lazy evaluation? – codymanix Oct 18 '10 at 12:32
  • 4
    @codymanix: Lazy evaluation is about delaying computation until the point that the results are required. This is short-circuiting, to avoid undefined behaviour (like deferencing a null pointer). – Oliver Charlesworth Oct 18 '10 at 12:58
  • 1
    Hmmm ... looks like some people call that short-circuiting lazy evaluation ( http://www.c2.com/cgi/wiki?LazyEvaluation ). I don't agree: to me lazy evaluation is "get ready to evaluate *something* but don't do it just now"; short-circuiting is "if it's ok do *something* right now, otherwise don't ever do it" – pmg Oct 18 '10 at 13:13
  • @oli: By your definition, short circuiting is lazy evaluation, too. Avoiding access to uninitialized values is just a side effect of it which is often used. – codymanix Oct 19 '10 at 12:34
  • @codymanix: I guess it's a matter of definition. I agree with pmg's definition... – Oliver Charlesworth Oct 19 '10 at 13:30

4 Answers4

21

Yes && is short circuited and you are using it correctly.
If next is NULL string compare will never happen.

codaddict
  • 445,704
  • 82
  • 492
  • 529
10

Yes, in C++ short circuit and and or operators are available.

Here's a question answered in the C-faq on the subject.

Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292
9

It's definitely the case in both C and C++.

Jackson Pope
  • 14,520
  • 6
  • 56
  • 80
  • 1
    Correction: It's definitely true in C, so it should be true in C++. – ruslik Oct 18 '10 at 12:01
  • 2
    @ruslik - that's no correction. C++ and C are covered by standards, and this answer is correct about what both standards say (for built-in types in C++) – Daniel Earwicker Oct 18 '10 at 12:05
3

This will work with lazy evaluation (the second statement not evaluated if the first one is evaluated to "false") unless your compiler is so non-standard compliant it can't even be named a C compiler. Millions lines of code in the field rely on this behavior, so you can think that this behavior is just guaranted.

sharptooth
  • 167,383
  • 100
  • 513
  • 979
  • 5
    it's called short circuit evaluation, [lazy evaluation](https://en.wikipedia.org/wiki/Lazy_evaluation) is something else – sp2danny Oct 16 '17 at 08:07