【发布时间】:2015-06-01 14:24:09
【问题描述】:
我有密码
#include <stdio.h>
#include <math.h>
double x = round(3.2/2.0);
int main()
{
printf("%f", x);
}
当我尝试编译时,我收到错误 initializer element is not a compile-time constant。如果没有round,它可以顺利编译。
我想将 x 作为全局变量进行舍入。这可能吗?
【问题讨论】:
-
如果你知道确切的值,(round(3.2 / 2.0) = 2.0),你为什么还要在代码中进行这样的计算呢?而不仅仅是
double x = 2.0 // Got this value from round(3.2 / 2.0)? -
有趣的是,GCC 只发出警告而不是错误,当使用 __builtin_round 时,它甚至不会这样做。但我猜(也有点希望)非可移植的 GCC-only 代码是不行的。
标签: c compiler-errors global-variables rounding compile-time