Printf not working if there isn't \n at the end [duplicate]

喜欢而已 提交于 2020-06-23 12:34:11

问题


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.

Thanks.


回答1:


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.

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.

As an alternative, you can use fflush(stdout) if you don't want a that newline character to be printed.

For output streams (and for update streams on which the last operation was output), writes any unwritten data from the stream's buffer to the associated output device.



来源:https://stackoverflow.com/questions/61352024/printf-not-working-if-there-isnt-n-at-the-end

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!