2015-10-24 48 views
-3

工作,所以我有這樣的C代碼C程序停止使用後的scanf

#include <stdio.h> 
#include <stdlib.h> 

int main() 
{ 
    int a; 
    int b; 
    int c; 
    scanf("%d", &b); 
    scanf("%d", &a); 
    c = a + b; 
    printf(c); 
    return 0; 
} 

但是,當我插入數a和b,程序停止工作。請幫我在這裏 Ç小白

+4

你知道'printf()'的語法? – Haris

回答

0

在你的代碼有下面這行是錯誤的:

printf(c); 

的printf()語法會像什麼,我已經寫了下面

printf("%d",c); 

所以你現在的代碼將是:

#include <stdio.h> 

int main() 
{ 
    int a; 
    int b; 
    int c; 
    scanf("%d", &b); 
    scanf("%d", &a); 
    c= a + b; 
    printf("%d",c); //this is the correct printf() syntax 
    return 0; 
} 
+0

空格大大提高了可讀性。 –

+0

爲什麼呢? C *忽略*空格。 –

+0

哦,我看到了...我以爲c會給出一個錯誤... – Cherubim

0
printf(c); 

應該

printf("%d\n", c); /* `\n` at the end of the string flushes the `stdout` */ 

因爲printf需要一個const char*作爲第一個參數,而不是一個int

+0

不要忘記'scanf'可能會留下一些換行符,並且不會掃描下一個整數,導致試圖將非空'int'和空值一起添加。 – Arc676

+0

但'%d'不會掃描換行符。與空白相關的唯一格式說明符是'%c','%['和'%n'。試試這個程序。 –