printf() without '\n' doesn't work in libev [duplicate]

北城余情 提交于 2019-12-29 10:07:38

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/31179225/printf-without-n-doesnt-work-in-libev

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