Skip to content
KALANBLON
Show contents

Chapter 1 of 4 · 15 min

Installing a compiler and compiling

By the end of this chapter, you'll be able to

  • Install a C compiler on your computer
  • Check the installation with gcc --version
  • Compile a .c file into an executable program
  • Run the program, and recompile after every change

Compiling means translating once and for all

In Python, a program called an interpreter reads your code and runs it line by line, every time you launch it. In C, there are two stages:

  1. the compiler reads your source file (hello.c) and translates it into an executable, a file full of machine instructions;
  2. you then run that executable, as many times as you like, without the compiler.

It's like translating a book: the translation takes time, but once it's printed, anyone can read it without a translator. That's one of the reasons why C programs are so fast.

The processor carries out the instructions: the text appears in the terminal.

Step 5 of 5
Text description of the animation
  1. You write your program in a text file whose name ends in .c.
  2. You ask gcc to compile it. It first checks that everything follows the rules of C.
  3. If it finds an error, it stops and reports it with the line number. No executable is created.
  4. If all goes well, it produces the executable. You run it with ./hello.
  5. The processor carries out the instructions: the text appears in the terminal.

Installing the compiler

We use GCC, a free and open-source compiler. The examples in this course are checked with GCC 14, following the C17 standard. Pick your situation:

  • Linux (Ubuntu, Debian): open a terminal and type sudo apt install build-essential.
  • Linux (Fedora): sudo dnf install gcc.
  • macOS: type xcode-select --install in the Terminal and accept the installation. You get the Clang compiler, which also answers to the name gcc: all the examples in the course work with it.
  • Windows: install MSYS2 from its official website (msys2.org), open the "MSYS2 UCRT64" terminal, then type pacman -S mingw-w64-ucrt-x86_64-gcc.

Then check that everything works:

gcc --version

If a line with a version number appears, you're all set. If you read "command not found", the compiler isn't installed, or your terminal was opened before the installation: close it and open it again.

Your first compiled program

Create a folder, then a file hello.c with a text editor (for example VS Code, or even Notepad):

#include <stdio.h>

int main(void) {
    printf("Hello, Kalanblon!\n");
    return 0;
}

In the terminal, go into that folder (cd path/to/folder), then compile and run:

gcc -std=c17 -Wall -Wextra -o hello hello.c
./hello
Hello, Kalanblon!

What the command means:

  • -std=c17: follow the C17 standard;
  • -Wall -Wextra: turn on the warnings, the messages where the compiler points out suspicious code. Despite its name, -Wall doesn't turn them all on, which is why -Wextra is added. Always keep them on, they'll save you a lot of mistakes;
  • -o hello: name the executable hello (on Windows, it's called hello.exe);
  • ./hello: run the program located in the current folder (./).

We'll see line by line what this program contains in the next chapter.

Your turn

Worked exercise

Worked exercise: your business card

Write a program card.c that displays three lines: your first name, your city and the language you're learning. Compile it with warnings turned on, then run it.

Hint 1
Start from the program hello.c: you just need three printfs.
Hint 2
Don't forget the \n at the end of each text, otherwise everything is displayed on a single line.
Show the solution and common mistakes
#include <stdio.h>

int main(void) {
    printf("First name: Amina\n");
    printf("City: Dakar\n");
    printf("Learning: C\n");
    return 0;
}
First name: Amina
City: Dakar
Learning: C

Commands: gcc -std=c17 -Wall -Wextra -o card card.c then ./card.

Common mistakes:

  • Typing ./card.c: you run the executable, not the source file.
  • Forgetting -o card: GCC then creates an executable with the default name, a.out (or a.exe on Windows).
  • Forgetting a parenthesis or a quote: read the line pointed out by the first error.

Summary

  1. 1C is compiled: the compiler translates the .c file into an executable, once and for all.
  2. 2You compile with gcc -std=c17 -Wall -Wextra -o name name.c, then run with ./name.
  3. 3After every change to the code, you must recompile.
  4. 4When there's an error, read the first one: file, line, column and message.

Your progress is saved on this device. Create a free account to keep your progress on all your devices.