0

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...???

user980411
  • 1,179
  • 3
  • 12
  • 18

3 Answers3

4

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.

Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249
  • The question was about C, not C++. –  Nov 20 '11 at 02:27
  • @WTP whoops, I think I fixed it. Does everything still apply now? – Seth Carnegie Nov 20 '11 at 02:28
  • It's not just that the subtraction must be done left to right, it's also that arithmetic operators of the same precedence level must be logically evaluated left to right. The reason for that is default promotions of operands. The sum `1+UINT_MAX+1.0` will differ from `1+1.0+UINT_MAX`. The first sum evaluates as `(1+UINT_MAX)+1.0` and equals `1.0`. The second evaluates as `(1+1.0)+UINT_MAX` and equals the double of `2+UINT_MAX`. If ints are 16-bit, you get `1.0` and `65537.0` respectively. If ints are 32-bit, you get `1.0` and `4294967297.0`. Try with your compiler. – Alexey Frunze Nov 20 '11 at 02:46
  • 1
    @user980411 I changed "In C++" to "In C" and removed "with intrinsic types" – Seth Carnegie Nov 20 '11 at 02:47
  • @Alex I think I indicated that in my answer with the line "when you have ambiguities from multiple operators of the same precedence" but if it's ambiguous, let me know. – Seth Carnegie Nov 20 '11 at 02:48
  • @SethCarnegie: Missed it. Should serve as a good example nonetheless. – Alexey Frunze Nov 20 '11 at 02:49
3

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.

asaelr
  • 5,438
  • 1
  • 16
  • 22
0

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.

Robert Martin
  • 16,759
  • 15
  • 61
  • 87
  • Associativity of NOT is given as right to left in http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Operator_precedence. That means in case of two operators having the same precedence, the rightmost will be executed first...? – user980411 Nov 20 '11 at 02:26
  • 1
    @user980411 yes but only if they are not seperated by operators of different precedence does associativity matter (I think). I don't know where the associativity of a unary operator could matter. – Seth Carnegie Nov 20 '11 at 02:29
  • @user980411 right-associative means the `not` operator associates with the item on its right. That's why we write `!false` instead of `false!`. It doesn't mean `!a + !b` must have the `b` evaluated before the `a`. – Robert Martin Nov 20 '11 at 02:34
  • @RobertMartin are you sure that's what right-associative means? For instance, the `=` operator is also right-associative. – Seth Carnegie Nov 20 '11 at 02:40
  • What I said is only true for unary operators. I'll update my answer to include right-associative for binary operators. – Robert Martin Nov 20 '11 at 05:42
  • Just emphasizing something RobertMartin says in passing: associativity and evaluation order are completely different concepts; knowing what the associativity is doesn't tell you what the evaluation order is. In fact, many times the associativity will be exactly defined but the eval order won't be. See http://stackoverflow.com/questions/2395423 (and many other questions about sequence points). – dubiousjim May 31 '12 at 19:15