有个奇怪的程序请教各位大哥.在线急求答

来源:百度知道 编辑:UC知道 时间:2024/05/22 13:36:24
#include "stdio.h"
main()
{
int x, y, z, result;

scanf ("%d%d%d", &x,&y,&z);
result = x*y*z;
printf ("The result is %d\n",result);
}
是三个数的积的程序,它运行20范围内的三个数,结果是对的,但如果大于50的三个数它的结果是错的.为什么?

结果越界了.

unsigned result吧.

#include<iostream>
using namespace std;
int main()
{ int x=0,
y = 0,
z = 0;
unsigned result=0;

scanf ("%d%d%d", &x,&y,&z);
result = x*y*z;
printf ("The result is %d\n",result);

return 0;
}

自动分配的空间太小,存不下,溢出了?

整形最大值为65535
三个50乘起来是125000,越界了~答案当然是错的

是不是超过整型范围了!

result改用long类型即可

int型最大值为65535 3个大于50的数相乘大于这个范围溢出啦。
main()
{
int x, y, z;
long result;

scanf ("%d%d%d", &x,&y,&z);
result = x*y*z;
printf ("The result is %d\n",result);
}