Chan歪歪520 发表于 2020-1-17 17:04:50

s1e7课后作业

本帖最后由 Chan歪歪520 于 2020-1-17 17:08 编辑

请问为什么result类型是 double,为什么不可以是long long int?试验了一下 long long int ,运行后显示 为0,是为什么呢?
#include <stdio.h>
#include <math.h>

int main()
{
      int i;
      double result;
      long long int temp;

      scanf("%d", &i);

      result = pow(i,5);
      temp = pow(i,5);

      printf("the result is : %.2f\n",result);
      printf("temp is : %d\n",temp);

      return 0;
}

结果:
128
the result is : 34359738368.00
temp is : 0



我尝试将printf中%d改为%lld结果就对了,请问是为什么呢?

sunrise085 发表于 2020-1-17 17:28:57

本帖最后由 sunrise085 于 2020-1-17 17:31 编辑

因为%d输出是针对int类型的,所以只读4字节,而long long int 是8字节,需要用%lld输出。在使用%d输出的时候只读了4字节,你的编译器应该是小端存储模式,所以只读到了低4字节。你可以换一个数字试一下,就能够知道是不是只读到低4字节了。
我试了一下,输入1234.
the result is : 2861381721051424.00
temp is : 1494012192
2861381721051424->A2A69590CD120
1494012192->590CD120

Chan歪歪520 发表于 2020-1-17 22:32:43

sunrise085 发表于 2020-1-17 17:28
因为%d输出是针对int类型的,所以只读4字节,而long long int 是8字节,需要用%lld输出。在使用%d输出的时 ...

啊了解啦,谢谢您!
页: [1]
查看完整版本: s1e7课后作业