我需要计算一个长度约为9-10位的大数字.
Msdn 称long long类型的范围为:-9,223,372,036,854,775,808至9,223,372,036,854,775,807
但是,当我运行此代码时,我得到一个垃圾值打印:
#include <stdio.h>
int main(){
int seed = 88888; //This will be always a 5 digit number so the square of
// it will be 9 or 10 digits length
long long square = seed * seed;
printf("square = %lld", square);
getchar();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:square = -688858048
seed是类型的int. seed*seed也将是类型int.而88888*88888溢出的大小int.然后,您将此垃圾值分配给您的long long.
更改计算的类型seed或投射:
long long square = ((long long) seed) * seed;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
115 次 |
| 最近记录: |