C程序编译但不执行

时间:2014-11-13 02:07:20

标签: c netbeans-7 netbeans-plugins

我成功安装了NetBeans for C,但我不知道出了什么问题,因为每当我编写任何代码时,它就会说“#34;构建成功"但它没有执行。 当我点击运行按钮时没有任何反应,Netbeans只编译代码,但屏幕上没有显示任何内容。

以下是简单的代码:

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)
""

我该怎么办? 提前致谢

1 个答案:

答案 0 :(得分:1)

stdout流是行缓冲的。这意味着无论您fwriteprintf等等,stdout等都不会在遇到换行符(\n)之前实际写入您的终端。

因此,您的程序会缓冲您的字符串,并在scanf上被阻止,等待stdin的输入。一旦发生这种情况,您的控制台窗口就会关闭,您永远不会看到打印。

要解决此问题,请在字符串末尾添加换行符:

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秒后,终于在键盘上捣碎了一些东西,然后程序结束,控制台窗口关闭。

或者,如果Netbeans没有打开一个单独的控制台窗口,这一切都发生在Netbeans IDE的其中一个MDI窗格中。

相关问题