This question already has an answer here:
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?
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 aflush
of the buffer content to the output.Alternatively, you can use
fflush(stdout)
, after aprintf()
with no\n
but remember, this works for output buffers only.FWIW, to answer why
puts()
"works", to quote the man page, (emphasis mine)there is an implicit newline supplied with
puts()
, so the buffer is flushed.