10

I've two functions:

void foo0(int bar);
void foo1(int bar);

I wan't to be able create a macro QUX that will expand to these function names based on another macro BAZ. I tried the following:

#define BAZ 0
#define QUX(x) foo##BAZ(x)

But it did not work since the generated function was fooBAZ(). How can I get it to generate foo0()?

1
  • My solution solves your fooBAZ problem. I provided code you can copy and paste right into a compiler and try. I also provided a link explaining why you get that error. Jul 17, 2013 at 12:59

5 Answers 5

10
#define CAT_I(a,b) a##b
#define CAT(a,b) CAT_I(a, b)
#define QUX(x) CAT(foo, BAZ) ## (x)
2
  • 7
    there must be a reason to have the intermediate #define CAT(a,b) CAT_I(a, b). Could you elaborate?
    – AjB
    Apr 7, 2018 at 17:30
  • 6.10.3.3 The ## operator says: "(3) For both object-like and function-like macro invocations, before the replacement list is reexamined for more macro names to replace, each instance of a ## preprocessing token in the replacement list (not from an argument) is deleted and the preceding preprocessing token is concatenated with the following preprocessing token. ... The resulting token is available for further macro replacement." This means if you have a macro in one of the args, it is pasted before substitution.
    – aghast
    Apr 26, 2021 at 1:23
8
#include <stdio.h>

#define ONE 1
#define ZERO 0

#define GLUE_HELPER(x, y) x##y
#define GLUE(x, y) GLUE_HELPER(x, y)

int foo0(int x)
{
    printf("\n foo0...%d\n", x);
}

int foo1(int x)
{
    printf("\n foo1...%d\n",x);
}

int main()
{
   //Calling Function
   GLUE(foo,ZERO)(5);    
   GLUE(foo,ONE)(10);    
   return 0;
}

will works as expected. You have to do string concate in the second level macro expansion.

1
  • 1
    But he wants the 0 to come from the macro expansion of BAZ. I'm guessing GLUE(foo, BAZ) doesn't work if BZ id defined as 0.
    – JeremyP
    Jul 17, 2013 at 12:50
3

Either have an intermediate macro that evaluates once more

#define QUX_0(A) foo ## A
#define QUX_1(A) QUX_0(A)
#define QUX(x) QUX_1(BAZ)(x)

or maybe even simpler with

#define fooBAZ foo0

BTW: your second ## should error out on a conforming compiler. Also don't put ; at the end of a macro definition, it will bite you.

2
  • Your first example gives me fooBAZ when I try it.
    – JeremyP
    Jul 17, 2013 at 12:25
  • @JeremyP, right, you'd have to expand even more, please see my edit. Jul 17, 2013 at 13:08
1

Here is a solution I came up with after reading a bit online. Basically, in order to get the number to show up in the signature you need to stringify the argument. If you stringify an argument, the prescan does not complete which results in your fooBAZ function definition (it did not fully expand)

If you use another macro intermediary it will complete the expansion.

Here is a link that explains further about prescan and the times it is not completed.

// Testing.cpp : Defines the entry point for the console application.
//
#include <stdio.h>

#define BAZ 0

#define QUXdef(x) QUX(x)
#define QUX(x) foo##x()

#define QUXimp(x) DEF(x)
#define DEF(x) foo##x()


//Function Def
void QUXdef(BAZ);

int main(int argc, char* argv[])
{
    foo0();
}

void QUXimp(BAZ)
{
    printf("Hello world\n");
}

NOTE: I included another set of macros for implementing the function. You did not ask for this, but I figured it would be more "complete" if defining and implementing functions was your goal. QUXdef and QUX fulfill your specific needs.

Edit 1:

  • I removed C++-style code from the solution. It was brought to my attention by Jens Gustedt and is a valid critique considering this was tagged as C(My fault for not reading fully).
  • I also removed the ; form the macro itself. It is now supplied by the user instead which is much more readable. This was also a valid critique from Jens Gustedt
  • I took void out of the macro expansion. Instead I let the user define the return type. To me this seems to also allow for more readability.
  • I removed the stdafx statement considering this is for C code and visual studio is less likely to be used by pure C programmers. As it sits, this should be able to be copied and pasted into a file and then compiled by GCC or some other more popular C compiler.

Otherwise, this answer is solid and if you look at the link I provided it explains the need for an intermediary macro to get the proper expansion the Asker is looking for.

4
  • 1
    your function example isn't C but C++. And really don't put the ; in a macro definition. Things are much better readable (and parsable by editors) if you have the ; at the call site of the macro. But wait, then you wouldn't even need the distinction in def and imp. Jul 17, 2013 at 14:18
  • Valid critiques. I will get rid of the C++-style code and having the ; out of the macro itself definitely helps with readability. However, intermediary macro definitions are required due to stringifying the x argument which helps produce the 0 within the foo0 prototype. I tried removing the ; and then seeing if this would allow me to using QUX on its own, but the same issue with the prescan is run into if you do this. My solution works fine otherwise. Jul 17, 2013 at 15:29
  • @JensGustedt I have made an edit implementing the critiques you mentioned. Thanks for the useful comment. Jul 17, 2013 at 15:37
  • Great link to the gcc prescan info. Thank you.
    – egyik
    Mar 12, 2020 at 10:50
0

If there's only two possibilities:

#if BAZ == 0
#define QUX(X) foo0(x)
#else
#define QUX(X) foo1(x)
#endif
1
  • @johnmachan You need #elif quite a lot.
    – JeremyP
    Jul 17, 2013 at 12:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.