Question: How can I ensure COLOR\_NAMES is filled to the correct size at compile-time? If a new color is added, say COLOR\_4 (and hence N\_COLORS is auto incremented), the compiler will then tell me COLOR\_NAMES is not filled to size.

Most answers that I found online is for runtime, not compile time.

This is for C-style notation (no STL and other libraries usage).

```
enum Colors
{
   COLOR_1,
   COLOR_2,
   COLOR_3,
   N_COLORS;
};

const char* COLOR_NAMES[N_COLORS] =
{
   /* COLOR_1 */ "Color1",
   /* COLOR_2 */ "Color2",
   /* COLOR_3 */ "Color3"
};

const char* Blah()
{
   Colors color;
   ...
   printf("%s blah blah\n", COLOR_NAMES(color));
}

```

Answer: 
It is standard practice for such cases of arrays and corresponding enums to compare the "enum size member", `N_COLORS` in your case, against the number of items in the array.

To get the number of items in an array, simply divide the array size with the size of one array member.

Thus:

```
_Static_assert(sizeof(COLOR_NAMES)/sizeof(*COLOR_NAMES) == N_COLORS, 
               "Array item missing!");

```

**Edit:**

Oh btw for this to be meaningful the array declaration must be `const char* COLOR_NAMES[] =` otherwise you wouldn't be able to tell if there are missing initializers in the array initialization list.
The ideal would to be able to use sizeof from preprocessor. But we can't because sizeof is evaluated by the compiler.

There are many ways to bypass this but here is quite simple and portable one:

```
const char* COLOR_NAMES[] = {
   /* COLOR_1 */ "Color1",
   /* COLOR_2 */ "Color2",
   /* COLOR_3 */ "Color3"
};

typedef char CHECK_COLOR_NAMES[sizeof(COLOR_NAMES) / sizeof(COLOR_NAMES[0]) == N_COLORS ? 1 : -1];

```

If the test fails, you will attempt to define an array with size -1, which will result into a compilation error.

EDIT: Then we use a typedef to avoid to actually create a variable that we'll not use (Lundin's remark)