Doubt regarding code compilation and need of dynamic allocation of array

It’s said that we need the size of array while compilation only and thus if we did not know the size of the array while compilation then use dynamic allocated arrays. If it is so, then why does this piece of code work

It’s a simple code. Why does this have been working all this time

yes it works but not recommended to do so
because N is not defined at compile time
it contains garbage at compile time

so random size of array is made it will work in most of cases
but not on all cases

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.