I am new to programming and was doing a practice with expressions on Visual Studio 2015. I first built this program:
#include <iostream>
int main(void)
{
int x;
std::cout
<< "The expression x = 100 has the value " << (x = 100) << ".\n"
<< "Now x has the value " << x << ".\n"
<< "The expression x < 3 has the value " << (x < 3) << ".\n"
<< "The expression x > 3 has the value " << (x > 3) << ".\n";
system("pause");
return 0;
}
And the compiler gave an error warning: uninitialized local variable 'x' used in line 11. It's the line << "The expression x > 3 has the value " << (x > 3) << ".\n";
. I changed the program to:
...
std::cout
<< "The expression x = 100 has the value " << (x = 100) << ".\n";
std::cout
<< "Now x has the value " << x << ".\n"
<< "The expression x < 3 has the value " << (x < 3) << ".\n"
<< "The expression x > 3 has the value " << (x > 3) << ".\n";
...
And the warning disappeared. My question is, in the first program, why is x still uninitialized after I have assigned 100 to it? Why is the error in line 11 but not in line 9 or 10? Is there a way to fix the problem without splitting the cout statement?