如果结尾没有\ n,则Printf无法正常工作 [英] Printf not working if there isn't \n at the end
问题描述
C中的printf()
函数是否最后需要一个\n
才能起作用?
Does the printf()
function in C need a \n
at the end in order to work ?
我尝试打印出最后没有换行符的简单语句,但是它不起作用.
I tried printing out a simple statement without a newline at the end and it didn't work.
谢谢.
推荐答案
The likely reason is a line buffered stdout
, (this is implementation defined so I can't be 100% sure). In these implementations the content written to the buffer won't immediately be transferred to the output.
使用"\n"
会导致对输出的缓冲区刷新,并且printf
将打印内容,不利之处是"\n"
也将被打印.
Using "\n"
, causes a flush of the buffer to the output and the printf
will print the contents, the downside is that the "\n"
will also be printed.
作为替代方案,如果您不这样做,则可以使用 fflush(stdout)
不想打印换行符.
As an alternative, you can use fflush(stdout)
if you don't want a that newline character to be printed.
对于输出流(以及在其上输出了最后一个操作的更新流),将任何未写入的数据从该流的缓冲区写入关联的输出设备.
这篇关于如果结尾没有\ n,则Printf无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!