0

I've been trying to create static consts using extern consts declared in an included .h file and defined in an associated .c file. However the compiler complains that the initializer element is not constant, as if the extern invalidated constants. So I was wondering if there was a way to use these extern consts in the definition of other consts without this error.

Here's the code:

consts.c

const float G = 6.67408E-11;
const int metre = 10;
const int window_width = 500;
const int window_height = 500;
const float seconds_per_frame = 1.0f/60.0f;
const float frames_per_second = 60.0f;

consts.h

extern const float G;
extern const int metre;
extern int window_width;
extern const int window_height;
extern const float seconds_per_frame;
extern const float frames_per_second;

particle.c

#include "../constants/consts.h"

static const float max_position_x = window_width; 
static const float max_position_y = window_height; //Errors in these three statements
static const float max_speed = 5 * metre;          //"initializer element is not constant"
(...)

And before you ask, I have linked with consts.c and can use any of the consts defined there inside particle.c no problem. It's just trying to use them for consts that causes an error.

Ian vos
  • 39
  • 5
  • Does this answer your question? [Error "initializer element is not constant" when trying to initialize variable with const](https://stackoverflow.com/questions/3025050/error-initializer-element-is-not-constant-when-trying-to-initialize-variable-w) – kaylum Nov 03 '21 at 02:31
  • It was very helpful, thanks!. I don't know how I'd fix my problem though. I want to initialize these constants in particle.c from those defined in consts.h. – Ian vos Nov 03 '21 at 02:39
  • "I've been trying to create static consts using extern consts" That doesn't make any sense, `static` and `extern` are pretty much each other's opposites. It's like saying "I'd like to do this project in-house by outsourcing it to external consultants". You should study `static` and `extern` before doing anything else with this code. – Lundin Nov 03 '21 at 12:49

1 Answers1

1

In C, variables declared at file scope may only be initialized with a constant expression, which loosely speaking means a compile time constant. A variable declared as const is not considered a constant expression, so it cannot be used to initialize file scope variables.

The way around this is to use #define macros for these values instead. A macro does direct token substitution so that they may be used where a constant expression is required.

So your header file would contain this:

#define G 6.67408E-11
#define metre 10
#define window_width 500
#define window_height 500
#define seconds_per_frame (1.0f/60.0f)
#define frames_per_second 60.0f

And consts.c would not be necessary.

dbush
  • 205,898
  • 23
  • 218
  • 273
  • Sweet! thanks a lot! I didn't know macros carried across files like this... – Ian vos Nov 03 '21 at 02:45
  • @Ianvos yes, anytime you `#include` another file, it behaves as if the other file was copy/pasted at that location. – dbush Nov 03 '21 at 02:46