Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: replaced EX2 exception with new EX2 exception

...

PRE00-EX1: Macros can be used to implement local functions (repetitive blocks of code that have access to automatic variables from the enclosing scope) that cannot be achieved with inline functions.

PRE00-EX2: Macros can also be made to support certain forms of lazy calculation that cannot be achieved with an inline functionbe used for concatenating tokens or performing stringification. For example,

Code Block
#define SELECT(s, v1, v2) ((s) ? (v1) : (v2))enum Color { Color_Red, Color_Green, Color_Blue };
static const struct {
  enum Color  color;
  const char *name;
} colors[] = {
#define COLOR(color)   { Color_ ## color, #color }
  COLOR(Red), COLOR(Green), COLOR(Blue)
};

See [PRE05-C. Understand macro replacement when concatenating tokens or performing stringification] for more information.

calculates only one of the two expressions depending on the selector's value.

...