2015-08-09 58 views
1

我有一個簡單的C程序簡單的C程序不打印輸出

#include <stdio.h> 

int add(int, int); 
int add (int a, int b) { 
    return a+b; 
} 

int main(void) { 
    int a, b, c; 

    printf("Enter the 1st number "); 
    scanf("%d",&a); 
    printf("Enter the 2nd number "); 
    scanf("%d",&b); 
    c = add(a, b); 
    printf("The value is %d ", c); 
    return (0); 
} 

我編譯程序與cc main.c ,當我與./a.out
運行的程序,我沒有得到任何輸出在安慰。

+4

它在我的系統使用gcc – Haris

+0

@FFgTYnh是什麼我後a.out的意思是完美運行? –

+2

** C不是C++不是C!** – Olaf

回答

1

出於性能原因,輸出被緩衝。更換

printf("The value is %d ", c); 

printf("The value is %d\n", c); 

或使用fflush(stdout);

Why does printf not flush after the call unless a newline is in the format string?

+1

它仍然會在程序結束時刷新。 –

+0

我不知道爲什麼,但有時換行符不夠。我正在運行mingw gcc(不記得確切的版本,但它可能是4.9.2),即使在字符串末尾有一個換行符,也不會輸出它,除非調用fflush(stdout)。 P.S.誰downvoted?這是一個很好的答案,不是嗎? – kbau