Setting Up a C Compiler

To run C you need a compiler: a program that turns your source code into an executable file. This lesson shows how to install GCC or Clang on Windows, macOS and Linux, compile your first program from the terminal, and choose an editor.

The main compilers

CompilerCommandWhere
GCC (GNU Compiler Collection)gccThe standard on Linux; available everywhere
Clang (LLVM)clangThe default on macOS; also on Linux and Windows
MSVC (Microsoft Visual C++)clWindows, through Visual Studio

GCC and Clang accept the same options, so the commands in this course work with either. On most systems the command cc runs whichever one is installed.

Installing

Linux

$
# Debian, Ubuntu, Linux Mint
sudo apt update
sudo apt install build-essential

# Fedora
sudo dnf install gcc make

macOS

Install Apple's Command Line Tools, which include Clang:

$
xcode-select --install

Windows

The simplest options:

  • WSL (Windows Subsystem for Linux) — run wsl --install in PowerShell, open Ubuntu, and follow the Linux steps. This gives you exactly the environment this course uses.
  • MSYS2 — installs GCC (the "UCRT64" toolchain) and a Unix-style terminal for native Windows programs.
  • Visual Studio (Community edition) with the "Desktop development with C++" workload — includes MSVC, which compiles C too.

Checking the installation

$
gcc --version
clang --version

Either one printing a version number means you're ready. Any GCC from version 8 or Clang from version 7 handles the C17 code in this course; the C23 lesson needs GCC 14 or Clang 18 or newer.

Compiling and running

Save this as hello.c:

C hello.c
#include <stdio.h>

int main(void) {
    printf("Hello from my own computer!\n");
    return 0;
}

In a terminal, in the same folder, compile and run it:

$
gcc hello.c -o hello
./hello
Output
Hello from my own computer!

-o hello names the output file; without it you get a.out (or a.exe on Windows). On Windows outside WSL, run it with hello.exe or .\hello.

The command this course uses

Always turn on warnings — they catch many bugs before your program ever runs:

$
gcc -std=c17 -Wall -Wextra -pedantic hello.c -o hello
OptionMeaning
-std=c17Use the C17 standard
-Wall -WextraEnable most useful warnings
-pedanticWarn about anything that isn't standard C
-o nameName of the executable
-gInclude debugging information (lesson 56)
-O2Optimise for speed
-lmLink the maths library (needed on Linux for math.h, lesson 25)

Every example in this course compiles with these options without a single warning.

Editors and IDEs

  • Visual Studio Code with Microsoft's C/C++ extension (or the clangd extension) — a popular, lightweight choice.
  • CLion, Visual Studio, Code::Blocks — full IDEs with a built-in debugger.
  • Any text editor plus a terminal works perfectly well.

An IDE's green "Run" button just runs the same compile command for you. Learning the terminal command first makes errors much easier to understand.

No install? Use an online compiler

Online C compilers such as Compiler Explorer (godbolt.org) let you paste code and run it in a browser. They're handy for quick tests, though some don't support reading keyboard input.

Try it yourself

Install a compiler, then save this program as main.c and compile it with warnings on. Change the numbers and compile again.

Try it yourself · main.c
Edit the code, copy it into main.c, then compile and run it with cc -std=c17 -Wall main.c -o main && ./main. How to set up a C compiler
Output
7 apples shared by 3 friends: 2 each, 1 left over