Skip to content

Instantly share code, notes, and snippets.

@Zelmoghazy
Last active February 8, 2025 13:02
Show Gist options
  • Save Zelmoghazy/fd99fffd5a7903cee99a813808f96367 to your computer and use it in GitHub Desktop.
Save Zelmoghazy/fd99fffd5a7903cee99a813808f96367 to your computer and use it in GitHub Desktop.
Get the string representation of enums and iterate them in C using macros
#include <stdio.h>
#define ENUM_GEN(ENUM) ENUM,
#define STRING_GEN(STRING) #STRING,
// Add all your enums here
#define LIST_RET_T(RET_T) \
RET_T(RET_SUCCESS) \
RET_T(RET_FULL) \
RET_T(RET_EMPTY) \
RET_T(RET_NULL) \
RET_T(RET_COUNT)
typedef enum RET_T
{
LIST_RET_T(ENUM_GEN)
}RET_T;
const char* RET_T_strings[] =
{
LIST_RET_T(STRING_GEN)
};
#define GET_RET_T_STR(e) RET_T_strings[e]
#define FOREACH_RET_T(ret) for(ret = 0; ret < RET_COUNT; ret++)
int main(void)
{
RET_T ret = RET_SUCCESS;
FOREACH_RET_T(ret){
printf("%s\n", GET_RET_T_STR(ret));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment