Q:

Why does std:endl assign zero to an unitialized variable?

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?

A:

as expected the result printed will be unpredictable ...
Now, the x printed is always equal to zero. Why is that?

It is so because the behaviour is undefined.
You expected the number to be "unpredictable". It seems that you didn't predict the number to be zero. This should be according to your expectations.
You did nothing to make the number non-zero, so why would you expect the number to be non-zero?
On the other hand, you may have been expecting that the behaviour doesn't change because the change to the program seems to be unrelated. That expectation is ill-advised. Undefined behaviour is not guaranteed to be the same undefined behaviour if you change any part of the program. In fact, the behaviour is not guaranteed to be the same even if you don't change anything. On the other hand, the behaviour is also not guaranteed to be different. Nothing about the behaviour of the program is guaranteed. That is what undefined behaviour means.

