C quiz questions/answers?

Kevin Buettner plug-devel@lists.PLUG.phoenix.az.us
Mon Nov 5 23:06:02 2001


On Nov 5,  3:17pm, Rob Wehrli wrote:

> I've been writing an C "test" and thought that I'd ask the list for any
> good traps/questions they might have...

There's a good book on this topic called "The C Puzzle Book" by
Alan R. Feuer.  (Prentice Hall, 1982)

Here's a problem that demonstrates a gotcha that I ran into a while ago.

Consider the following program:

#include <stdio.h>

long lshift (long, int);

int
main (int argc, char **argv)
{
  long val = 1;
  int shift = 33;

  printf ("%lx << %d = %lx\n", val, shift, lshift (val, shift));

  return (0);
}

long
lshift (long v, int s)
{
  return v << s;
}

What is the output of this program?

    A: 1 << 33 = 0
    B: 1 << 33 = 2
    C: 1 << 33 = 200000000
    D: All  of the above
    E: None of the above

Explain your answer.