C程序编译但不执行

标签 c netbeans-7 netbeans-plugins

我成功地为 C 安装了 NetBeans,但我不知道出了什么问题,因为每当我编写任何代码时,它都会显示“构建成功”,但它不会执行。 当我点击运行按钮时没有任何反应,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)
""

我该怎么办? 提前致谢

最佳答案

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 面板之一中。

关于C程序编译但不执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26900167/

相关文章:

c - 使用函数反转 C 中的字符串

c - 在 Linux 上从消息队列读取时出现错误 "Bad address"

java - 由 netbeans 构建的 JAR 文件未执行

android - NetBeans Android 插件出错

java - 我无法通过maven获取jar文件

创建数据包时编译错误

c - 处理程序的阻塞信号

java - NetBeans 7.1 构建错误 : java. lang.UnsatisfiedLinkError libXtst.so.6: 错误的 ELF 类:ELFCLASS64

tomcat - 在 Netbeans 中查看 Tomcat 应用程序日志文件

java - NetBeans 模块中的操作 - 上下文菜单、主菜单