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:
- the compiler reads your source file (
hello.c) and translates it into an executable, a file full of machine instructions; - 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.
Text description of the animation
- You write your program in a text file whose name ends in .c.
- You ask gcc to compile it. It first checks that everything follows the rules of C.
- If it finds an error, it stops and reports it with the line number. No executable is created.
- If all goes well, it produces the executable. You run it with ./hello.
- 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 --installin the Terminal and accept the installation. You get the Clang compiler, which also answers to the namegcc: 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,-Walldoesn't turn them all on, which is why-Wextrais added. Always keep them on, they'll save you a lot of mistakes;-o hello: name the executablehello(on Windows, it's calledhello.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
hello.c: you just need three printfs.Hint 2
\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(ora.exeon Windows). - Forgetting a parenthesis or a quote: read the line pointed out by the first error.
Summary
- 1C is compiled: the compiler translates the .c file into an executable, once and for all.
- 2You compile with gcc -std=c17 -Wall -Wextra -o name name.c, then run with ./name.
- 3After every change to the code, you must recompile.
- 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.