Chapter 16

I l @ ve RuBoard

Chapter 16

    1. dist = 5280 * miles; is valid.

    2. plort = 4 * 4 + 4; is valid. But if the user really wanted 4 * (4 + 4) , he or she should have used #define POD (FEET + FEET) .

    3. nex = = 6;; is valid, but not meaningful. Apparently the user forgot that he or she was writing for the preprocessor, not writing in C.

    4. y = y + 5; is valid. berg = berg + 5 * lob; is valid, but this is probably not the desired result. est = berg + 5/ y + 5; is valid, but this is probably not the desired result. nilp = lob *-berg + 5; is valid, but this is probably not the desired result.

  1. #define NEW(X) ((X) + 5)

  2. #define MIN(X,Y) ( (X) < (Y) ? (X) : (Y) )

  3. #define EVEN_GT(X,Y) ( (X) > (Y) && (X) % 2 == 0 ? 1 : 0 )

  4. #define PR(X,Y) printf(#X " is %d and " #Y " is %d\n", X,Y)

    Because X and Y are never exposed to any other operations (such as multiplication) in this macro, you don't have to cocoon everything in parentheses.

    1. #define QUARTERCENTURY 25

    2. #define SPACE ' '

    3. #define PS() putchar (' ') or #define PS() putchar(SPACE)

    4. #define BIG(X) ((X) + 3)

    5. #define SUMSQ(X,Y) ((X)*(X) + (Y)*(Y))

  5. Try this:

     #define P(X) printf("name: "#X"; value: %d; address: %p\n", X, &X) 

    Or, if your implementation doesn't recognize the %p specification for the address, try %u or %lu .

  6. Use the conditional compilation directives. One way is to use # ifndef :

     #define _SKIP_  /* remove when you don't want to skip code */ #ifndef _SKIP_     /* code to be skipped */ #endif 
    1.  enum days {sun, mon, tue, wed, thu, fri, sat}; /* 0 - 6  enum days {sun = 1, mon, tue, wed, thu, fri, sat}; / 1 - 7 */  
    2. enum days visit;

  7. The argv argument should be declared as type char *argv[] . Command-line arguments are stored as strings, so the program should first convert the string in argv[1] to a type double value, for example, by using atof() from the stdlib.h library. The math.h header file should be included for the sqrt() function. The program should check for negative values before taking a square root.

    1. The function call should look like this:

       qsort( (void *)scores, (size_t) 1000, sizeof (double), comp); 
    2. Here's a suitable comparison function:

       int comp(const void * p1, const void * p2) {     /* need to use pointers to int to access values   */     const int * a1 = p1; /* a1 is proper pointer type */     const int * a2 = p2;     if (*a1 > *a2)         return -1;     else if (*a1 == *a2)         return 0;     else         return 1; } 
  8. The program should include the stdlib.h file , if available, or else declare malloc() or calloc () .

     struct wine * ptrwine; ptrwine = (struct wine *) calloc(100, sizeof (struct wine)); 

    or

     ptrwine = (struct wine *) malloc(100 * sizeof (struct wine)); 
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