int a[3]={1,2,3};
printf("%d",a[3]);

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.

Is 2 a garbage value here?

Yes.

Can anyone explain how 2 comes instead of a garbage value?

If you're expecting something predictable for garbage, then your definition of "garbage" is badly flawed.

int a[3]={1,2,3};
printf("%d",a[3]);

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[3] points to (array out of bounds) Your array ends at a[2], not a[3]. Don't confuse it with a[1], which you assigned a value of 2.

Its just the garbage value. a[3] means nothing.. and its value any thing. array bounded at a[2].

I got this.
warning: array subscript is above array bounds
output:

-1075036048

Any one can get any value ...So don worry about '2'...its a garbage value too

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.