Hello World

The compilation process

How does a program written in C turn into an executable binary? There are three phases: compilation, assembling, and linking. The compilation phase turns source code into lower-level assembly code. Before higher-level languages and compilers, all programming was done in assembly, which took much longer. The assembling phase translates the assembly into a binary format called an object file. The object file contatins lots of information about where different variables and functions are expected to be, along with as close to what the processor can directly understand as is possible until references to other object files can be resolved. Almost always nowadays, these first two phases are done together. The final phase, linking, combines multiple object files into one special executable object file with all references resolved, ready to be loaded by the operating system and run by the processor. Later, when we get into dynamically linked libraries, we will see that this third step cannot always be done ahead of time, but for now we won't worry about that.

The code

#include <stdio.h>

int main() {
  puts("Hello, world!");
  return 0;

We won't get into what each part of this means yet. This step is just to make sure your compiler and everything else is installed right. We will be using GCC on a POSIX OS (Linux, macOS, *BSD, etc) in this tutorial, but these commands can be ported to other compilers such as Clang and platforms such as Windows. The source code will follow the C standard, and will remain the same assuming your platform also follows the C standard.

To compile, assemble, and link this program with GCC, all we have to do is run gcc hello.c -o hello, where hello.c is our source file name, and hello is our executable file name. Now run ./hello. Did it print Hello, world! when you ran it? If so, congrats! You just compiled and ran your first C program!