This is not related to codeblocks. Codeblocks is NOT a compiler, but an Integrated Development Environment. It can use many other compilers than gcc/g++.
Nevertheless, your result is normal because you have used the debug mode apparently. This mode in gcc makes some initialisations for you.
If you try the release mode, the result is not the same, because here ans is not initialised : indicated by a warning :
main.cpp|26|warning: 'ans' may be used uninitialized [-Wmaybe-uninitialized]|