Please, consider the following C++ snippet:
#include <iostream>
int main() {
int x;
std::cout << x << '\n';
return 0;
}
as expected the result printed will be unpredictable since the variable x
has not been initialized. If you run it may get 458785234 and 348934610 the second time. However, if you change the code the following way:
#include <iostream>
int main() {
int x;
std::cout << x << std::endl;
return 0;
}
Now, the x printed is always equal to zero. Why is that? Note the only change introduced is the std::endl
. Can anybody explain why this assigns 0 to x
variable?