Modern Electronics (was Re: List charter mods & headcount... ;

From: Sean 'Captain Napalm' Conner <spc_at_conman.org>
Date: Wed Jun 23 20:00:16 2004

It was thus said that the Great Fred Cisin once stated:
>
> On Wed, 23 Jun 2004, Jules Richardson wrote:
> > > int N = 10;
> > > while (N--) printf("%d\n",N);
> > > What are the first and last numbers displayed?
> > > is appropriate for open-book.
> >
> > Hmm, personally I wouldn't want to hire anyone saying they knew a
> > language if that was typical of the sort of thing they'd been asked in
> > an exam where they also had the reference material handy! Any student
> > should be able to say what the result is without having to resort to any
> > kind of reference.
>
> Sure, for somebody who says that they know a language,
> coming to you for a job.
> But for a student part way through the course (MID-TERM!), it is
> a simple test for whether they can apply the order of precedence
> rules.

  Precedence rules I can look up. If *I'm* in doubt, I'll add parentheses
to make my intent clear.

> This is NOT an employment exam!
> It is from a mid-term exam, one of 50 questions to be done
> in 2 hours, intended to find out which students
> can APPLY precedence rules (and wake them up!).
> VIRTUALLY anybody who has to look it up, will almost certainly
> get it wrong, since the book(s) may make it quite clear about
> when the decrement is applied relative to the conditional test,
> but will NOT tell them about whether the decrement is applied
> before or after the BODY of the loop. If they UNDERSTAND,
> then they will get it right.

  I've been working with C for over ten years and *I* got it wrong, which I
can understand because the compiler is free to insert the increment anywhere
between sequence points (I guess I didn't realize there was a sequence point
in the middle there). I thought I used that form, but in going over my
code, it looks like I've actually avoided that particular construct (while
(var--) ... )in my code (heck, I'm looking over 10 year old code and I can't
find that particular form).

Another example to be wary of:

        i = 1;
        printf("%d %d %d\n",i++,i++,i++);

  I've seen different results from different compilers, all ANSI C
compliant (GCC does what people expect, while the IRIX compiler followed the
letter, if not the spirit, of the standard).

> In fact, I'd be willing to bet that 10% of the folks on this
> list who claim to know C would get it wrong! And several of
> those would argue about the answer without trying it.
> (if N were to be declared "unsigned", even more would get it wrong!)

  Really? Once you realize that C treats the value of 0 as false, and
anything else as true, then there isn't any difference between using a
signed or unsigned int in the loop (okay, I cheated, I ran the fragment of
code both ways---that's how I know I'm wrong).

  But if we're going to go into gotchas of C, what's wrong with this code?

        void foo(char *s)
        {
          int charcount[256];
          
          memset(charcount,0,sizeof(charcount));

          for ( ; *s ; s++)
          {
            charcount[*s]++;
          }
        }

  -spc (And no, not being Unicode savvy isn't one of the problems I'm
        looking for ... )
Received on Wed Jun 23 2004 - 20:00:16 BST

This archive was generated by hypermail 2.3.0 : Fri Oct 10 2014 - 23:37:00 BST