Unicorn# 发表于 2020-7-26 21:49:13

整型范围问题

0. 为什么要lld才能输出4个字节的数据
#include <stdio.h>
#include <math.h>

int main(void)
{
        unsigned i;
        i = pow(2, 32)-1;
        printf("%d\n%ld\n%lld\n", i, i, i);
        return 0;
}
1.两种赋值方法的结果为什么不一样
#include <stdio.h>
#include <math.h>

int main(void)
{
        unsigned i, j, k;
        i = pow(2, 32);
        j = 32;
        k = pow(2, j);
       
        printf("%lld %lld", i, k);
       
        return 0;
}

永恒的蓝色梦想 发表于 2020-7-26 22:01:22

0.输出无符号用 %u 啦#include <stdio.h>
#include <math.h>

int main(void)
{
      unsigned i;
      i = pow(2, 32)-1;
      printf("%d\n%ld\n%lld\n", i, i, i);
      return 0;
}
1.我这里是一样的。发个截图?

Unicorn# 发表于 2020-7-26 22:04:54

本帖最后由 Unicorn# 于 2020-7-26 22:07 编辑

永恒的蓝色梦想 发表于 2020-7-26 22:01
0.输出无符号用 %u 啦
1.我这里是一样的。发个截图?

永恒的蓝色梦想 发表于 2020-7-26 22:21:43

Unicorn# 发表于 2020-7-26 22:04


我这里是这样的:0 0
C:\Users\XXX\source\repos\Project\x64\Release\Project.exe (进程 8300)已退出,代码为 0。
按任意键关闭此窗口. . .{:10_277:}

Unicorn# 发表于 2020-7-26 22:23:08

永恒的蓝色梦想 发表于 2020-7-26 22:21
我这里是这样的:

可能是编译器的原因叭
我的是dev

永恒的蓝色梦想 发表于 2020-7-26 22:24:34

Unicorn# 发表于 2020-7-26 22:23
可能是编译器的原因叭
我的是dev

那就用 Visual Studio 吧,Dev 不行{:10_256:}

巴巴鲁 发表于 2020-7-27 09:45:47

1.用pow函数结果精度会损失,一般不建议使用
如果使用把变量类型定义为double
#include <stdio.h>
#include <math.h>

int main(void)
{
      intj;
        double i, k;
      i = pow(5, 3);
      j = 3;
      k = pow(5, j);
      
      printf("%lf %lf", i, k);
      
      return 0;
}

巴巴鲁 发表于 2020-7-27 09:55:46

你可以把类型换成整形运行一下,结果会少1;
调用pow函数把result一般用double来定义

囡囡爱你哟 发表于 2020-8-4 12:48:09

pow函数返回的是double型函数

风过无痕1989 发表于 2020-8-4 13:35:53

      pow(a,b) 函数内部的参数 和 main(a,b) 函数内部的参数是不同的变量,它们占用不同的内存,它们除了名字一样,没有其他任何关系,pow() 计算的是它内部 a、b 的值,不会影响它外部(main() 内部) a、b 的值。

如下面的代码:

#include <stdio.h>

void swap(int a, int b)
{
         int temp;         //临时变量
         temp = a;
         a = b;
         b = temp;
}

int main()
{
      int a = 66, b = 99;
      swap(a, b);
      printf("a = %d, b = %d\n", a, b);
      return 0;
}

****************************************
运行结果:
a = 66, b = 99

乐乐学编程 发表于 2020-9-26 01:31:49

两个月了,问题还没解决,帮你顶上去,让大家帮你
页: [1]
查看完整版本: 整型范围问题