工作,所以我有这样的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,程序停止工作。请帮我在这里 Ç小白
工作,所以我有这样的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,程序停止工作。请帮我在这里 Ç小白
在你的代码有下面这行是错误的:
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;
}
printf(c);
应该
printf("%d\n", c); /* `\n` at the end of the string flushes the `stdout` */
因为printf
需要一个const char*
作为第一个参数,而不是一个int
。
不要忘记'scanf'可能会留下一些换行符,并且不会扫描下一个整数,导致试图将非空'int'和空值一起添加。 – Arc676
但'%d'不会扫描换行符。与空白相关的唯一格式说明符是'%c','%['和'%n'。试试这个程序。 –
你知道'printf()'的语法? – Haris