0

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.

Giuppox
  • 1,393
  • 9
  • 35
  • 5
    Can you elaborate a bit on what you are trying to accomplish, e.g., show a complete example of how this would be used? Right now I'm not seeing what the macro does other than obfuscates. – Arkku Oct 18 '21 at 15:03
  • Macros are a *text replacement system.* Changing the syntax in any meaningful way seems unlikely. As Arkku states, you need to be more specific about what you want. – Robert Harvey Oct 18 '21 at 15:04
  • 1
    C macros are a very primitive system, and doing something fancy with them often requires ugly code. – Barmar Oct 18 '21 at 15:08
  • Code generation may be a solution, otherwise it is probably either this or repeatedly including a file while redefining macros that change its contents. – Arkku Oct 18 '21 at 15:10
  • What do you mean by "code generation"? @Arkku – Giuppox Oct 18 '21 at 15:12
  • 1
    Make another program/script that generates the C file for you, run it as part of the build process. – Arkku Oct 18 '21 at 15:12
  • @Arkku Ow, I see, do you mean something like numpy's `.src` files? https://github.com/numpy/numpy/blob/main/numpy/core/src/multiarray/lowlevel_strided_loops.c.src – Giuppox Oct 18 '21 at 15:14
  • Basically yes. That solution is replacing/supplementing the limited C pre-processor with a more advanced one. Another code generation approach is to generate from a more un-C-like domain-specific format, like YACC/Bison does for parsers. – Arkku Oct 18 '21 at 15:16
  • Making another c program (or any script in any suitable scripting language) is probably much better than doing ugly things with macros. This is a very common process. – Jabberwocky Oct 18 '21 at 15:18
  • Some compilers have a predefined macro `__FUNCTION__` that expands to the string representation of the function's name in which it appears. Would this help you? – the busybee Oct 18 '21 at 15:38
  • @thebusybee not really :/ – Giuppox Oct 18 '21 at 15:52

0 Answers0