int a = 1920;
int b = 1080;
int c = 2000 * (a*b)/(1920*1080);
c 变量这样赋值对吗???? (a*b)会不会被系统默认转换成 long long int??
最好不要这样用int,容易出错,可以先返回a*b,再赋值之后进行c的赋值
运行结果
源码:
#include <stdio.h>
int multiply( int a ,int b)
{
return (a*b);
}
int main()
{
int x, y, product,c;
printf("please input two integers:");
scanf("%d%d",&x, &y);
product=multiply(x,y);
printf("The product is %d\n", product);
c=2000*product/(1920*1080);
printf("c=%d\n",c);
return 0;
}
追问出错的原因是什么呢?我在代码中发现这样的BUG,但不知道为什么,是不是int之间的乘法运算隐式转换成了long long int ,再赋值给int后数据丢失??
追答a*b还是int的,我说最好不要这样弄是为了防止溢出,你可以试一下这个,是可以运行的
源码:
#include<stdio.h>
int main()
{
int a = 1920;
int b = 1080;
int c = 2000*(a*b)/(1920*1080);
printf("%d\n",c);
return 0;
}
主要是如果你是大数运算的话就会溢出,运行结果会不符合实际
另外,乘积的结果不能大于 int 的定义域,否则会被系统截位,出现系统不提示的错误。