1

I've went through a number of similar questions and almost got it to work, but not quite.

What I have is the standard Macro-Expansion-Stringification-method.

#define QUOTEME(M)          #M
#define DOQUOTE(M)          QUOTEME(M)
#define XCONCAT(X, Y)       X##Y
#define CONCAT(X, Y)        XCONCAT(X, Y)

Then I have a macro PREFIX like this:

#define SW_PREFIX    XY2Ar-

What I was trying to do was:

#define SW_FILE DOQUOTE(CONCAT(SW_PREFIX, update))

What this is supposed to output is: "XY2Ar-update" What it outputs on my GCC-type compiler is:

error: pasting "-" and "update" does not give a valid preprocessing token

Now, I assume that he's replacing the macro correctly, but apparently doesn't want to append update to XY2Ar- because of the -.

I also tried:

#define SW_FILE     DOQUOTE(SW_PREFIX.update)

which, once again, almost does what I want, however it outputs XY2Ar-.update, which is not what I want either.

I'm out of ideas.

3 Answers 3

3

Here is an alternative:

#define QUOTEME(M)    #M
#define COMPOSE(A, B) QUOTEME(A) #B

#define SW_PREFIX     XY2Ar-
#define SW_FILE       COMPOSE(SW_PREFIX, update)
Sign up to request clarification or add additional context in comments.

5 Comments

I did think that I tried something like that before. Error message: error: pasting ""XY2Ar-"" and ""update"" does not give a valid preprocessing token. Thanks for the input though.
@RefugnicEternium: You're right, sorry. That was messed up. Check now, I think this works. I think it has to be asymmetric in the two arguments, because one is a bareword and the other needs replacement. Demo.
This seems to work, though I think, quite personally, that my solution is a little less...shall we say 'verbose', wouldn't you agree? ;) I discovered it shortly after I left the comment on your previous solution.
@RefugnicEternium: Yes, absolutely. Feel free to make yours the accepted answer. (That said, your answer also changed the question by allowing SW_PREFIX to be a string rather than a bareword, so it's somewhat different situation.)
Yes, I understand that the situation is a different one, which is why yours remains the accepted one. Otherwise, Jens' answer has a very similar effect to that end.
3

Your thinking seems too complicated :-) Do not create a single token to stringify. Simply use string concatenation for separate parts.

$ cat x.c
#define QUOTEME(M) #M
#define DOQUOTE(M) QUOTEME(M)

#define SW_PREFIX    XY2Ar-
#define SW_FILE  DOQUOTE(SW_PREFIX) QUOTEME(.update)

char *x = SW_FILE;
$ gcc -E x.c
# 1 "x.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "x.c"
char *x = "XY2Ar-" ".update";

1 Comment

Yeah, I get that a lot. I also came to the same conclusion a little while ago. Thanks for pointing it out again though. :)
1

Sometimes I keep finding myself forgetting the KISS-principle...and it's times like these where it shows.

#define SW_PREFIX   "XY2Ar-"
#define SW_FILE     SW_PREFIX"update"

These simple lines, no further expansion or anything return the desired output. As for the reason why it does that:

Since there aren't any # in there, it expands SW_PREFIX to "XY2Ar-" and appends "update" to it, which results in "XY2Ar-""update", which, for the compiler, is the same as XY2Ar-update.

Thanks to the guy who erased his answers for reminding me.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.