>>>>> Marin Ramesa <m...@hi.t-com.hr> writes: >>>>> On 18.12.2013 10:46:40, Richard Braun wrote:
[...] >> No, that's wrong. The && and || operators are guaranteed to be >> evaluated left-to-right, and yield if the first operand compares >> equal to 0. And that's exactly why this check against NULL is done >> first. > In the expression (!a && !b), if !a equals 0, the compiler must check > !b == 0 in order to return [FALSE]. If !a equals 0, that means the > entry is a null pointer, and evaluation of !b is a dereference of a > null pointer. Well, when explaining these operators to the students, I’d rather call them “conditionals” (along with A ? B : C and ‘if’.) Specifically: • the A && B operator evaluates the A expression; if the result is zero, zero is returned; if the result is non-zero, the B expression is evaluated, and its result is returned; • the A || B operator evaluates the A expression; if the result is non-zero, it is returned; if the result is zero, the B expression is evaluated, and its result is returned. Essentially, A && B is equivalent to (A ? B : 0), while A || B is equivalent to (A ? A : B), except that in latter case, A is evaluated only once. (Thus A++ || B doesn’t result in A being incremented twice, as would be in the case of A++ ? A++ : B.) One may wish to check the following simplictic C code. #include <stdio.h> int main () { int a = 0, b = 42; int c = a && puts ("a is true"); int d = a || puts ("a is false"); int e = b && puts ("b is true"); int f = b || puts ("b is false"); /* . */ return 0; } Note that only some of the puts () calls are in fact evaluated. -- FSF associate member #7257