问题描述
我真的很新的C语言。我有以下问题。
I am really new to c-language. I have the following problem.
如果我用scanf()的 - 功能的程序似乎没有正确执行。我使用Eclipse和控制台窗口是空的。可是 - 当我termine的C程序一切都显示在控制台窗口了。
If I use scanf()-function the program does not seem to execute properly. I am using Eclipse and the console window is empty. BUT - when I termine the c-program everything is showing up in the console-window.
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
char c;
char s[10];
float f;
printf("Enter an integer number:");
scanf("%d",&i);
fflush(stdin);
printf("Enter string:");
scanf("%s",s);
fflush(stdin);
printf("Enter a floating number:");
scanf("%f",&f);
fflush(stdin);
printf("Enter a character:");
scanf("%c",&c);
printf("\nYou have entered \n\n");
printf("integer:%d \ncharacter:%c \nstring:%s \nfloat:%f",i,c,s,f);
getch();
}
是什么原因?
推荐答案
标准输出
,其中的printf()
写道:到时,行缓冲,所以它遇到一个 \\ n
。
stdout
, which printf()
writes to, is line buffered, so it is only flushed when encountering a \n
.
所以,让你的输入提示出现,你需要显式冲水标准输出
:
So to have your input prompts appear, you need to explictly flush stdout
:
printf("Enter an integer number:");
fflush(stdout);
scanf("%d", &i);
在程序缓冲区刷新隐含终止,这就是为什么的printf()
ED数据持续出现在节目结束在控制台上。
On termination of the program buffers are implicitly flushed, that's why printf()
ed data lasted appear on the console when the program has ended.
不过,从源头上您发布应该有打印到控制台数据后,该行一直执行:
However, from the source you post there should be data printed to the console after this line had been execute:
printf("\nYou have entered \n\n");
由于有 \\ n
秒。所以,我想你不告诉我们确切的code。
As there are \n
s. So I assume you do not show us the exact code.
这篇关于使用scanf的函数执行C-程序时,空consolewindow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!