使用宏获得的输出说明 [英] Explanation of the output obtained by using Macro
问题描述
为什么不打印第二个'%'?
另外,如果我输入 printf(scanf, scanf, scanf);
为什么会给出相同的输出?
# 包括 <stdio.h>#define scanf "%s Geeks Quiz "主函数(){打印(扫描,扫描);返回0;}
为什么第二个
%
没有打印出来?
Ans : 让我们将 printf()
签名与您的用法进行比较,好吗?
根据 手册页,签名,
int printf(const char *format, ...);
和你的使用情况
printf(scanf, scanf);
这里,
- 第一个
scanf
代表format
字符串,包含转换说明符. - 第二个
scanf
是printf()
中第一个%s
对应的参数.
本质上,您的 printf()
看起来像
printf("%s Geeks Quiz", "%s Geeks Quiz");^ |--------------||%s 的转换参数说明符
所以,根据 printf()
的工作原理,第一个 %s
被替换为 %s Geeks Quiz
(这里,%s
是输出的一部分,不被视为格式说明符).
所以,你最终的 o/p 看起来像
%s 极客测验 极客测验
<块引用>
另外,如果我输入 printf(scanf, scanf, scanf); 为什么它会给出相同的输出?
Ans: printf(scanf, scanf, scanf);
将产生与上述相同的输出,因为根据 C11
标准,第 7.21.6.1 章,fprintf()
函数,
如果格式已用尽而参数仍然存在,则多余的参数将评估(一如既往),但在其他方面被忽略.
根据上面的解释,我们只有一个格式说明符 %s
(来自第一个 scanf
替换),并且需要一个参数.所以,第三个 scanf
就被忽略了.
Why the second '%' is not printed ?
Also, why it is giving same output if I input 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 : Let's compare the printf()
signature with your usage, shall we?
As per the man page, the signature,
int printf(const char *format, ...);
and your usage
printf(scanf, scanf);
Here,
- first
scanf
represents theformat
string, which includes the conversion specifier. - second
scanf
is the argument corresponding to the first%s
in theprintf()
.
In essence, your printf()
looks like
printf("%s Geeks Quiz", "%s Geeks Quiz");
^ |-------------|
|
conversion argument for %s
specifier
So, as per the workings of printf()
, the first %s
is replaced by %s Geeks Quiz
(here, %s
is part of the output, not treated as a format specifier).
So, your final o/p looks like
%s Geeks Quiz Geeks Quiz
Also, why it is giving same output if I input
printf(scanf, scanf, scanf);?
Ans: printf(scanf, scanf, scanf);
will produce the same output as above, because, as per the C11
standard, chapter 7.21.6.1, fprintf()
function,
If the format is exhausted while arguments remain, the excess arguments are evaluated (as always) but are otherwise ignored.
As per the above explanation, we have only one format specifier %s
(from the first scanf
replacement) and one argument exactly is required for that. So, the third scanf
is simply ignored.
这篇关于使用宏获得的输出说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!