Is there one macro way to prefix and quote a macro argument C

So, when looking for my macro definitions I found macros #and ##and use them to simplify my macro. The key part of the macro sets the variable to a string containing the name of the variable (but not only the name of the variable). As a simplified example, take a macro with a name SET(X)that should extend SET(something)to something = "pre_something".

The only way I've found this so far is with two macros, for example, #define QUOTE(X) #Xand #define SET(X) X = QUOTE(pre_##X). However, using multiple macros seems excessive and can cause problems with further macro expansion (I think). Is there a cleaner single-line way to do the same?

+4
source share
1 answer

#define SET(x) x = "pre_"#x

C concatenates the string at compile time, so the two string literals next to each other are combined.

"hello " "world""hello world"

+4
source

Source: https://habr.com/ru/post/1614404/


All Articles