I have C code that contains strinigication as below.
我有C代码,其中包含如下所示的字符串形式。
#define xstr(s) str(s)
#define str(s) #s
#define foo 4
Now xstr (foo) is evaluating as "4" correctly.
现在,xstr (foo)被正确地评估为“4”。
But str(foo) is getting evaluated as "foo". But I thought it should be evaluated as "4". Can any one please explain me how it is evaluated as "foo".
但是str(foo)的值是foo。但我认为它应该被评价为“4”。谁能给我解释一下它是如何被评估为foo的。
1 个解决方案
#1
3
Because of macro expansion rules in C. Using the str(s)
you defined the foo
immediately gets placed as #foo
rather than evaluating the value of foo
. When you wrap it with xstr
it gives it a chance to actually evaluate foo
before applying stringification.
由于c中的宏扩展规则,使用您定义的str(s),立即将foo设置为#foo,而不是评估foo的值。当你用xstr对它进行包装时,它就有机会在应用stringification之前实际地评估foo。
The process looks something like this
这个过程是这样的
str(foo)->#foo->"foo"
xstr(foo)->str(4)->#4->"4"
#1
3
Because of macro expansion rules in C. Using the str(s)
you defined the foo
immediately gets placed as #foo
rather than evaluating the value of foo
. When you wrap it with xstr
it gives it a chance to actually evaluate foo
before applying stringification.
由于c中的宏扩展规则,使用您定义的str(s),立即将foo设置为#foo,而不是评估foo的值。当你用xstr对它进行包装时,它就有机会在应用stringification之前实际地评估foo。
The process looks something like this
这个过程是这样的
str(foo)->#foo->"foo"
xstr(foo)->str(4)->#4->"4"