Could you tell me what's wrong? When I try to compile it I see an error:Initializer element is not constant. It's about 2nd line, probably going about x.
float x = 10;
float d = x/100;
float a = 0;
Could you tell me what's wrong? When I try to compile it I see an error:Initializer element is not constant. It's about 2nd line, probably going about x.
float x = 10;
float d = x/100;
float a = 0;
In C, global variables can be initialized using only constants. Hence, the line
float d = x/100;
is not correct.
You can use preprocessor macros to accomplish your goal.
#define CONSTANT 10.0
float x = CONSTANT;
float d = CONSTANT/100;
float a = 0;
Initializers outside functions must be constants, you are not allowed to use x/100
because x could be undefined in this phase of unit translation (compilation).
As a workaround you can use a symbol to use for both declarations:
#define Value 10.0
float x = Value;
float d = Value/100;
float a = 0;
Anyway you can use this code inside a function for C99-C11 standards:
void foo(void)
{
float x = 10; //Here works
float d = x/100;
float a = 0;
...
}
P.S. I wrote the same answer as Sahu in the same time. Anyway I would point out the reasons explained in the first part.