Pergunta

i) What does if(0) mean?

Everytime I use it to test what output i will get, it returns the false part.

Is it equivalent to if(0 == 0), incase of which the true part is evaluated.

ii) Associativity of logical NOT ! is right to left.

Link: http://www.liv.ac.uk/HPC/HTMLF90Course/HTMLF90CourseNotesnode94.html

The second example in the link of logical operators:

But as per the line "the two subexpressions containing the monadic .NOT. are effectively evaluated first, as there are two of these the leftmost, .NOT.A is done first followed by .NOT.E.", the left NOT is evaluated first, but the first one to be evaluated should be the one on the right...???

Foi útil?

Solução

I) In C, 0 is false and everything else is true. So with if (0), the condition will always be false and the body will never be executed because 0 is always false.

if (0 == 0) is completely different because 0 does in fact equal zero, and the expression 0 == 0 evaluates to true, so the body of the if is executed.

II) The associativity of operators determines what happens when you have ambiguities from multiple operators of the same precedence. For instance, what should happen in a - b - c? Should the b - c be evaluated first or the a - b? It does matter what order you do them in, because if a = 1, b = 2, and c = 3, a - (b - c) is 2, but (a - b) - c is -4. But because subtraction is left-associative, we can know that a - b will be evaluated first, so the answer to a - b - c is -4 when a = 1, b = 2, c = 3.

All that being said, I can't think of a case where the associativity of the logical not operator would matter, and the associativity of an operator does not determine what order it will be executed in when it is seperated by operators of different precedence.

Outras dicas

i) in C, 0 mean false, so if(0) will always jump to the else (if there). it is the opposite of if(0==0), (or simply if(1)), which will do the true part.

if (0) evaluates the predicate 0 as a binary value. Binary values in C use integers, where zero means false and non-zero means true. Therefore, 0 will always evaluate to false.

For binary operators, being right- or left-associative determines the order that otherwise equally important operators will be processed. Consider the subtraction operator:

37 - 10 - 4

Both - are equal precedence so which should we evaluate first? Well, - is left-associative so we do:

(37 - 10) - 4 ==> (27) - 4 ==> 23

If the - operator were right-associative, we would do:

37 - (10 - 4) ==> 37 - 6 ==> 31

Equality (=) is right-associative because we might chain equalities together. So, if we see

// a and b are initialized to 0
a = b = 45

Both = are equal precedence so we evaluate right to left and do:

a = (b = 45) // b receives 45
a = 45       // a receives 45

If we were to go left-right, we'd get an unexpected result:

(a = b) = 45 // a receives 0
b = 45       // b receives 45

For unary operators, however, ordering can only matter when multiple unary operators affect the same value. For example, let's do:

char x = 0xFF
bool y = !~x

These unary operators are right-associative, so we do:

!(~0xFF) ==> !(0x0) ==> true

In the example you showed, the negation operators affecting A and E didn't have "equal precedence" because they didn't have the same operand. So, associativity doesn't apply.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top