与printf相关的问题!!为什么会这样? [英] problem related to printf!! why is it so???

查看:62
本文介绍了与printf相关的问题!!为什么会这样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个与printf相关的问题,并缓冲到printf语句

查看这个程序。

main()

{

int pid;

int loop,max = 3;


for(loop = 0; loop< max; loop ++ )

{

pid = fork();

if(pid< 0)

printf(" ; \ n十年中最大的叉错误);

else if(pid == 0)

{

printf(" \ n我是一个孩子);

转出;

}

其他

{

printf(\ n我已经长大了);

}

}

out:

}


输出:

我是小孩

我长大了

我是个孩子我已经长大了

我已经长大了

我还是个孩子我已经长大了

我已经长大了

同样的程序,''\ n''放在printf的末尾

main()

{

int pid;

int loop,max = 3;


for(loop = 0; loop< max; loop ++)

{

pid = fork();

if(pid< 0)

printf(十年最大的叉子错误);

否则如果(pid == 0)

{

printf(我是小孩);

转出;

}

其他

{

printf(我已经长大了);

}

}

out:

}


输出:

我是小孩

我有长大了

我还是个孩子

我长大了

我还是个孩子

我长大了


为什么会有这种差异???

有人可以解释一下.....

It is a problem related to printf, and buffering to the printf statements
Look through this program.
main()
{
int pid;
int loop,max=3;

for(loop = 0; loop <max;loop++)
{
pid = fork();
if(pid < 0)
printf("\nThe greatest fork error of the decade");
else if(pid==0)
{
printf("\nI am a child");
goto out;
}
else
{
printf("\nI have grown up");
}
}
out:
}

output:
I am a child
I have grown up
I am a childI have grown up
I have grown up
I am a childI have grown up
I have grown up

the same program with ''\n'' placed at the end of printf
main()
{
int pid;
int loop,max=3;

for(loop = 0; loop <max;loop++)
{
pid = fork();
if(pid < 0)
printf("The greatest fork error of the decade\n");
else if(pid==0)
{
printf("I am a child\n");
goto out;
}
else
{
printf("I have grown up\n");
}
}
out:
}

output:
I am a child
I have grown up
I am a child
I have grown up
I am a child
I have grown up

why is this difference???
could anyone please explain.....

推荐答案

这是一个与printf相关的问题,并缓冲到printf语句
查看这个程序。
main()
{
int pid;
int loop,max = 3;

for(loop = 0; loop< max;循环++)
{p / = pid = fork();
if(pid< 0)
printf(\ n十年来最大的叉错误);
如果(pid == 0)
{
printf(" \ n我是一个孩子;
转到;
}

{/ /> printf(\ n我已经长大了);
}
}
输出:
}

输出:
我还是个孩子
我长大了
我是个孩子我已经长大了
我已经长大了
我是一个孩子我已经长大了
我已经长大了

同样的程序''\ n''放置在printf的末尾
main()
{int / pid;
int loop,max = 3;

for(loop = 0; loop < max; loop ++)
{p / = pc = fork();
if(pid< 0)
printf("十年中最大的叉错误\ n" ;);
如果(pid == 0)
{
printf(我是小孩的话) n;
转出;
}

{/> printf(我已经长大了);
} < br:>
输出:
}

输出:
我还是个孩子
我长大了
我还是个孩子
我长大了
我还是个孩子
我长大了

为什么会有这种差异???
任何人都可以解释.... 。
It is a problem related to printf, and buffering to the printf statements
Look through this program.
main()
{
int pid;
int loop,max=3;

for(loop = 0; loop <max;loop++)
{
pid = fork();
if(pid < 0)
printf("\nThe greatest fork error of the decade");
else if(pid==0)
{
printf("\nI am a child");
goto out;
}
else
{
printf("\nI have grown up");
}
}
out:
}

output:
I am a child
I have grown up
I am a childI have grown up
I have grown up
I am a childI have grown up
I have grown up

the same program with ''\n'' placed at the end of printf
main()
{
int pid;
int loop,max=3;

for(loop = 0; loop <max;loop++)
{
pid = fork();
if(pid < 0)
printf("The greatest fork error of the decade\n");
else if(pid==0)
{
printf("I am a child\n");
goto out;
}
else
{
printf("I have grown up\n");
}
}
out:
}

output:
I am a child
I have grown up
I am a child
I have grown up
I am a child
I have grown up

why is this difference???
could anyone please explain.....




是的我可以解释:


如果你理解缓冲的概念,你会来

知道为什么上面的程序就像这样。


Streams有两种类型:缓冲和非缓冲。缓冲

流在满足以下任何条件时刷新:


*缓冲区已满

*何时''如果流是行缓冲的话,会遇到\ n''

*当调用从stdin读取的函数时

*当程序退出时

*如果默认刷新(即缓冲)行为被

修改为setvbuf(3)或setbuf(3)库函数。


然而,一旦数据到达,就会刷新unbufferd流。

记住上述事实,尝试再次追踪你的程序。


[OT]

分叉后,哪个进程(父进程或子进程)将首先执行可以

不能预料到。

[/ OT]


-

Vijay Kumar R Zanvar

我的主页 - http://www.geocities.com/vijoeyz/


200年1月28日4 01:54:36 -0800, ma******@tataelxsi.co.in (Manohar S)

在comp.lang.c中写道:
On 28 Jan 2004 01:54:36 -0800, ma******@tataelxsi.co.in (Manohar S)
wrote in comp.lang.c:
这是一个与printf相关的问题,并缓冲到printf语句
通过这个程序。
main()
{int / pid;
int loop,max = 3;

for(loop = 0;循环< max;循环++)
{
pid = fork();
It is a problem related to printf, and buffering to the printf statements
Look through this program.
main()
{
int pid;
int loop,max=3;

for(loop = 0; loop <max;loop++)
{
pid = fork();




[snip]


你的问题在这里是偏离主题的,因为在C语言或库中没有诸如
fork()之类的功能。它是由您的特定编译器和操作系统提供的非标准扩展



与语言无关。


有关此类扩展的问题应在该特定平台的支持

组中询问。在这个特殊情况下,我会建议新闻:comp.unix.programmer或

news:comp.os.linux.development.apps,取决于你的平台。


-

杰克克莱恩

主页: http://JK-Technology.Com

常见问题解答

comp.lang.c http://www.eskimo.com/~scs/C-faq/top .html

comp.lang.c ++ http://www.parashift.com/c++-faq-lite/

alt.comp.lang.learn.c-c ++
http://www.contrib.andrew.cmu.edu/ ~a ... FAQ-acllc.html



[snip]

Your question is off-topic here, as there is no such function as
fork() in the C language or library. It is a non-standard extension
provided by your particular compiler and operating system, nothing at
all to do with the language.

Questions about extensions such as this should be asked in a support
group for that particular platform. In this particular case I would
suggest either news:comp.unix.programmer or
news:comp.os.linux.development.apps, depending you your platform.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html


2004年1月28日星期三15:55:18 + 0530,Vijay Kumar R Zanvar

< vi ***** @ hotpop.com>在comp.lang.c中写道:
On Wed, 28 Jan 2004 15:55:18 +0530, "Vijay Kumar R Zanvar"
<vi*****@hotpop.com> wrote in comp.lang.c:
这是一个与printf相关的问题,并缓冲到printf语句
通过这个程序查看。
main()
{int / pid;
int loop,max = 3;

for(loop = 0; loop< max;循环++)
{
pid = fork();
It is a problem related to printf, and buffering to the printf statements
Look through this program.
main()
{
int pid;
int loop,max=3;

for(loop = 0; loop <max;loop++)
{
pid = fork();



[剪断非标准,非主题代码]

是的我可以解释:


[snip]


请不要,因为这里完全偏离主题。关于非标准扩展的非主题帖子,关于非标准扩展的关键是将

海报指向支持编译器/操作系统组合的组。
提供他们,而不是在这里提供非主题答案。

[OT]
分叉后,哪个进程(父或子)将首先执行可以
不被预料到。
[/ OT]


[snip non-standard, off-topic code]
Yes I can explain:
[snip]

Please don''t, as it is completely off-topic here. The proper thing to
do with off-topic posts about non-standard extensions is to direct the
poster to a group that supports the compiler/OS combination that
provides them, not to provide off-topic answers here.
[OT]
After forking, which process (parent or child) will execute first can
not be anticipated.
[/OT]




因为你知道它是偏离主题的并且没有分叉在C

语言,为什么污染小组?


-

Jack Klein

主页: http://JK-Technology.Com

常见问题解答

comp.lang.c http:// www。 eskimo.com/~scs/C-faq/top.html

comp.lang.c ++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c ++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html



Since you know it is off-topic and there is no forking in the C
language, why do you pollute the group?

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html


这篇关于与printf相关的问题!!为什么会这样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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