C program compiles but does not execute

自作多情 提交于 2019-12-13 18:26:53

问题


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.

The Following is the simple code:

int main(void) {
    int a=0;
    printf("input any number");
    scanf("%d",&a);
    return (EXIT_SUCCESS);
}

And here is its compilation:

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


回答1:


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.

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

or manually cause stdout to be flushed:

printf("input any number: ");
fflush(stdout);                       // Force stdout to be flushed to the console

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.

Or, if Netbeans isn't opening a separate console window, this is all happening in one of those MDI panes of the Netbeans IDE.



来源:https://stackoverflow.com/questions/26900167/c-program-compiles-but-does-not-execute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!