Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'Initializer not constant' on global variable?

So I get the 'initializer element not constant' error when compiling the following code:

#include <stdlib.h>
#include <stdio.h>
#include <math.h>

float wl = 2.0f;
float k = 2.0f * (float) M_PI / wl;

int main ()
{
     //Do stuff
}

If I move "float k" inside the main method, there's no errors, but this isn't an option for me, because I NEED float k to be a global variable. Even if I change it to this:

const float wl = 2.0f;
const float k = 2.0f * (float) M_PI / wl;

the error still happens. How do I fix this?

like image 508
Gribbles Chan Avatar asked Apr 13 '14 08:04

Gribbles Chan


People also ask

What does initializer element is not constant mean?

It's not inside a function, which means it has to be an initializer - which is assigned only when the item is declared - which means it must be a constant value at compile time, which malloc cannot be.

Do global variables have to be constant?

Global variables aren't constant (you can change the value of a global variable, but you can only define a constant once). Constants aren't always global (you can declare a constant in a class).

Do global variables need to be initialized?

In C language both the global and static variables must be initialized with constant values. This is because the values of these variables must be known before the execution starts. An error will be generated if the constant values are not provided for global and static variables.

Are global variables always initialized to 0?

Yes, all members of a are guaranteed to be initialised to 0. If an object that has static storage duration is not initialized explicitly, it is initialized implicitly as if every member that has arithmetic type were assigned 0 and every member that has pointer type were assigned a null pointer constant.

Is it possible to initialize global values with non constant values?

C does not allow initialization of global values with non constant values. C99 Standard: Section 6.7.8: All the expressions in an initializer for an object that has static storage duration shall be constant expressions or string literals. Show activity on this post.

How to initialize static and global variables in C++?

In C, static and global variables are initialized by the compiler itself. Therefore, they must be initialized with a constant value. Note that the above programs compile and run fine in C++, and produce the output as 10. As an exercise, predict the output of following program in both C and C++. This article is contributed by Shankar Shastri.

What happens if a constant is not used to initialize variables?

If constants are not used to initialize the global and static variables, this will lead to an error. A program that demonstrates this is as follows. The above program leads to a error “initializer element is not constant”.

What is the C standard for initialization of global objects?

The C standard prohibits initialization of global objects with non-constant values. Section 6.7.8 of the C99 standard states: All the expressions in an initializer for an object that has static storage duration shall be constant expressions or string literals.


1 Answers

According to C99 standard:

§6.7.8 Initialization

  1. All the expressions in an initializer for an object that has static storage duration shall be constant expressions or string literals.

Using const doesn't help here because, in C, const variables are not really const. Check out this post for more details.


To work out, you can make wl constant by using preprocessor:

#define wl 2.0f

By doing this, 2.0f * (float) M_PI / wl can be a compile time constant.

like image 121
herohuyongtao Avatar answered Sep 25 '22 13:09

herohuyongtao