Before you can write and compile C programs, you need to set up your development environment. This guide will take you from an out-of-the-box MacBook Pro to compiling and running your very first C program.
macOS doesn’t come with a C compiler pre-installed, but Apple provides a free package called Command Line Tools for Xcode. This package includes clang (the default C compiler for macOS), make, and other essential development utilities.
You do not need to install the massive, multi-gigabyte Xcode IDE from the App Store. We can install just the lightweight command-line tools via the Terminal.
Cmd + Space to open Spotlight, type “Terminal”, and press Enter).Enter:xcode-select --install
To confirm that your compiler and make utility are ready to go, run these two commands in your Terminal:
clang --version
(You should see output indicating the Apple clang version.)
make --version
(You should see output indicating GNU Make.)
To write C code, you need a plain text editor. Do not use generic word processors like TextEdit or Microsoft Word, as they inject hidden formatting characters that break code compilation. Popular, lightweight choices for modern development on macOS include:
Pick your preferred editor, install it, and you’re ready to write some code.
Now let’s test the environment by creating a classic “Hello, World!” program.
#include <stdio.h>
int main(void)
{
printf("Hello, World!\n");
return 0;
}
C is a compiled language. This means your human-readable .c file must be translated into a machine-readable binary executable before it can run.
To compile your file directly, use the compiler command in the Terminal:
clang hello.c -o hello
Because macOS includes make, you can take advantage of its built-in implicit rules for C. You don’t even need a Makefile for a single-file project. Simply type:
make hello
make automatically looks for a file named hello.c in the current directory and invokes the compiler to build an executable named hello.
Once compiled by either method, you will see a new executable file named hello in your folder. Run it by typing:
./hello
(The ./ tells the Terminal to look for the executable in the current directory.)
Hello, World!
If you see that output on your screen, your MacBook Pro is officially configured for C development! You are ready to move on to building more complex applications.