簡體   English   中英

C程序編譯但不執行

[英]C program compiles but does not execute

我已成功安裝了NetBeans for C,但我不知道出什么問題了,因為每當我編寫任何代碼時,它都會說“ build success”,但不會執行。 當我按下運行按鈕時,什么都沒有發生,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流是行緩沖的。 這意味着在遇到換行符( \\n )之前,無論您使用fwrite還是printf等將其stdoutstdout實際上都不會寫入到終端。

因此,您的程序對字符串進行了緩沖,並在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窗格中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM