[英]Why does program not execute final printf statement?
我無法弄清楚為什么在for循環之后,程序控制沒有到達第三個printf。
為什么第三張printf
打印?
如果我將for循環更改為while循環,它將仍然無法打印。
這是程序和輸出:
main()
{
double nc;
printf ("Why does this work, nc = %f\n", nc);
for (nc = 0; getchar() != EOF; ++nc)
{
printf ("%.0f\n", nc);
}
printf ("Why does this work, nc = %f", nc);
}
輸出為:
Why does this work, nc = 0.000000
test
0
1
2
3
4
對我來說效果很好,您打算如何終止該程序? 一旦getchar()
EOF
檢測為輸入,則for
循環應結束。
EOF
在Windows下是Control-Z
( ^Z
),在Linux / Unix下是Control-D
( ^D
)。 輸入此內容后,循環終止,並得到最終的printf()
以顯示其輸出。
作為最后的說明(如@DanielFisher所述),在最終的printf()
調用的末尾添加一個'\\n'
,因為您的特定實現可能會要求它,否則程序的行為可能是不確定的(感謝@ KeithThompson和@AndreyT在評論中指出了這一點):
printf ("Why does this work, nc = %f\n", nc);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.