c - 嵌套的 printf 语句工作

标签 c printf

<分区>

我无法理解以下代码如何给出不同的输出

#include <stdio.h>
int main()
{
        int i=43;
        printf("%d\n",printf("%d",printf("%d",i)));
        return 0;
}

输出:4321

printf("%d\n",printf("%d",printf("%d ",i)));

输出:43 31

printf("%d\n",printf("%d ",printf("%d ",i)));

输出:43 3 2

printf("%d\n",printf("%d ",printf(" %d ",i)));

输出 43 4 2

printf("%d\n",printf(" %d ",printf(" %d ",i)));

输出:43 4 3

和其他变体也提供其他输出。

一个空格怎么能改变一个数字。

提前致谢。

最佳答案

printf 返回打印的字符数

在您的情况下,该语句可以像这样被破坏 - 预期输出:

    printf("%d\n", // 1
           printf("%d", // 2
                  printf("%d",i)    // 43
                 )
          );  // 4321

有关更多信息,请参阅标准 fprintf(printffprintf 的特例,流为 stdout)

7.21.6.1 The fprintf function

 #include <stdio.h>
       int fprintf(FILE * restrict stream,
           const char * restrict format, ...);

The fprintf function returns the number of characters transmitted, or a negative value if an output or encoding error occurred.

关于c - 嵌套的 printf 语句工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40970959/

相关文章:

c - 如何从 GET HTTP 请求中仅获取消息正文?

c - vsprintf 缓冲区的动态内存分配

c - 为什么 printf 创建 windows 行尾?

bash - 根据列值打印n次字符

c - 如何缓冲和延迟 printf() 输出?

c - if语句的问题

c - 如何使用动态内存分配将元素分配给矩阵?

c - 确定包含的库头文件使用了哪些预处理器定义

c - 这个语法正确吗?

c - glibc 的 fprintf() 实现是线程安全的吗?