C:如何舍入一个全局变量?
C: How do I round a global variable?
我有密码
#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 作为全局变量进行舍入。这可能吗?
不能在全局范围内调用函数,试试
#include <math.h>
double x;
int main(void)
{
x = round(3.2 / 2.0);
return 0;
}
在 C 语言中,具有静态存储持续时间的对象只能用整型常量表达式初始化。不允许在整型常量表达式中调用任何函数。
您将必须找到一种方法来通过整型常量表达式生成您的值。这样的事情可能会奏效
double x = (int) (3.2 / 2.0 + 0.5);
我有密码
#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 作为全局变量进行舍入。这可能吗?
不能在全局范围内调用函数,试试
#include <math.h>
double x;
int main(void)
{
x = round(3.2 / 2.0);
return 0;
}
在 C 语言中,具有静态存储持续时间的对象只能用整型常量表达式初始化。不允许在整型常量表达式中调用任何函数。
您将必须找到一种方法来通过整型常量表达式生成您的值。这样的事情可能会奏效
double x = (int) (3.2 / 2.0 + 0.5);