gpt4 book ai didi

c - 在带有数字符号的 printf 中使用宏

转载 作者:太空狗 更新时间:2023-10-29 14:54:56 25 4
gpt4 key购买 nike

我对 K&R 第二版第 90 页中有关宏的解释感到有点困惑。这是段落:

Formal parameters are not replaced within quoted strings. If, however, a parameter name is preceded by a # in the replacement text, the combination will be expanded into a quoted string with the parameter replaced by the actual argument.

我不确定第二句话在说什么。它继续用“调试打印宏”解释它的用途。

This can be combined with a string concatenation to make, for example, a debugging print macro:

#define dprint(expr) printf(#expr " = %g\n", expr);

编辑:

所有的输入都是有用的。谢谢你们。

最佳答案

如果你这样定义宏:

#define MAKE_STRING(X) #X

然后,你可以这样做:

puts(MAKE_STRING(a == b));

这将扩展为:

puts("a == b");

dprint() 示例中,它打印出表达式的字符串形式,以及表达式值。

dprint(sin(x)/2);

将扩展为:

printf("sin(x)/2" " = %g\n", sin(x)/2);

字符串文字连接会将第一个参数视为单个字符串文字。

关于c - 在带有数字符号的 printf 中使用宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25588105/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com