Steve's list of good "C" questions to ask programming candidates:

1. What's the difference between i++ and ++i?

2. How does sizeof() determine the size of a structure?  How about a union?

3. Suppose x is of type "struct list".  What expression yields the value of x's
   next field?

4. What fields would a doubly linked list structure have?

5. Why does getchar() return an int instead of a char?

6. Suppose the statement: x = *y; is in your program.  What is most likely to
   happen if the value of y is NULL when your program gets to this statement?

7. a. Suppose you defined the function swap(x, y) where x and y are declared as
      integers.  Why won't this work?

   b. What change is needed to make it work?

8. What's wrong with returning a locally declared array from a function?

9. How do you declare a variable named x to be of type "pointer to function
   returning pointer to char".

10. What's the difference between a declaration and a definition of a variable?

11. Why shouldn't you put definitions of variables (as opposed to declarations)
    in your .h file? (assume you include your .h file in more than one .c file).

12. Suppose you defined x as an integer array of size 10 and somewhere in your
    program you have the statement: x[10] = y;.  What are some of the symptoms
    this runtime error might cause?

13. a. Which of the following loops will (in general) run faster?

       Loop1: for (i = 0; i < 10; i++) x[i]++;

       Loop2: for (p = x; p < x+10; p++) (*p)++;

    b. How would you improve the speed of Loop2?

14. How would you make the following routine do breadth first search?

  void depth_first_search(v)
  int v;
  {
    int w, x;
    STACK stack;
    BOOLEAN *visited;

    /* open stack and initialize visited array */
    stack = stack_open();
    visited = (BOOLEAN *)malloc(MAXSIZE*sizeof(BOOLEAN));
    for (x = 0; x < MAXSIZE; x++) visited[x] = FALSE;

    /* initialize stack with v */
    STACK_PUSH(stack, v);
    visited[v] = TRUE;

    while (STACK_NOT_EMPTY(stack)) {
      w = STACK_POP(stack);

      for (list = CHILDREN(w); list != NULL; list = list->next) {
        x = list->object;

        if (!visited[x]) {
          STACK_PUSH(stack, x);
          visited[x] = TRUE;
        }
      }
    }

    /* close stack and free visited array */
    stack = stack_close(stack);
    free(visited);

    return;
  }


Answers:
1. Both increment i.  The value of ++i is the value of i AFTER incrementing,
   whereas the value of i++ is the value of i BEFORE incrementing.

2. The size of a structure is the sum of the sizes of its components.  The size
   of a union is the maximum of the sizes of its components.

3. x.next   (x->next is used when x is a POINTER to a struct list)

4. A doubly linked list would have at least a next pointer and a previous
   pointer, and at least one other field to store the objects in the list.

5. getchar() returns EOF at end-of-file.  EOF is always defined as a negative
   integer to distinguish it from ALL 8-bit characters.  Although 8 bits which
   are all 1's is -1 AS A CHARACTER, some machines will not sign-extend this
   when it converts it to an integer.  Therefore your comparison to EOF to
   detect end-of-file will never succeed, leaving your program in an infinite
   loop.

6. Your program will core dump unless you have trapped the SIGSEGV signal.

7. a. In C, function parameters are passed by value, and so even though the
      values of the formal parameters of the function will really be swapped,
      there will be no effect on the actual parameters to the function.

   b. Make x and y POINTERS to integers.

8. C passes arrays by reference.  The space allocated for a locally declared
   array ceases to exist after returning from the function (the space is
   allocated on the stack, and the stack is popped before returning).  The
   pointer to the array is still able to be dereferenced, but the data it
   points to may be garbage.

9. char *(*x)();

10. A declaration merely states that a variable exists somewhere and it has the
    indicated type.  A definition states that a variable has the indicated type
    and allocates space for the variable.

11. If your .h file is included in more than one .c file, you will have more
    than one address at which space has been allocated for each global variable.
    Each .c file will refer to a different set of global variables.  (Note: some
    C compilers are smart enough to figure this out, and correct for your error)

12. You may be writing over other data elsewhere in your program, so the effect
    would be anything from your program not outputing the correct results to
    dumping core.

13. a. In general, pointer accesses are faster than array accesses, i.e. Loop2
       is faster.

    b. for (p1 = x, p2 = x+10; p1 < p2; p1++) (*p1)++;
       /* the +10 is done only once */
--
Steve

PS I hope these help you
