2015-10-24 47 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'。试试这个程序。 –