Re: Brian Kernighan, maybe I'm not worthy, maybe I'm scum



On Mon, 31 Dec 2007 11:52:29 -0800, spinoza1111 wrote:

On Dec 31 2007, 10:00 pm, Richard Heathfield <r...@xxxxxxxxxxxxxxx>

Question 8 is broken. All of the suggested answers except A exhibit
undefined behviour, and answer A modifies the value of x. There is no
correct answer.

(A) x++;
(B) x += x--;
(C) x = x++;
(D) x = --x + 1;

The correct answer is D, and neither it nor any question exhibits
undefined behavior in C++, or in usable C compilers.

A adds one to the value of x and returns the value before the increment
to thin air. x is changed.

In B, x is stacked and then decremented in place. The stack copy is then
added to x. The decrement happens BEFORE the assignment (simple operator
precedence). Therefore x is set to (x-1)+x, where x is the starter
value. The behavior is NOT undefined, as far as I can tell. Please
inform me of why you believe otherwise. We can safely assume x is long
or int. x is changed. If someone comes up with a value, int or float or
double in which it doesn't change, this still makes this the wrong
answer because in D (see below) x is unchanged.

By the same reasoning as for B, x is set in C to (x+1)+x.

D decrements x, stacks it, adds one, and then replaces decremented x by
(x-1)+1. No change except perhaps for pathological cases near the end of
floating point precision.

Richard, am I missing something? The value is well-defined as long as
you commonsensically assume x to be int. The BEHAVIOR is well-defined,
period.

Yes you are missing something: an assignment statement does *not*
introduce a sequence point. As I learned today: not even in C++.


Question 9 is trivial.

Question 10 fails to account for pointer comparison, but is otherwise
trivial.

In C and C++, pointers are integers and as such compareable

Not without a cast. And even with a cast, results are questionable.


Question 14 is ambiguous - it depends on whether you consider 'between'
to be inclusive. I don't, so I might disagree with the marker over
which answer should be considered correct.

Question 14 does NOT depend on whether the English word "between" is
inclusive. In ordinary speech it is used both ways! But the competing
answer uses an OR (hope you noticed) and the best answer is A. The ONLY
answer is the BEST answer.


This quiz is not supposed to be about English semantics. Ambiguity could
(and should) be avoided, when possible. FWIW: I am not a native speaker/
reader. I don't know _ordinary speech_

HTH,
AvK

.



Relevant Pages