0

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.

5
Contributors
4
Replies
6
Views
7 Years
Discussion Span
Last Post by arindam31
0

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.

0

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.

Edited by Aia: Some more explanation

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.