C Language

Macros

Tricks

Macro callback pattern for list of things

#define LIST_OF_THINGS(DO)                      \
    DO(ITEM_0)                                  \
    DO(ITEM_1)                                  \
    DO(ITEM_2)                                  \
    DO(ITEM_3)                                  \
    DO(ITEM_4)                                  \
    DO(ITEM_5)                                  \
    DO(ITEM_MAX)

typedef enum enumname {
#define DECL_ENUM(item) item,
    LIST_OF_THINGS(DECL_ENUM)
#undef DECL_ENUM
} enumname_t;

const char* enumname_strings[] = {
#define DECL_S(it) #it,
    LIST_OF_THINGS(DECL_S)
#undef DECL_S
};


This way you can Re-use a list of items that are commonly repeated.

You can also addd more parameters and filter or discard values depending on your needs.