这是我的代码
#include<stdio.h>
#include<math.h>
void main(void)
{
printf("pow as double: %lf\n\r", pow(2,32));
printf("pow as long int: %ld\n\r", ((long int)pow(2,32)));
}
我在2个不同的Linux操作系统上编译了代码。 (gcc powfn.c -o powfn)
在VirtualBox Ubuntu上,我得到了以下结果
pow as double: 4294967296.000000
pow as long int: 4294967296
在Znyq ARM Cortex A9处理器上运行的Debian GNU / Linux 8操作系统上,我得到了以下结果
pow as double: 4294967296.000000
pow as long int: 2147483647
发生了什么事?为什么两个不同的结果?
答案 0 :(得分:1)
两个处理器极有可能针对相同的数据类型具有不同的大小。您可以通过在两台计算机上编译和运行此代码来测试它:
#include <stdio.h>
int main()
{
int integerType;
long int longIntType;
float floatType;
double doubleType;
char charType;
// Sizeof operator is used to evaluate the size of a variable
printf("Size of int: %ld bytes\n",sizeof(integerType));
printf("Size of long int: %ld bytes\n",sizeof(longIntType));
printf("Size of float: %ld bytes\n",sizeof(floatType));
printf("Size of double: %ld bytes\n",sizeof(doubleType));
printf("Size of char: %ld byte\n",sizeof(charType));
return 0;
}
以下是在Wandboard with Cortex-A9和Ubuntu 15.10上运行程序的结果:
wandboard:~$ ./test.exe
Size of int: 4 bytes
Size of long int: 4 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of char: 1 byte