Array problem . how we can execute the index which are not within the size of array

when i am giving a array a finite siza already then why it execyting the variable which are outside the array limit.
when I try to print the value outside the limit of array…it printing some value even I can assign a value to that array index greater than the size of an array.

This question also ask to me in my college viva, I told them the result but I can not explain it. please help me with this.

Hi Kartikey
please elaborate your question maybe by giving some examples.

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.

example int a[10];

Example> int a[10]; cout<<a[100]; for this I expected no output…but my system is giving some output.why this is happening?

This is happening due to what we call undefined behavior.

An undefined behavior (UB) is a result of executing computer code whose behavior is not prescribed by the language specification to which the code can adhere to, for the current state of the program (e.g. memory). This generally happens when the translator of the source code makes certain assumptions, but these assumptions are not satisfied during execution.
In simple word, there is no such restriction mentioned or specified in C’s language specification.

Languages like java have some functionality implemented that generates exception if someone access an array out of bound/defined range to prevent such access.

But, in case of C/C++ there is no such functionality preventing access. Thus, your code is returning a garbage value from an undefined memory location.

Hope, this would help.
Give a like, if you are satisfied.

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.