Xcode is mostly free, but it’s large – it occupies multiple gigabytes on your hard disk. If you just want to build code and just want compilers and SDKs, it’s a lot of overhead.
Several years ago, Kenneth Reitz put together a cut-down version that was Xcode minus Xcode – basically GCC and all the headers and libraries that weren’t Apple-licensed. He called this OSX-GCC-Installer, hosted it on GitHub, and it became fairly popular. It became popular enough that people from Apple became interested in it, and eventually Apple released Command Line Tools for Xcode. It’s free, although it does require an Apple ID to download it.