Thanks for your quick reply, I finally got it working after a couple of hours. I followed your steps on installing TDM-gcc and it works. However I was curious why the mingw-w64 build I installed wasn't working. TDM-gcc's latest version is 5.1.0 I think and mingw-w64 has a newer version. So I installed TDM together with my already installed previous compiler (i686-w64-mingw32 5.4.0). After a couple of runs I find that TDM is running fine, but mingw64 isn't. I looked into the windows user variable PATH and saw TDM was sitting underneath mingw64. I thought that maybe something is conflicting with my previous compiler - so I went into the system variables and put mingw64's bin directory to the highest slot, then after a restart I can run the above code just fine.
Note that I have no idea what mingw64 is conflicting with, and how TDM did not get affected. I am thinking it was because they package their stuff differently. I have to manually put mingw64's bin directory into system variable because putting it into user variable did not work. TDM's installer put its bin directory into the user variable.
So I concluded that mingw64 conflicted with something in my system's variable, solved it by moving the directory to the highest place. Now code runs fine.
Thanks for explaining things to me, I really appreciate it.