O'Reilly logo

C Primer Plus, Fourth Edition by Stephen Prata

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 16

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? (Assume C variables have been declared.)
  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);
    
A1:
  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; ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required