printf()函数没有'\\ n'不libev工作 [英] printf() without '\n' doesn't work in libev

查看:178
本文介绍了printf()函数没有'\\ n'不libev工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一篇文章的code:

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;
}

奇怪的事情发生了运行时:尽管的printf(你好,世界); 在主函数的第一个地方,但没有奏效。但是,如果使用的printf(你好,世界\\ n); 相反,事情的罚款。更进一步,我改变了的printf(你好,世界); 而不是看跌期权(你好,世界); ,它也工作。那么究竟是什么没做libev到IO?为什么\\ n的问题?

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 ,到了最后,导致刷新缓冲区的内容输出。

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

另外,你可以使用 fflush(标准输出),经过的printf()没有 \\ n 但要记住,这个工程的输出缓冲器只。

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

FWIW,要回答为什么 看跌期权() 作品的,引用手册页(重点煤矿的)

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.

这篇关于printf()函数没有'\\ n'不libev工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆