Pages

May 20, 2010

Macro

Cited from BTP300 Chris seneca cs Web

  • Continuation
  • A macro definition may extend over several lines. The backslash character - \ - immediately followed by the end-of-line character identifies a continuation onto the next line. For example,

    #define PI 3.141\
    592654
    is the same as
    #define PI 3.141592654

  • Efficiency and Flexibility

    Function-like macros provide efficient and flexible solutions. They avoid the overhead of function calls and do not impose type constraints on the parameters in the macro definition.

  • #define SQUARE(x)  ((x) * (x)) /* NOTE THE PARENTHESES *// Fardad noted in class
  • Side-Effects

    Macro definitions can also generate side effects. Because the pre-processor substitutes textual patterns rather than values, it can generate repeated evaluations of expressions that were intended to be a single evaluation. Such as area(r++).

  •  #define PI 3.14
    #undef PI
    #define PI 3.14159 /* OK */
    #define AREA(r) PI * r * r
  • Predeined macros
  •      printf("The name of the source file is %s\n", __FILE__);
    printf("The date of its translation is %s\n", __DATE__);
    printf("The time of its translation is %s\n", __TIME__);
    if (__STDC__ != 0)
    printf("Compiled under Standard C\n");
    else
    printf("Not compiled under Standard C\n");
reference macro

No comments:

Post a Comment