Tar*_*ngh 0 c printf initialization scanf
我编写了一个简单的程序,将两个数字相乘。当我将它与 scanf 输入相乘时,它会打印出随机答案......
#include<stdio.h>
int main()
{
int a, b ,c = a * b;
printf("Type two no.s to be multiplied, ensuring space between them");
scanf("%d%d", &a, &b);
printf("The required output is = %d\n", c);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我输入 8 和 7 时,我得到 4897 作为我的答案,那么答案一定是 56。
C 不是 Excel。这:
c = a * b
Run Code Online (Sandbox Code Playgroud)
并不意味着c将始终包含 的值a * b。这意味着您要设置为代码中该点当前c的值。a * b
在读取和 的值后,您需要将其移至。ab
int a, b ,c;
printf("Type two no.s to be multiplied, ensuring space between them");
scanf("%d%d", &a, &b);
c = a * b;
printf("The required output is = %d\n", c);
Run Code Online (Sandbox Code Playgroud)
问题是你的操作顺序不正确......你必须在scanf之后进行乘法:
#include <stdio.h>
int main()
{
int a, b, c;
printf("Type two numbers to be multiplied, ensuring space between them: ");
scanf("%d%d", &a, &b);
c = a * b;
printf("The required output is = %d\n", c);
return 0;
}
Run Code Online (Sandbox Code Playgroud)