When announcing

const double X_max = 8701859.625360, X_min = -8701859.625360; double X_Curr = X_max, X_Step = 100021.375000, Y_Curr = 7641389.500000, H_Curr = 84.972000; 

the compiler swears at the second line:

initializer element is not constant

I understand that, most likely, the mistake is stupid, but still tell me, please, what am I doing wrong?

  • You should start with a simplification of the example: remove the excess, saving the error. So you will understand what is happening. const double X_max = 8701859.625360; double X_Curr = X_max; - Ilya
  • You have not fully described the context. The key point in this case is that these variables are declared at the file level. There would be no such error in the local declaration. - AnT 2:21 pm

3 answers 3

The subtlety is this: in C, variables with the const modifier are not in the full sense constant (which we are used to in C ++). Constant expressions must be constant at compile time, which is not the case in your example. The traditional way out of this situation is:

 #define X_max 8701859.625360 double X_Curr = X_max; 
  • What's the difference? The macro is still deployed by the preprocessor, and the compiler in any case gets the assignment of a prime number. - ߊߚߤߘ
  • Well, yes, the macro unfolds without problems. And the under-constant variable doesn't work like this in C (here in C ++ it is allowed to write that, which is natural and logical). - Ilya
  • @Arhad: So we want to get a "assignment of a prime number." The “prime number” in C is a constant , but the const variable is not. - AnT

You have not fully described the context. The key point in this case is that these variables are declared at the file level and, therefore, have a static storage duration. In C, all objects with a static storage class can be initialized only by constant expressions.

Variables declared with const in C are not constant expressions. Therefore initialization

 double X_Curr = X_max, ... 

(and only this one) is erroneous.

For this reason, it is preferable to use #define or enum , rather than const to declare named constants in const

The same declaration, but made inside the block, will not lead to such an error, because the requirement of initialization by constants does not apply to it. But it is worth adding the static keyword there, as the error will occur again.

    This is actually not a compiler error, but a flaw in the C standard ... This garbage does not allow to write a very important functionality, namely, the initialization constructor. This is a brake on why people refuse C and go to C ++, although there is no other reason. Then you got out ... But if you try to write an initialization function, then the trick with you will not roll. And you are at an impasse. You need to call a new function before main and initialize it, but the language does not allow it. True, the developers of gcc have written extensions and, in particular, attributes. This can help. It is attribute ((constructor)) before the function name that allows it to run before main if it is an executable binary. In a shared module (dll in Windows or so in Linux) you will have to add instructions to the linker on the functions that need to be run when the module is connected and disconnected. You can of course write a function just to pull it, but this is inconvenient. Conveniently when everything is done by itself in the background. By the way, there is an attribute destructor. But there is no need for it, because you can simply add atexit (destructor) to the constructor and it will start. It is strange that this is not provided for in the development of syntax and is not added to the language itself. But there is an urgent need for this. That's what the Americans developed ... Even simple things are not thought out!

    • one
      Please, when answering the question, do not stay concise. This resource is not a forum. - 0xdb
    • I do not quite understand the meaning of this "cry of the soul" here. Practically all decisions made during the development of the language have concrete explanations, good reasons and are based primarily on the platform constraints that existed during the development of the language. - AnT