Skip to content
Laviyx Learn

Try

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.

Beginner4 min readUpdated
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:

PowerShell
winget install BrechtSanders.WinLibs.POSIX.UCRT

This installs a recent GCC (the WinLibs MinGW-w64 build). Close and reopen PowerShell so the new commands are found, then check:

PowerShell
gcc --version

You 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)#

  1. Download Visual Studio Community (free) from Microsoft's website.
  2. In the installer, tick the Desktop development with C++ workload (it includes the C compiler).
  3. 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:

Terminal
xcode-select --install

Then check the installation. On macOS, the gcc command is an alias for clang, so either name works:

Terminal
clang --version

Linux#

Most distributions provide GCC in their package manager. On Ubuntu and Debian:

Terminal
sudo apt update
sudo apt install build-essential
gcc --version

On Fedora, use sudo dnf install gcc.

Compile and run your first file#

Create a file hello.c:

hello.c (C)
#include <stdio.h>

int main(void) {
    printf("It works!\n");
    return 0;
}
Output
It works!

In a terminal, in the folder that contains the file:

Terminal
gcc hello.c -o hello
./hello

On Windows PowerShell, run .\hello.exe instead of ./hello.

  • gcc hello.c compiles the file.
  • -o hello names the output program (without it, the program is called a.out, or a.exe on Windows).

Turn on warnings and pick a standard version:

Terminal
gcc -std=c17 -Wall -Wextra -g hello.c -o hello
FlagMeaning
-std=c17Use the C17 standard
-Wall -WextraEnable most warnings: the compiler points out likely bugs
-gInclude debugging information for a debugger
-O2Optimise for speed (for finished programs)

Treat 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
me.c (C)
#include <stdio.h>

int main(void) {
    printf("Asha\n");
    printf("Hyderabad\n");
    return 0;
}
Output
Asha
Hyderabad
Terminal
gcc -std=c17 -Wall -Wextra me.c -o me
./me

If 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?