15

Is it possible to concatenate string from another macro when #including a file name (in C). For example,

I have,

#define AA 10 
#define BB 20

these are parameters that change with program runs

And the file include:

#include "file_10_20" // this changes correspondingly i.e. file_AA_BB

Is it possible to have something like #include "file_AA_BB" somehow? I googled to find that double pound operator can concat strings but couldn't find a way of doing it.

Any help would be appreciated.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
anasimtiaz
  • 321
  • 3
  • 9

2 Answers2

22

First I thought "that's easy", but it did take a few tries to figure out:

#define AA 10 
#define BB 20

#define stringify(x) #x
#define FILE2(a, b) stringify(file_ ## a ## _ ## b)
#define FILE(a, b) FILE2(a, b)

#include FILE(AA, BB)

As requested I'll try to explain. FILE(AA, BB) expands to FILE2(AA, BB) but then AA and BB is expanded before FILE2, so the next expansion is FILE2(10, 20) which expands to stringify(file_10_20) which becomes the string.

If you skip FILE2 you'll end up with stringify(file_AA_BB) which won't work. The C standard actually spends several pages defining how macro expansion is done. In my experience the best way to think is "if there wasn't enough expansion, add another layer of define"

Only stringily will not work because the # is applied before AA is replaced by 10. That's how you usually want it actually, e.g.:

#define debugint(x) warnx(#x " = %d", x)
debugint(AA);

will print

AA = 10
Per Johansson
  • 6,697
  • 27
  • 34
  • If you are able to explain me why FILE macro is necessary I'll upvote ;-). I've seen many such constructs already and never understood why just 2 macros do not work or even why asael's answer does not work. I am highly interested in knowing how preprocessor works in your example. Can someone explain? – Artur Feb 01 '12 at 13:21
  • 2
    Don't forget to #undef FILE, because it will collide with something in – wildplasser Feb 01 '12 at 13:22
  • Right, perhaps not the best choice of macro name. – Per Johansson Feb 01 '12 at 13:30
  • First of all, thanks for the details. I am about to try this but before that I am trying to understand rather than just simply using it. I don't understand why there is a need to define FILE and FILE2. You do say that stringify alone will not work - I don't understand why that wont. – anasimtiaz Feb 01 '12 at 13:48
  • 2
    @Per: Thanks for explanation. I think everyone interested in this question would actually like to read http://www.delorie.com/gnu/docs/gcc/cpp_32.html – Artur Feb 01 '12 at 15:42
  • @Artur Very useful link. VERY USEFUL. Clears everything nicely. – anasimtiaz Feb 01 '12 at 16:31
  • In practice this might work for some compilers, but it's not defined in the standard. See http://stackoverflow.com/a/20524959/2436175 – Antonio Aug 24 '16 at 08:37
  • Follow-up to @antonio's comment: "The method by which a sequence of preprocessing tokens between a < and a > preprocessing token pair or a pair of " characters is combined into a single header name preprocessing token is implementation-defined." – Seth Johnson Oct 02 '19 at 19:06
6

It is usually used like this:

#define stringify(x)  #x
#define expand_and_stringify(x) stringify(x)

#define AA 10
#define BB 20

#define TEXT1 "AA = " stringify(AA) " BB = " stringify(BB)
#define TEXT2 "AA = " expand_and_stringify(AA) " BB = " expand_and_stringify(BB)

TEXT1 = "AA = AA BB = BB"
TEXT2 = "AA = 10 BB = 20"

It's called stringification. You should check this answer.

Community
  • 1
  • 1
LihO
  • 41,190
  • 11
  • 99
  • 167
  • Isn't this not supposed to work for an include statement? See http://stackoverflow.com/questions/20524491/preprocessor-concatenation-for-include-path/20524959#20524959 and http://stackoverflow.com/questions/39038867/string-concatenation-for-include-path – Antonio Aug 24 '16 at 08:41