I have some difficulty understanding what is happening with this simple example compiled by two different compilers:
#include <iostream>
using namespace std;
int main()
{
double x;
cout << x << endl;
x += 1.0;
cout << x << endl;
return 0;
}
Compiling with g++ it gives
0
1
while compiling with clang++, it gives
6.95279e-310
1
I am sure it is my lack of understanding, but any tips understanding this behaviour would be much appreciated. Is the compiled code produced from clang++ setting x = 0
when it is reassigned, but not when shown with cout
? Is this expected behaviour?
Question: Should one always initialise variables because of these differences? What is common practice in C++?