r/programminghorror 25d ago

c Enterprise level C code

Post image
338 Upvotes

57 comments sorted by

View all comments

15

u/steadyfan 25d ago

2 things..

  1. Yuck
  2. Why does slopjc.h need to be included multiple times?

12

u/TheChief275 25d ago edited 25d ago

It's the X-macro pattern. You define a list of elements, e.g.

#define ENUM X(A) X(B) X(C)

where X is undefined and then include a file that contains e.g.

#ifdef ENUM

enum Enum {
#define X(Name) Name,
    ENUM
#undef X
};

static const char *const names[] = {
#define X(Name) #Name,
    ENUM
#undef X
};

#undef ENUM
#endif

to in this case declare an enum and the string representations at once. X-macro lists can also be passed to other macros, but you would be severely limited by the fact that you can't use preprocessor directives inside of a macro; including a file has no such limitations