Lesson 2 of 7 · C Foundations · 4 min read · Updated

Install a C compiler and run a program

Where to download it officially, how to install it on Windows, Mac or Linux, and how to compile and run.

A computer cannot run C code directly. You need a compiler, which turns your .c file into a program the computer can run. This course uses gcc, the free compiler used almost everywhere. Setting it up once takes about fifteen minutes.

You write hello.c → gcc compiles it into a program → you run the program and see the output.

Start today, in the browser

No computer of your own yet, or want to try before installing? OnlineGDB compiles and runs C in the browser, on a phone too. Everything in the first few topics works there.

Windows: the easy way — Code::Blocks

  • Go to the official Code::Blocks download page.
  • Under Windows, download the installer whose name ends in mingw-setup.exe. That one includes the gcc compiler; the others do not.
  • Run it and accept the defaults.
  • Open Code::Blocks → File → New → Empty File, type your program, save it as hello.c.
  • Press F9 (Build and run). A black window shows your output.

Windows: gcc on the command line — MSYS2

  • Download the installer from the official MSYS2 website and run it (keep the folder C:\msys64).
  • Open MSYS2 UCRT64 from the Start menu and run the command below to install gcc.
  • Add C:\msys64\ucrt64\bin to Windows Path: Start → "Edit the system environment variables" → Environment Variables → Path → New.
  • Open a new Command Prompt and type gcc --version. You should see a version number.
bash
pacman -S mingw-w64-ucrt-x86_64-gcc

Mac and Linux

  • Mac: open Terminal and run xcode-select --install. This installs Apple's free command line tools, which include a C compiler.
  • Ubuntu / Debian Linux: run sudo apt install build-essential.
  • Any system, for a nicer editor: install Visual Studio Code and its C/C++ extension, and use the compiler you installed above.

Compile and run: the everyday routine

bash
mkdir c-practice
cd c-practice

gcc hello.c -o hello      # compile: hello.c becomes the program 'hello'
hello                     # run it on Windows (Mac/Linux: ./hello)

gcc -Wall hello.c -o hello   # the same, with helpful warnings switched on

Keep all your practice files in one folder, open the terminal in that folder, and repeat two steps every time you change the code: compile, then run. Always compile with -Wall; the warnings it prints catch real mistakes.

Warning

'gcc' is not recognized means Windows cannot find the compiler: the bin folder is not on Path, or the Command Prompt was opened before you changed Path. Close it and open a new one. No such file or directory means the terminal is in a different folder from your .c file.

On Windows, which Code::Blocks installer already includes the gcc compiler?

Try this now

  1. Install a compiler using one of the ways above and check it with gcc --version (or build a program in Code::Blocks).
  2. Create a folder called c-practice, save the Hello program from the next lesson in it, compile it and run it.
  3. Compile once with -Wall and once without, after deleting #include <stdio.h>. Compare what each tells you.

Create a free account to save your progress, or sign in.