2

I would like to declare a constant global variable and initialize it with a function. The problem is, I can't use my function to initialize it because a function call is not a compiler-time constant, but I also can't split the declaration and the initialization as it is a const variable.

For example, the following codes are invalid.

#include <stdio.h>

int my_function(void){
    return 42;
}

const int const_variable = my_function();//invalid: my_function() is not
                                         //a compiler-time constant

/*main() here*/
#include <stdio.h>

const int const_variable;
const_variable = 10;//invalid: can't modify a const-qualified type (const int)

/*main() here*/

I looked for some answers. I saw that some people suggested the use of pointers to modify the value of a const variable after its declaration, but this could cause severe readability, portability and debugging problems.

Is there any simple, portable way to solve my problem?

5
  • What you're trying to do is simply not valid C. Variables in the global scope (outside any functions) simply can't be initialized at run-time. Commented Mar 24, 2022 at 10:36
  • The only thing you can do with a const variable is initializing it upon declaration with a constant value like this: const int const_variable = 10; Commented Mar 24, 2022 at 10:37
  • Ok… Thank you for your comments! I'll try to find another way to do what I want. Commented Mar 24, 2022 at 10:41
  • Hobbes, what is the real function calculating? Certainly not just 42. Post the code of that function. Perhaps there is a way to do that at compile time. Commented Mar 24, 2022 at 10:55
  • It is a SDL2 function, SDL_GetScancodeFromName(). I'll try and get the source Commented Mar 24, 2022 at 18:18

2 Answers 2

2

How to declare a global const variable and initialize it with a function in C?

There is no way. It is not possible.

Is there any simple, portable way to solve my problem?

No.

You can use a macro #define MY_FUNCTION() (42).

You can write a separate program, that uses that C function (or is written in a completely different language), that generates C source code with that constant and that generated source code is then compiled with your project.

Or you can switch to a different programming language with more features, for example Rust, D, C++.

Sign up to request clarification or add additional context in comments.

1 Comment

Not sure if the macro approach is helpful....
1

Global consts don't work like that in C.

If a global scope variable needs protection against any further change, that's a sign you need to encapsulate it.

Create a protected_variable_setter and a getter. Make the setter known only to the file where the variable is initialized. Then access the variable only through the getter.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.