In C, declaring an array size using a variable, even if it is a const variable, is NOT allowed. Ex: this fails to compile in C:
#include <stdio.h>
const int SIZE = 2;
int a[SIZE];
int main()
{
a[0] = 1;
a[1] = 2;
printf("%i, %i", a[0], a[1]);
return 0;
}
Output:
$gcc -o main *.c
main.c:5:5: error: variably modified ‘a’ at file scope
int a[SIZE];
^
In C++, however, it runs just fine.
Run the above code in C++.
Output:
$g++ -o main *.cpp
$main
1, 2
To make it run in C, you must use #define instead of a variable. ie:
This runs just fine in C OR C++:
#include <stdio.h>
#define SIZE 2
// const int SIZE = 2;
int a[SIZE];
int main()
{
a[0] = 1;
a[1] = 2;
printf("%i, %i", a[0], a[1]);
return 0;
}
So, in C++ I've almost always used a variable, rather than #define, to declare my array sizes. I just make the array size variable const and it's all good! Recently I started doing a lot of microcontroller programming in pure C, however, and when I ran into this error and figured out the problem, a senior developer told me it's bad practice to use anything but #define-ed constants (or maybe hard-coded numbers) to declare array sizes.
Is this true? Is it bad practice in C++ to use const variables instead of #define when specifying array sizes? If so, why?
In C, apparently you're stuck with #define: you have no other choice. But in C++ you clearly have at least 2 choices, so is one better than the other? Is there a risk to using one over the other?
Related:
- variably modified array at file scope in C
- static const vs #define <-- this is a solid question and very helpful. It is most definitely related to my question, but my question is NOT a duplicate because although they are both about const vs #define, my question is a very special case where one of the options doesn't even work in a language which is regularly considered to be a subset of C++. That's pretty unusual, and makes my question a more narrow subset which fits within the broad scope of this other question. Therefore, not a duplicate.
- https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#es31-dont-use-macros-for-constants-or-functions
- "static const" vs "#define" vs "enum"
- https://en.wikipedia.org/wiki/Variable-length_array#C99