解释一下为什么会出现输出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/