I need to check if the node->next != NULL
and I do not know if I can write:
if(node!= NULL && node->next!= NULL)
I need to check if the node->next != NULL
and I do not know if I can write:
if(node!= NULL && node->next!= NULL)
Yes, this is a guaranteed behaviour of C/C++, as nicely described in this question.
Just as an addition: In C/C++, this is refered to as short circuit evaluation, not lazy evaluation.
This has got nothing to do with GCC; the evaluation order and the short-circuiting is guaranteed by the language standard. Here's the relevant part from the C++11 standard, draft N3337 (emphasis mine):
5.14 Logical AND operator
- The && operator groups left-to-right. The operands are both contextually converted to type bool. The result is true if both operands are true and false otherwise. Unlike &, && guarantees left-to-right evaluation: the second operand is not evaluated if the first operand is false.
- The result is a bool. If the second expression is evaluated, every value computation and side effect associated with the first expression is sequenced before every value computation and side effect associated with the second expression.
5.15 Logical OR Operator
- The || operator groups left-to-right. The operands are both contextually converted to bool. It returns true if either of its operands is true, and false otherwise. Unlike |, || guarantees left-to-right evaluation; moreover, the second operand is not evaluated if the first operand evaluates to true.
- The result is a bool. If the second expression is evaluated, every value computation and side effect associated with the first expression is sequenced before every value computation and side effect associated with the second expression.
Any standards-conformant compiler should do so. So, yes, GCC does so too.