So I made an uninitialized array in C++ and tried printing the last element to see what the output would be. Every element in an uninitialized array should have the value of 0 (right?), but the output I got was something else. This is what the main function looked like:
int main() {
int i[5];
cout << i[4] << '\n';
}
Running this outputs 1606416656
(same number every time) with a line break. However, changing '\n'
to endl
changes the output to 0
with a line break.
Why is that?
Also, trying to print i[3]
instead of i[4]
correctly outputs 0
even with '\n'
. Why?
I did some research and read somewhere that '\n'
doesn't "flush the buffer" while endl
does. What does this "flushing the stream" actually mean, and is this what's affecting the output?