Software Design (C++) - Exercises I, "C Refresher",   (1 - 5),   6 Nov - 7 Nov, 2014


Use available C books and online materials (e.g. en.cppreference.com/w/c) to answer the following questions.

  1. Explain the following items concerning C features. When necessary, give illustrative examples.
     
    1. What is the purpose of a typedef statement in C? Java does not provide typedef's.
    2. Explain what is a C macro? How is it used, and why is it useful? At what stage during compilation are preprocessor directives analyzed?
    3. Since C doesn't define type sizes exactly (as number of bits in computer memory), it can be hard to write portable code (that behaves identically on different platforms). What techniques or ways can we use to achieve portability?

  2. Explain the following items concerning C features:
     
    1. How are primitive arrays and character strings implemented in C?
    2. Assuming that the variables s and t are both pointers to characters, what does the following C statement do? Explain in detail how the processing proceeds:
       
             while (*s++ = *t++);
       
      (Such condensed code represents common idiomatic C usage even if possibly not best programming style.)

  3. Explain the following items concerning C features:
     
    1. How does C manage (check, analyze, or prevent) uninitialized variables?
    2. What does the built-in operation sizeof take as argument? What does it compute? Why is it a necessary feature in C?

  4. Give a C function that allocates memory from the free store (heap) for an array for n integers (of type int) where n is given as an argument to the function. The function returns the allocated memory (or NULL in case of error) to the caller.
    Then, give a program that uses the function to allocate space for an array of 10 integers, inserts the numbers from 1 to 10 into the array (in ascending order), prints out the 3rd element of the array, deallocates the memory, and finally terminates.

    What initial values do the 10 elements in the array have? How could you affect defining/setting the initial values?

  5. Answer the following questions about the compilation of C programs.
     
    1. Explain the difference between a definition and a declaration in C programs. What is indicated by a declaration that uses the extern modifier?
    2. Explain how C (and C++, too) supports separate compilation. Define what a translation unit is. Explain how the #include directive works in C (and C++). Also explain what header files are, and why they are needed in building programs. Explain also, what goes on during linking (hint: look up "Linker (Computing)" from Wikipedia).
    3. Give a short C program that uses multiple translation units (use the program in Exercise 4, for example). Compile and try out your program.