问题
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