I barely start my c++ learning. I have met a strange case that I cannot understand.
It's about declaring and printing a double
variable.
My first piece of code looks like below:
#include <iostream>
int main ()
{
double wage;
std::cout << wage << std::endl;
return 0;
}
After I compile this file with g++
and run it, there is a 0
printed in my terminal, which is fine. Then I keep adding lines of code and now it looks like this:
#include <iostream>
int main ()
{
double wage;
std::cout << wage << std::endl;
double salary = wage = 9999.99;
std::cout << wage << std::endl;
std::cout << salary << std::endl;
std::cout << wage
<< salary << std::endl;
return 0;
}
However, when I try to compile and run this program again, a very strange decimal appears.
$ cpp-directory ./a.out
2.122e-314
9999.99
9999.99
9999.999999.99
As you can see from the result, there is a 2.122e-314
. From my perspective, it should print 0 on the first line rather than this number... I am not familiar with c++ stuff but is it something like a memory leak or so...
Can someone give me a tip or an explanation on this? Thanks.
Note:
$ ~ g++ --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 8.1.0 (clang-802.0.42)
Target: x86_64-apple-darwin16.5.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin