C course menu
Installing a C Compiler on Windows, macOS and Linux (gcc, clang, MSVC)
Install a C compiler step by step: GCC (MinGW-w64) or Visual Studio on Windows, Xcode command line tools (clang) on macOS, and gcc on Linux. Check the installation, compile and run a C file, and set up VS Code.
On this page
To run C programs you need a compiler: a program that turns your .c source files into an executable. Three widely used compilers are free:
- GCC (
gcc): the standard on Linux, also available on Windows through MinGW-w64. - Clang (
clang): the default on macOS. - MSVC (
cl): Microsoft's compiler, included with Visual Studio on Windows.
Any of them works for this course. Pick the section for your operating system.
Windows#
Option 1: GCC with winget (quick, command line)#
Windows 10 and 11 include the winget package manager. Open PowerShell and run:
winget install BrechtSanders.WinLibs.POSIX.UCRTThis installs a recent GCC (the WinLibs MinGW-w64 build). Close and reopen PowerShell so the new commands are found, then check:
gcc --versionYou should see a version line such as gcc (MinGW-W64 ...) 14.2.0 or newer. Any recent version supports everything in this course.
Option 2: Visual Studio (full IDE)#
- Download Visual Studio Community (free) from Microsoft's website.
- In the installer, tick the Desktop development with C++ workload (it includes the C compiler).
- Create a new Console App project, rename the source file to end in
.c, and press Ctrl + F5 to build and run.
Visual Studio includes an excellent debugger, but uses MSVC instead of gcc, so command-line examples in this course will look slightly different.
macOS#
Install Apple's command line tools, which include clang:
xcode-select --installThen check the installation. On macOS, the gcc command is an alias for clang, so either name works:
clang --versionLinux#
Most distributions provide GCC in their package manager. On Ubuntu and Debian:
sudo apt update
sudo apt install build-essential
gcc --versionOn Fedora, use sudo dnf install gcc.
Compile and run your first file#
Create a file hello.c:
#include <stdio.h>
int main(void) {
printf("It works!\n");
return 0;
}It works!In a terminal, in the folder that contains the file:
gcc hello.c -o hello
./helloOn Windows PowerShell, run .\hello.exe instead of ./hello.
gcc hello.ccompiles the file.-o hellonames the output program (without it, the program is calleda.out, ora.exeon Windows).
Recommended flags#
Turn on warnings and pick a standard version:
gcc -std=c17 -Wall -Wextra -g hello.c -o helloTreat warnings as errors to fix. Many C bugs are visible as warnings long before they crash anything.
An editor#
Visual Studio Code with Microsoft's C/C++ extension gives colouring, auto-complete and debugging. Write code in VS Code and compile in its built-in terminal (Ctrl+backtick). Online compilers also work for quick experiments, but installing a compiler lets you work with files and real projects.
Practice#
Practice exercise
Install a compiler, then compile and run a program that prints your name and your city on two lines, with warnings enabled.
Show solution
#include <stdio.h>
int main(void) {
printf("Asha\n");
printf("Hyderabad\n");
return 0;
}Asha
Hyderabadgcc -std=c17 -Wall -Wextra me.c -o me
./meIf the terminal says gcc is not recognised, close and reopen it after installing so it picks up the updated PATH.
Key takeaway#
Finished this lesson?