# 2019q1 Homework1 (list) contributed by < `TsundereChen` > ###### tags: `sysprog` `TsundereChen` * [GitHub Repo](https://github.com/TsundereChen/linux-list) ## 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 ```cpp #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 - [ ] Complete [Homework requirement](https://hackmd.io/s/S12jCWKHN#%E4%BD%9C%E6%A5%AD%E8%A6%81%E6%B1%82) ## Reference * [Macro vs Function in C - StackOverflow](https://stackoverflow.com/questions/9104568/macro-vs-function-in-c) * [Inline Functions in C](http://www.greenend.org.uk/rjk/tech/inline.html) * [Function-like macros - IBM Knowledge Center](https://www.ibm.com/support/knowledgecenter/en/ssw_ibm_i_71/rzarg/cpxmac.htm)