c - 解释为什么输出10会出现在c程序中

标签 c

解释一下为什么会出现输出10?

main()
{

   for(printf("1");!printf("0");printf("2"))
    {
    printf("hello");
    }   
}

输出:

10 

最佳答案

根据其printf(3)手册页中,printf 函数返回

the number of characters printed (excluding the null byte used to end output to strings)

所以,for的初始化部分运行一次:printf("1");它将 1 字符放入 stdout 的缓冲区中。然后对条件进行求值 !printf("0"); 由于对 printf("0") 进行求值,因此会将 0 字符放入stdout 的缓冲区并返回输出字符的数量,即 1,因此条件为 false,并且 for 循环存在。

最后,main 退出程序,此时仅刷新 stdout 缓冲区。

说句迂腐的话,当由于不太可能的原因,stdout 无法成功写入时,您的程序将会有不同的行为。

关于c - 解释为什么输出10会出现在c程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12702232/

相关文章:

c - C stdlib 函数中的抽象格式说明符(例如 printf、scanf)

c++ - 在 C++ 和 C 中的现有选项卡中打开 URL

从另一个 c 文件调用 Yacc 文件中的函数

c - C异或可执行文件加密/解密的问题

c - 为什么编译器会在编译的汇编代码中生成额外的 sqrts

c - 为什么 "union test{ short a,b; char c1,c2,c3,c4;};"将 c1,c2,c3,c4 存储在同一个字节中,有两个可用?

c - fopen中的r和rb有什么区别

android - 为 Android 构建 ImageMagick

c - YACC : symbol **** is used but, 未定义为 token ,没有规则

c++ - 不使用虚函数的多态行为