Looking around I have found this SO answer that explains how to list something in C macros to be able to use them later. Something like this:
#define VARS(F) F(name) F(age) F(city)
#define VAR(X) int X;
VARS(VAR)
#undef VAR
However this solution really seems ugly to me, it must be admitted that it is quite horrendous. Is there some king of macro-magic that lets us specify the values of VARS
separated by commas or in some other more readable way than this?
I have a list of words that should be used as function names and that should be used in the body of the corresponding function, something equivalent to this.
void name() {
printf("name");
}
void age() {
printf("age");
}
void address() {
printf("address");
}
this can be obviously be compressed into something like this:
#define VARS(F) F(name) F(age) F(address)
#define VAR(X) void X() { printf(#name); }
VARS(VAR)
#undef VAR
however this I kinda ugly, is there some way to accomplish the same result without having to use that F(...)
thing? I think it can become quite chunky if the number of elements increase.