为什么输出后会有多余的百分号? [英] Why is there an extra percent sign after the output?

查看:608
本文介绍了为什么输出后会有多余的百分号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近正在Linux下学习fork()函数.我写了一个程序.

I'm learning fork() function under linux recently. I wrote a program.

#include<stdio.h>
int main(){
    int p1, p2;
    while((p1 = fork()) == -1);
    if(p1 == 0)
        printf("b");
    else{
        while((p2 = fork()) == -1);
        if(p2 == 0)
            printf("c");
        else
            printf("a");
    }
}

编译并运行它之后,出现了意外的百分号.

After I compiled and run it, I got a unexpected percent sign.

但是,如果我在这些字母后面加上\ n,百分号就会消失.

But if I add \n after those letters, the percent sign disappears.

有人知道原因吗?

还有另一个问题.每次我重新运行该程序时,都会得到相同的答案.它始终显示"acb".顺序总是相同的.为什么?

And I have another question. Every time I reran the program, I got the same answer. It always shows "acb". The order is always the same. Why?

推荐答案

您的shell(zsh)添加了它,以指示输出未以换行符结尾.

Your shell (zsh) added it to indicate the output did not end with a newline character.

要摆脱它,只需以\n结束输出即可.

To get rid of it, just end your output with a \n.

关于另一个问题,这不是确定性的.如果您在其他地方或足够时间运行它,则可能会得到不同的结果.但这说明了为什么很难找到同步问题,因为事情似乎总是(几乎)一直在运行.

As to the other question, it's not deterministic. If you ran it elsewhere or enough times you might get different results. But it's an illustration of why synchronization problems can be so hard to find, because things can seem to run the same (almost) all the time.

这篇关于为什么输出后会有多余的百分号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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