Initialize array size in C at File Scope
Solution 1:
According to the C Standard (6.7.6.2 Array declarators)
- ... If the size is an integer constant expression and the element type has a known constant size, the array type is not a variable length array type; otherwise, the array type is a variable length array type.
and (6.7.6.2 Array declarators)
2 If an identifier is declared as having a variably modified type, it shall be an ordinary identifier (as defined in 6.2.3), have no linkage, and have either block scope or function prototype scope. If an identifier is declared to be an object with static or thread storage duration, it shall not have a variable length array type.
and at last (6.6 Constant expressions)
6 An integer constant expression shall have integer type and shall only have operands that are integer constants, enumeration constants, character constants, sizeof expressions whose results are integer constants, and floating constants that are the immediate operands of casts. Cast operators in an integer constant expression shall only convert arithmetic types to integer types, except as part of an operand to the sizeof operator.
So you can use either defined named constants like
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
or enumerators
enum { SCREEN_WIDTH = 800, SCREEN_HEIGHT = 600 };
Solution 2:
When you write const int SCREEN_WIDTH = 800
, you define an object named SCREEN_WIDTH
that contains the value 800.
Because SCREEN_WIDTH
is an object, not a value, you cannot use it for a value in a constant expression. Even though its value is unchanging and obvious to us, it does not qualify as a compile-time constant.
To define an array at file scope, you must use dimensions that are constant expressions (expressions formed entirely of compile-time constants).