c - 使用宏获得的输出说明

标签 c macros printf

为什么第二个'%'没有被打印?

另外,为什么如果我输入 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/

相关文章:

iOS 其他应用中的点击/拖动事件

c - fprintf 语句中的\b 转义序列有问题..?

c - 显示文件指针位置

c - VLAN上的UDP套接字单播/多播问题

计算文件中 8 位符号的频率

c - 为什么 snprintf 更改输出字符串?

embedded - 使用 printf 打印 float/double 会使程序崩溃

用于取消引用指向固定大小二维数组的指针的 C typedef

macros - 具有不同类型的可变参数的宏

macros - Go 中的 C 风格宏