printf() without '\n' doesn't work in

2019-01-29 13:29发布

Post the code first:

#define EV_STANDALONE 1
#include <stdio.h>
#include "ev.c"

ev_timer timeout_watcher;
struct ev_loop* loop;
static void timeout_cb (EV_P_ ev_timer *w, int revents)
{
//    puts("timeout");
    printf("timeout");
    ev_timer_again(loop, w);
}
int main (void)
{
    printf("hello, world.");
    loop = EV_DEFAULT;
    ev_timer_init (&timeout_watcher, timeout_cb, 5.5, 0.);
    timeout_watcher.repeat = 2.0;
    ev_timer_start (loop, &timeout_watcher);
    ev_run (loop, 0);
    return 0;
}

Strange thing happened while running: although the printf("hello, world."); was in the first place in main function, but it didn't work. But if I use printf("hello, world\n"); instead, things worked fine. Further more, I changed printf("hello, world"); instead of puts("hello, world");, it also worked. So what on earth did libev do to the io? Why "\n" matters?

标签: c printf libev
1条回答
ら.Afraid
2楼-- · 2019-01-29 14:00

Usually, the buffer associated with standard output is line buffered. The content which is written to the buffer are not immediately transferred to the output.

A \n, at the end, causes a flush of the buffer content to the output.

Alternatively, you can use fflush(stdout), after a printf() with no \n but remember, this works for output buffers only.

FWIW, to answer why puts() "works", to quote the man page, (emphasis mine)

puts() writes the string s and a trailing newline to stdout.

there is an implicit newline supplied with puts(), so the buffer is flushed.

查看更多
登录 后发表回答