使用宏获得的输出说明 [英] Explanation of the output obtained by using Macro

查看:13
本文介绍了使用宏获得的输出说明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么不打印第二个'%'?

另外,如果我输入 printf(scanf, scanf, scanf); 为什么会给出相同的输出?

# 包括 <stdio.h>#define scanf "%s Geeks Quiz "主函数(){打印(扫描,扫描);返回0;}

解决方案

为什么第二个 % 没有打印出来?

Ans : 让我们将 printf() 签名与您的用法进行比较,好吗?

根据 手册页,签名,

int printf(const char *format, ...);

和你的使用情况

printf(scanf, scanf);

这里,

  • 第一个scanf代表format字符串,包含转换说明符.
  • 第二个scanfprintf()中第一个%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 the format string, which includes the conversion specifier.
  • second scanf is the argument corresponding to the first %s in the printf().

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆