Consider:
#include <stdio.h>
int const a = 9;
int stack[a];
int main()
{
return 0;
}
The above code gives an error:
variably modified 'stack' at file scope
But when I change the code to:
#include <stdio.h>
#define b 3
int stack[b];
int main()
{
return 0;
}
It compiles without error. While both #define and const variable are used for defining the constant identifier then why is there an error when I use the const var instead of #define?
I searched the similar questions, but they all gave a solution about the error, but no reason to it.
I searched about the const
and #define
and found that sometimes the GCC compiler recognizes const
as read-only, but it is too confusing.