C程序编译但不执行 [英] C program compiles but does not execute
问题描述
我成功安装了NetBeans for C,但我不知道出什么问题了,因为每当我编写任何代码时,它都会说"build success",但不会执行. 当我按下运行按钮时,什么都没有发生,Netbeans只是编译代码,但屏幕上什么都没有显示.
I installed NetBeans for C successfully, but I don't know what is wrong because whenever I write any code it says "build successful" but it does not execute. Nothing happens when I hit the run button, Netbeans just compiles the code but nothing is displayed on screen.
以下是简单的代码:
int main(void) {
int a=0;
printf("input any number");
scanf("%d",&a);
return (EXIT_SUCCESS);
}
这是它的编译:
""/C/MinGW/msys/1.0/bin/make.exe" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make.exe[1]: Entering directory `/c/Users/timekeeper/Documents/NetBeansProjects/ft'
"/C/MinGW/msys/1.0/bin/make.exe" -f nbproject/Makefile-Debug.mk dist/Debug/MinGW-Windows/ft.exe
make.exe[2]: Entering directory `/c/Users/timekeeper/Documents/NetBeansProjects/ft'
mkdir -p build/Debug/MinGW-Windows
rm -f "build/Debug/MinGW-Windows/main.o.d"
gcc -std=c99 -c -g -MMD -MP -MF "build/Debug/MinGW-Windows/main.o.d" -o build/Debug/MinGW-Windows/main.o main.c
mkdir -p dist/Debug/MinGW-Windows
gcc -std=c99 -o dist/Debug/MinGW-Windows/ft build/Debug/MinGW-Windows/main.o
make.exe[2]: Leaving directory `/c/Users/timekeeper/Documents/NetBeansProjects/ft'
make.exe[1]: Leaving directory `/c/Users/timekeeper/Documents/NetBeansProjects/ft'
BUILD SUCCESSFUL (total time: 34s)
""
我该怎么办? 预先感谢
What should i do? Thanks in advance
推荐答案
stdout
流是行缓冲的.这意味着您遇到fwrite
或printf
等到stdout
的任何内容,直到遇到换行符(\n
)时,才真正写入到终端.
The stdout
stream is line-buffered. That means whatever you fwrite
or printf
, etc. to stdout
will not actually be written to your terminal until a newline character (\n
) is encountered.
因此,您的程序将字符串缓冲了,并在scanf
上被阻塞,等待您从stdin
输入.一旦发生这种情况,您的控制台窗口就会关闭,并且您再也看不到打印内容.
So your program has your string buffered, and is blocked on the scanf
, waiting for your input from stdin
. As soon as this happens, your console window is closing, and you never see the print.
要解决此问题,请在字符串末尾添加换行符:
To remedy this, either add a newline character at the end of your string:
printf("input any number:\n"); // Newline at end of string
或手动导致刷新stdout
:
printf("input any number: ");
fflush(stdout); // Force stdout to be flushed to the console
此外,我假设(total time: 34s)
数字包括程序等待您键入内容的时间.您非常耐心,大约34秒后,终于将键盘上的东西捣碎了,然后程序结束并且控制台窗口关闭了.
Furthermore, I'm assuming that the (total time: 34s)
figure includes the time the program was waiting for you to type something. You were incredibly patient, and after ~34 seconds, finally mashed something on the keyboard, after which the program ended and the console window closed.
或者,如果Netbeans没有打开单独的控制台窗口,则这全部发生在Netbeans IDE的那些MDI窗格中.
Or, if Netbeans isn't opening a separate console window, this is all happening in one of those MDI panes of the Netbeans IDE.
这篇关于C程序编译但不执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!