程序不“看起来”打印“hello-out” [英] program doesn't "seem" to print "hello-out"
问题描述
以下程序并不看起来。打印hello-out。 (尝试
执行它)
#include< stdio.h>
#include< unistd.h>
int main()
{
而(1)
{
fprintf (stdout,hello-out);
fprintf(stderr,hello-err);
sleep(1);
}
返回0;
}
可能是什么原因?
The following program doesn''t "seem" to print "hello-out". (Try
executing it)
#include <stdio.h>
#include <unistd.h>
int main()
{
while(1)
{
fprintf(stdout,"hello-out");
fprintf(stderr,"hello-err");
sleep(1);
}
return 0;
}
What could be the reason?
推荐答案
saurabhnsit2001写道:
saurabhnsit2001 wrote:
以下程序并不看起来像打印hello-out。 (尝试
执行它)
#include< stdio.h>
#include< unistd.h>
int main()
{
而(1)
{
fprintf (stdout,hello-out);
fprintf(stderr,hello-err);
sleep(1);
}
返回0;
}
可能是什么原因?
The following program doesn''t "seem" to print "hello-out". (Try
executing it)
#include <stdio.h>
#include <unistd.h>
int main()
{
while(1)
{
fprintf(stdout,"hello-out");
fprintf(stderr,"hello-err");
sleep(1);
}
return 0;
}
What could be the reason?
如果你在while循环中添加一行
fflush(stdout);
,你会发现真正发生的事情。
if you add a line
fflush(stdout);
in the while loop , you will find out what really happenen.
输出到stderr尽快消失,输出到stdout经常被缓冲。
如果你等了足够长的时间,你会看到输出出现。
output to stderr goes out ASAP, output to stdout is often buffered up.
If you wait long enough you''ll see the output appear.
saurabhnsit2001写道:
saurabhnsit2001 wrote:
>
以下程序并不看起来。打印hello-out。 (尝试
执行它)
#include< stdio.h>
#include< unistd.h>
int main()
{
而(1)
{
fprintf (stdout,hello-out);
fprintf(stderr,hello-err);
sleep(1);
}
返回0;
}
可能是什么原因?
>
The following program doesn''t "seem" to print "hello-out". (Try
executing it)
#include <stdio.h>
#include <unistd.h>
int main()
{
while(1)
{
fprintf(stdout,"hello-out");
fprintf(stderr,"hello-err");
sleep(1);
}
return 0;
}
What could be the reason?
可能它甚至不应该在标准C
编译器上编译。在标准C(这里讨论的主题)中没有< unistd.h>,也没有睡眠
函数。
如果消除那些东西,导致:
#include< stdio.h>
int main(无效)
{
while(1){
fprintf(stdout," hello-out");
fprintf(stderr," hello -err);
}
返回0;
}
从来没有任何由于输出中任何''\ n''缺少
,也没有任何fflush(stdout)调用,因此出现任何输出的原因。 stderr
是无缓冲的,所以它的输出可能会出现。使用''\ n''终止
字符串。
-
一些信息链接:
news:news.announce.newusers
http:// www。 geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html
Probably the fact that it shouldn''t even compile on a standard C
compiler. There is no such thing as <unistd.h>, nor a sleep
function, in standard C (which is the subject of discussion here).
If you eliminate those things, resulting in:
#include <stdio.h>
int main(void)
{
while(1) {
fprintf(stdout, "hello-out");
fprintf(stderr, "hello-err");
}
return 0;
}
there is never any reason for any output to appear, due to the lack
of any ''\n'' in the output nor any calls to fflush(stdout). stderr
is unbuffered, so it''s output will probably appear. Terminate the
strings with ''\n''.
--
Some informative links:
news:news.announce.newusers
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html
这篇关于程序不“看起来”打印“hello-out”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!