Try   HackMD

2019q1 Homework1 (list)

contributed by < TsundereChen >

tags: sysprog TsundereChen

Note

Function-like macro

  • Function-like macro definition
    • An identifier followed by a parameter list in parentheses and the replacement tokens
    • Parameters are imbedded in the replacement code
    • White space can't separate the identifier
    • A comma must separate each parameter
    • For portability, shouldn't have more than 31 parameters in a macro
    • __VA_ARGS__ may appear in the replacement list
  • Function-like macro invocation
    • An identifier followed by a comma-separated list of args in parentheses
    • Number of args should match the number of params in the macro definition, unless parameter list in the definition ends with an ellipsis
    • If parameter list ends with an ellipsis, the number of args in the invocation should exceed the number of params in the definition
    • Preprocessor handles argument substitution
    • __VA_ARGS__ is replaced by trailing args if permitted by the macro definition
  • Example
    ​​#define SUM(a, b) (a + b)
    ​​c = SUM(x, y); /* c = (x + y); */
    ​​c = d * SUM(x, y); /* c = d * (x + y) */
    ​​
    ​​#define SQR(c) ((c) * (c))
    ​​y = SQR(a + b); /* y = SQR((a + b) * (a + b)); */
    ​​
    ​​#define debug(...) fprintf(stderr, __VA_ARGS__)
    ​​debug("foo"); /* fprintf(stderr, "foo") */
    

static inline versus function-like macro

  • static inline should be as quick as function-like macro
    • Since both of them replace the original code

typeof

  • typeof can be used with experssion or type
    • Experssion
      typeof(x[0](1))
    • Type
      typeof(int *)

TODO

Reference