Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "x" mean in the typical xstr macro?

It's common practice to define stringizing macros like:

#define str(token) #token
#define xstr(token) str(token)

Why is the x prefix typically used? Is there any meaning to "x"?

(My guess would be that maybe it stands for "expanded" (if anything), but I have no evidence to back that up.)

like image 651
jamesdlin Avatar asked Jan 24 '18 10:01

jamesdlin


1 Answers

As others have suggested, the x seems to be used for extended or I've also seen expanded used as well. An article on Stringizing by GNU has a reference to this use.

They phrase it as:

"If you want to stringize the result [...], you have to use two levels of macros."

#define xstr(s) str(s)
#define str(s) #s
#define foo 4
str (foo)
     → "foo"
xstr (foo)
     → xstr (4)
     → str (4)
     → "4"

They continue to state:

"s is stringized when it is used in str, so it is not macro-expanded first. But s is an ordinary argument to xstr, so it is completely macro-expanded before xstr itself is expanded (see Argument Prescan). Therefore, by the time str gets to its argument, it has already been macro-expanded."

While none of this is concrete, I think you're safe to assume x is expanded/extended usage.

like image 53
Trevor Tracy Avatar answered Oct 04 '22 20:10

Trevor Tracy