Interview Question
Country: United States
i've executed both the programs on gcc .
second prog is giving o/p 0 1 2 3
but first one is neither showing any o/p neither any an error . Cursor remain static .
i guess it depends on the compiler too. I got the output in microsofts c++ compiler in visual studio 2010 along with the error.
however in gcc in unix i seem to having the exact same output as yours.
But what could be the possible reason ??
Changing i to 1 changes the pattern of whole output .
out of index.
stack will overflow, so last a[3] will overwrite to buffer of variable 'i' on some O/S.
Anyway, the result is;
(1)
1
1
1
1
(2)
0
1
2
3
your array is of size 3 but you are storing 4 items in it. so the compiler seems to throwing an error - stack around the variable is corrupted.
- Anonymous July 05, 2012as such the first code prints
1
1
1
1
and the second one prints
0
1
2
3