我已经开始学习C,我想做一个加法程序。 我编写了以下代码:
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main() {
int a,b,c;
scanf("Give a,b",&a,&b);
c=a + b;
printf("A+b=",c);
printf("\n\n\n");
return 0;
}
它应该从用户那里获取 2 个数字并显示它们的总和。
然而,用户写入 2 个数字后我得到的唯一输出是 A+b=
有什么想法吗?
最佳答案
您未能包含整数变量c
的格式说明符。如果没有这个,printf
就不知道格式字符串后面需要什么参数,它们的类型是什么,或者如何打印它们。变化:
printf("A+b=",c);
至:
printf("A+b=%d",c);
请注意,启用警告的良好编译器(例如 gcc -Wall ...
)会在编译时为您指出这个简单的错误。
scanf
用法是错误的 - 更改:
scanf("Give a,b",&a,&b);
至:
printf("Give a,b");
scanf("%d %d",&a,&b);
关于不会添加 C 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29602323/