Reputation: 173
I have the code
#include <stdio.h>
#include <math.h>
double x = round(3.2/2.0);
int main()
{
printf("%f", x);
}
When I try to compile, I get the error initializer element is not a compile-time constant. Without round
, it compiles without a hitch.
I want to round x while having it as a global variable. Is this possible?
Upvotes: 0
Views: 147
Reputation: 53016
You can't call a function in the global scope, try
#include <math.h>
double x;
int main(void)
{
x = round(3.2 / 2.0);
return 0;
}
Upvotes: 2
Reputation: 320631
In C language objects with static storage duration can only be initialized with integral constant expressions. You are not allowed to call any functions in integral constant expressions.
You will have to find a way to generate your value through an integral constant expression. Something like this might work
double x = (int) (3.2 / 2.0 + 0.5);
Upvotes: 3