I just did a experiment yesterday, and find something confusing:
#include
int main()
{
int j;
scanf(\"%d\",&j);
const int i
For some delving into how allocating a variable amount of memory on the stack can work, see this delving into how a compiler can implement the (non-standardized) alloca() function:
Alloca implementation
The C99 standard offers Variable Length Arrays ("VLA") with essentially the same functionality; although the memory is reclaimed on a per-scope basis rather than a per-function basis:
What's the difference between alloca(n) and char x[n]?
There are some reasons to be hesitant to use either too aggressively with unbounded size. There's no way to check if stack memory is available as you can test for whether heap memory is available via. NULL from malloc(). If your variable length array is too large it will cause a stack overflow and undefined behavior; true for both methods of stack allocation:
Why is the use of alloca() not considered good practice?