Review Questions

I l @ ve RuBoard

Review Questions

  1. Here are groups of one or more macros followed by a source code line that uses them. What code results in each case? Is it valid code?

    1.  #define FPM  5280     /* feet per mile */ dist = FPM * miles; 
    2.  #define FEET 4 #define POD FEET + FEET plort = FEET * POD; 
    3.  #define SIX = 6; nex = SIX; 
    4.  #define NEW(X) X + 5 y = NEW(y); berg = NEW(berg) * lob; est = NEW(berg) / NEW(y); nilp = lob * NEW(-berg); 
  2. Fix the definition in 1.d. to make it more reliable.

  3. Define a macro function that returns the minimum of two values.

  4. Replace the even_gt() macro in Listing 16.4 with a macro of your own devising.

  5. Define a macro function that prints the representations and the values of two integer expressions. For example, it might print

     3+4 is 7 and 4*12 is 48 

    if its arguments are 3+4 and 4*12 .

  6. Create #define statements to accomplish the following goals:

    1. Create a named constant of value 25 .

    2. Have SPACE represent the space character.

    3. Have PS() represent printing the space character.

    4. Have BIG(X) represent adding 3 to X .

    5. Have SUMSQ(X,Y) represent the sums of the squares of X and Y .

  7. Define a macro that prints the name , value, and address of an int variable in the following format:

     name: fop;  value: 23;  address: ff464016 
  8. Suppose you have a block of code you want to skip over temporarily while testing a program. How can you do so without actually removing the code from the file?

    1. How would you create an enumerated type days that makes the abbreviations sun , mon , tue , wed , thu , fri , and sat stand for the integers 0 “6? For the integers 1 “7?

    2. How would you create a variable visit of that type?

  9. What's wrong with this program?

     #include <stdio.h> int main(int argc, char argv[]) {     printf("The square root of %f is %f\n", argv[1],            sqrt(argv[1]) ); } 
  10. Suppose scores is an array of 1000 int values that you want to sort into descending order. You will use qsort () and a comparison function called comp() .

    1. What is a suitable call to qsort() ?

    2. What is a suitable definition for comp() ?

  11. How could you dynamically allot space to hold an array of structures?

I l @ ve RuBoard


C++ Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 314
Authors: Stephen Prata

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net