由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - c preprocess question
相关主题
请高人解释一下为啥这个输出总是"HELLO-ERR"c++ pointer conversion question
Question on using ## in #defineC++ Q13: Input
shortest path algorithm(dijkstra)的变形怎么样实现fuzzy join
Is this correct?i +++ j
[合集] Debugging large macroC 和 C++ 的区别
STL的问题(C++)请教怎么用#define实现如下的功能
深情的呼唤师傅们!C++做题做不出来啦!关于在C中定义常量
请教一个算法问题!求助 怎么编辑 多个 .c files(比如a.c, b.c) 和一个.h file(ab (转载)
相关话题的讨论汇总
话题: preprocess话题: define话题: line1话题: line2话题: n%
进入Programming版参与讨论
1 (共1页)
s*****n
发帖数: 461
1
为什么line1合法,line2非法?
#include
#define f(a,b) a##b
#define g(a) #a
#define h(a) g(a)
int main(void)
{
printf("\n%s", g(f((1,2),3)) ) ; /*line1*/
printf("\n%s", h(f((1,2),3)) ) ; /*line2*/
}
X****r
发帖数: 3557
2
宏函数替换的时候所有不是用在#或##里的参数也会被宏展开,所以你这里g和h
并不等价。
g(f((1,2),3)) => "f((1,2),3))"
h(f((1,2),3)) => g(f((1,2),3)的宏展开),
但是f((1,2),3) => (1,2)3 编译错误
C89 6.10.3.1 Argument substitution
1 After the arguments for the invocation of a function-like
macro have been identified, argument substitution takes
place. A parameter in the replacement list, unless
preceded by a # or ## preprocessing token or followed by
a ## preprocessing token (see below), is replaced by the
corresponding argument after all

【在 s*****n 的大作中提到】
: 为什么line1合法,line2非法?
: #include
: #define f(a,b) a##b
: #define g(a) #a
: #define h(a) g(a)
: int main(void)
: {
: printf("\n%s", g(f((1,2),3)) ) ; /*line1*/
: printf("\n%s", h(f((1,2),3)) ) ; /*line2*/
: }

s*****n
发帖数: 461
3
谢谢!
1 (共1页)
进入Programming版参与讨论
相关主题
求助 怎么编辑 多个 .c files(比如a.c, b.c) 和一个.h file(ab (转载)[合集] Debugging large macro
JAVA的初衷本来就是C++太复杂了,productivity不行STL的问题(C++)
interview with Ben Klemens, author of 21st Century C深情的呼唤师傅们!C++做题做不出来啦!
有码工的地方就有是非啊请教一个算法问题!
请高人解释一下为啥这个输出总是"HELLO-ERR"c++ pointer conversion question
Question on using ## in #defineC++ Q13: Input
shortest path algorithm(dijkstra)的变形怎么样实现fuzzy join
Is this correct?i +++ j
相关话题的讨论汇总
话题: preprocess话题: define话题: line1话题: line2话题: n%