我想弄清楚我可以使用多少大数字作为浮点数和double.但它不存储我预期的方式,除了整数值.double应该保存8个字节的信息,足以容纳变量a,但它不能保持正确.它显示1234567890123456768最后2位数字不同.当我214783648在float变量的最后一位存储或任何数字时b,它显示相同的值214783648.这应该是极限.发生什么了?
double a;
float b;
int c;
a = 1234567890123456789;
b = 2147483648;
c = 2147483647;
printf("Bytes of double: %d\n", sizeof(double));
printf("Bytes of integer: %d\n", sizeof(int));
printf("Bytes of float: %d\n", sizeof(float));
printf("\n");
printf("You can count up to %.0f in 4 bytes\n", pow(2,32));
printf("You can count up to %.0f with + or - sign in 4 bytes\n", pow(2,31));
printf("You can count up to %.0f in 4 bytes\n", pow(2,64));
printf("You …Run Code Online (Sandbox Code Playgroud) 你不能只使用指针存储尽可能多的数据吗?你为什么要malloc()用来获得更多的记忆?
int * a;
int max, i;
printf("Enter the maximum number you want: ");
scanf("%d", &max);
for (i = 0; i < max; i++)
{
* (a + i) = i;
}
for (i = 0; i < max; i++)
{
printf("%d\n", * (a + i));
}
return 0;
Run Code Online (Sandbox Code Playgroud)
所以我让用户选择任何数字,计算机将"分配内存"吧?而不是使用以下代码:
a = (int *) malloc(10 * sizeof(int));
Run Code Online (Sandbox Code Playgroud)