还有什么'\\ n'不除换行符打印? [英] what else '\n' do except printing newline?

查看:272
本文介绍了还有什么'\\ n'不除换行符打印?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

评论2号线你好打印九次,但注释1号线输出Hello的9倍多之后。我的问题是什么是\\ n的这个角色?

 #包括LT&;&stdio.h中GT;
#包括LT&;&stdlib.h中GT;诠释的main()
{
    INT TMP [10],I,N = 0;    对于(i = 0; I< 9;我++)
    {
        TMP [I] = fork()的;
        如果(TMP [I] 0)
            打破;
        其他
        {
            的printf(你好\\ n); // ---- 1号线
            的printf(你好); // ---- 2号线
        }
    }
}


解决方案

\\ n也刷新标准输出缓冲器。如果不是present,它可能是你有$ P $它pviously输入的数据。法拉盛也意味着它强制的printf尽快\\ n被处理的屏幕上打印。否则,它是缓冲的输出,你就别想predict多长时间?OS缓冲你的输出,当precisely它选择实际打印。

After commenting Line 2 "Hello" is printed nine times but commenting Line 1 outputs "Hello" more than nine times. My question is what's the role of '\n' in this?

#include<stdio.h>
#include<stdlib.h>

int main()
{
    int tmp[10], i, n=0;

    for(i=0;i<9;i++)
    {
        tmp[i]=fork();
        if(tmp[i]>0)
            break;
        else
        {
            printf("Hello\n");  //      ---- Line 1
            printf("Hello ");   //      ---- Line 2
        }
    }
}

解决方案

\n also flushes the standard output buffer. If it is not present, it is possible that you have previously entered data in it. Flushing also means it forces printf to print on the screen as soon as \n is processed. Otherwise it is buffered output and you can never predict how long would OS buffer your output and when precisely it chooses to actually print.

这篇关于还有什么'\\ n'不除换行符打印?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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