关于 c:Linux 中 printf(“\ …”) 和 printf(“..\ “) 的输出差异 | 珊瑚贝

Difference in Output between printf(“\n…”) and printf(“..\n”) in Linux

本问题已经有最佳答案,请猛点这里访问。


谁能解释一下为什么这两个相似的代码(除了 \
位置)会导致不同的输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <unistd.h>
#include <sys/wait.h>
#include <stdio.h>

int main()
{
    int pid, i=0;
    printf(“Ready to fork\
);
    pid=fork();
    if (pid==0)
    {
        printf(“Child starts\
);
        for (i=0; i<1000; i++);
        printf(“Child ends\
);
    }
    else
    {
        wait(0);
        for (i=0; i<1000; i++);
        printf(“Parent process ends\
);
    }
    return 1;
}

输出:

enter

还有这个:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <unistd.h>
#include<sys/wait.h>
#include <stdio.h>

int main()
{
    int pid, i=0;
    printf(\
Ready to fork %d”
, getpid());
    pid=fork();
    if (pid==0)
    {
        printf(\
Child starts %d”
,getpid());
        for (i=0; i<1000; i++);
        printf(\
Child ends %d”
, getpid());
    }
    else
    {
        wait(0);
        for (i=0; i<1000; i++);
        printf(\
Parent process ends %d”
, getpid());
    }
    return 1;
}

结果:

enter

我真的找不到任何令人满意的理由来解释为什么 \
的位置的简单更改会在 fork 执行完成后父程序似乎重新启动的级别更改程序的输出。



来源:https://www.codenong.com/49096059/

微信公众号
手机浏览(小程序)
0
分享到:
没有账号? 忘记密码?