以下C程序不会在屏幕上打印任何内容。为什么下面的程序不会生成任何可见的输出?
我编译的程序与gcc
:
#include<stdio.h>
main()
{
printf("hai");
for(;;);
}
以下C程序不会在屏幕上打印任何内容。为什么下面的程序不会生成任何可见的输出?
我编译的程序与gcc
:
#include<stdio.h>
main()
{
printf("hai");
for(;;);
}
最有可能的,stdout
是行缓冲。您的程序不会调用fflush
或发送换行符,以便缓冲区不会被写出。
#include <stdio.h>
int main(void) {
printf("hai\n");
for(;;)
;
return 0;
}
也看到C FAQquestion 12.4和What's the correct declaration of main()?。
为什么downvote?未能从不返回的函数中包含“return 0”? – 2009-08-11 13:14:53
你甚至没有修复他的包括错字... – 2009-08-11 13:21:03
我downvoted因为你有什么不编译。 – 2009-08-11 13:22:39
默认情况下,标准输出趋于行缓冲,因此您没有看到任何内容的原因是因为您没有刷新行。
这将工作:
#include <stdio.h>
int main (int argC, char *argV[])
{
printf("hai\n");
for(;;)
;
return 0;
}
或者,你可以fflush
标准输出或使程序退出刚刚摆脱无限循环:
#include <stdio.h>
int main (int argC, char *argV[])
{
printf("hai");
return 0;
}
,但你可能想换行反正有。
这是由stdio中发生的缓冲引起的(即,除非通过包含\ n或fflush来告诉它,否则不会立即输出)。请参阅Write to stdout and printf output not interleaved这说明了这一点。
(附注:或编译器是不是在#包括错字高兴)
您的(;;)循环停止从被刷新流。正如其他人所建议的那样,在正在输出的字符串中添加一个换行符,或者明确地刷新流:
fflush(stdout);
printf后。并纠正#include的拼写。
#include拼写错误... – richsage 2009-08-11 13:05:56
这将不会编译。 – 2009-08-11 13:06:11
是的,它不是,你的问题是什么? – qrdl 2009-08-11 13:25:45