这个C代码
unsigned long int a, b, x;
float c, d, y;
a = 6;
b = 2;
x = a/b;
c = 6.0;
d = 2.0;
y = c/d;
printf("\n x: %d \n y: %f \n",x,y);
Run Code Online (Sandbox Code Playgroud)
工作正常并打印出来
x: 3
y: 3.000000
Run Code Online (Sandbox Code Playgroud)
但是,当我将第一行更改为此时
unsigned long long int a, b, x;
Run Code Online (Sandbox Code Playgroud)
我得到这个输出:
x: 3
y: 0.000000
Run Code Online (Sandbox Code Playgroud)
这真让我高兴...我没有用c,d和y改变任何东西 - 为什么我得到这个?我在linux上使用gcc
对于后者使用:
printf("\n x: %llu \n y: %f \n",x,y);
Run Code Online (Sandbox Code Playgroud)
使用u无符号积分(你的输出是唯一正确的,因为你使用较小的值).使用ll修饰符进行long longs否则printf将使用错误的大小来解码printf的第二个参数(x),因此它使用错误的地址来获取y.
| 归档时间: |
|
| 查看次数: |
229 次 |
| 最近记录: |