相关疑难解决方法(0)

究竟哪种双串联技巧有效?

至少有一些C预处理器允许你将宏的值(而不是它的名称)通过一个类似函数的宏传递给另一个将它串行化的宏来进行字符串化:

#define STR1(x) #x
#define STR2(x) STR1(x)
#define THE_ANSWER 42
#define THE_ANSWER_STR STR2(THE_ANSWER) /* "42" */
Run Code Online (Sandbox Code Playgroud)

这里的用例示例.

这确实有效,至少在GCC和Clang(两者都有-std=c99),但我不确定它是如何工作的C标准术语.

这种行为是否由C99保证?
如果是这样,C99如何保证呢?
如果不是,那么从C定义到GCC定义的行为在什么时候?

c stringification c-preprocessor

81
推荐指数
2
解决办法
2万
查看次数

MSVC不会正确扩展__VA_ARGS__

考虑以下代码:

#define F(x, ...) X = x and VA_ARGS = __VA_ARGS__
#define G(...) F(__VA_ARGS__)
F(1, 2, 3)
G(1, 2, 3)
Run Code Online (Sandbox Code Playgroud)

X = 1 and VA_ARGS = 2, 3两个宏的预期输出,这就是我用GCC得到的,但是,MSVC将其扩展为:

X = 1 and VA_ARGS = 2, 3
X = 1, 2, 3 and VA_ARGS =
Run Code Online (Sandbox Code Playgroud)

也就是说,__VA_ARGS__将其扩展为单个参数,而不是分解为多个参数.

有什么方法吗?

visual-c++ c-preprocessor variadic-macros

48
推荐指数
2
解决办法
1万
查看次数

c预处理器将多个参数作为一个传递

C预处理器传递多个参数,就好像它们是单个参数一样.我非常确定问题是我们如何调用untouchable宏,但是我们为改变first宏所做的每一次尝试都未能产生预期的结果.这是一个包含注释的完整代码示例,用于解释发生了什么以及我们想要发生什么:

//this can be changed, but it must remain a #define to be of any use to us
#define first a,b,c

// v all code below this line cannot be altered (it's outside of our control)

#define untouchable(name, c1, c2, c3) \
    wchar_t name[] = \
    { \
        quote(c1), \
        quote(c2), \
        quote(c3) \
    }

#define quote(c) L#@c

// ^ all code above this line cannot be altered (it's outside of our control)

int _tmain(int argc, …
Run Code Online (Sandbox Code Playgroud)

c macros c-preprocessor

12
推荐指数
2
解决办法
1610
查看次数

c编程语言中的宏评估

可能重复:
"#define STR(a)#a"有什么作用?

#include <stdio.h>
  #define f(a,b) printf("yes")
  #define g(a)   #a
  #define h(a) g(a)

  int main()
  {
        printf("%s\n",h(f(1,2)));
        printf("%s\n",g(f(1,2)));

  }
Run Code Online (Sandbox Code Playgroud)

有人可以解释为什么两个printf()语句的输出都不同.

c macros

5
推荐指数
2
解决办法
3651
查看次数