While I'm executing this snippet, I'm getting the output as 2.
Is 2 a garbage value here? I don't think so...
Can anyone explain how 2 comes instead of a garbage value?
Any answer could be appreaciated.
2 is the representation of what just happen to be in memory at the location that a points to (array out of bounds) Your array ends at a, not a. Don't confuse it with a, which you assigned a value of 2.