C : is there “lazy evaluation” when using && operator, as in C++?

前端 未结 4 1547
后悔当初
后悔当初 2020-12-03 21:20

I would like to know if this looks correct :

while((next !=NULL) && (strcmp(next->name, some_string) < 0) {
    //some process
}
相关标签:
4条回答
  • 2020-12-03 21:48

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

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

    0 讨论(0)
  • 2020-12-03 21:50

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

    0 讨论(0)
  • 2020-12-03 21:57

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

    0 讨论(0)
  • 2020-12-03 22:01

    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.

    0 讨论(0)
提交回复
热议问题