为什么第二个'%'没有被打印?
另外,为什么如果我输入 printf(scanf, scanf, scanf);
会给出相同的输出?
# include <stdio.h>
# define scanf "%s Geeks Quiz "
int main()
{
printf(scanf, scanf);
return 0;
}
最佳答案
Why the second
%
is not printed ?
Ans:让我们将 printf()
签名与您的用法进行比较,好吗?
根据 man page 、签名、
int printf(const char *format, ...);
以及您的使用情况
printf(scanf, scanf);
这里,
- 第一个
scanf
表示format
字符串,其中包括转换说明符。 - 第二个
scanf
是与printf()
中的第一个%s
相对应的参数。
本质上,你的printf()
看起来像
printf("%s Geeks Quiz", "%s Geeks Quiz");
^ |-------------|
|
conversion argument for %s
specifier
因此,根据 printf()
的工作原理,第一个 %s
被 %s Geeks Quiz
替换(此处,%s
是输出的一部分,不被视为格式说明符)。
所以,你的最终o/p看起来像
%s Geeks Quiz Geeks Quiz
Also, why it is giving same output if I input
printf(scanf, scanf, scanf);?
答:printf(scanf, scanf, scanf);
将产生与上面相同的输出,因为根据 C11
标准,第 7.21.6.1 章,fprintf()
函数,
If the format is exhausted while arguments remain, the excess arguments are evaluated (as always) but are otherwise ignored.
根据上面的解释,我们只有一个格式说明符 %s
(来自第一个 scanf
替换),并且需要一个参数。因此,第三个 scanf
被简单地忽略。
关于c - 使用宏获得的输出说明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30341788/