A macro definition:
#define HTTP_ERRNO_MAP(XX) \
/* No error */ \
XX(OK, "success") \
\
/* Callback-related errors */ \
XX(CB_message_begin, "the on_message_begin callback failed") \
XX(CB_url, "the on_url callback failed") \
/* Define HPE_* values for each errno value above */
#define HTTP_ERRNO_GEN(n, s) HPE_##n,
enum http_errno {
HTTP_ERRNO_MAP(HTTP_ERRNO_GEN)
};
#undef HTTP_ERRNO_GEN
After expand it by “gcc -E”,
enum http_errno {
HPE_OK, HPE_CB_message_begin, HPE_CB_url,};
How does the macro expand to the result?
HTTP_ERRNO_MAP(XX)will substituteXXwithHTTP_ERRNO_GENand therefore call theHTTP_ERRNO_GEN(n, s)macro 3 times:HTTP_ERRNO_GEN(n, s)simply takes the actual text of the first argument and concatonates it (##n) with aHPEout the front and a comma at the end. Therefore the three commands above will produce the following three outputs:Therefore when expanded by the preprocessor,
becomes (+- whitespace):