How to Run a C Program on macOS Tahoe — Compile and Execute in Terminal

This guide shows you how to compile and run a C program on macOS Tahoe using the Terminal. If you have already installed a compiler, you are ready to go. If not, start with our guide on how to install GCC on macOS Tahoe first — it takes one command.

Just need to run one or two quick programs without setting anything up? You can compile C in your browser instead — see our guide to online C compilers. No installation required.

The Two-Step Workflow

Running a C program on macOS is always two steps:

  1. Compile — turn your .c source file into an executable program
  2. Run — execute that program

Unlike Python or JavaScript, C is a compiled language — you cannot run a .c file directly. The compiler (gcc, which is Apple’s Clang on macOS) translates your code into machine code first.

Step 1 — Write Your C Program

Open any text editor and create a file called hello.c. You can use the built-in nano editor right in the Terminal:

nano hello.c

Type this program:

#include <stdio.h>

int main(void) {
    printf("Hello, World!\n");
    return 0;
}

Save and exit nano with Ctrl+O, Enter, then Ctrl+X.

Step 2 — Navigate to Your File

Use cd to move into the folder where you saved hello.c. For example, if it is on your Desktop:

cd ~/Desktop

Confirm the file is there with ls:

ls hello.c

Step 3 — Compile the Program

gcc hello.c -o hello

Breaking this command down:

Part Meaning
gcc The compiler (Apple Clang on macOS)
hello.c Your source file — the input
-o hello Name the output executable hello

If your code has no errors, the command finishes silently and creates a new executable file named hello. If you omit -o hello, the compiler names the output a.out by default.

Step 4 — Run the Program

./hello

Output:

Hello, World!

The ./ prefix is required — it tells the shell to run the program from the current directory. Typing just hello will not work, because the current folder is not in your PATH by default.

The Complete Sequence

cd ~/Desktop
gcc hello.c -o hello
./hello

Three commands, every time: navigate, compile, run.

Recommended Compiler Flags

For learning C, always compile with warnings enabled. They catch real bugs before they bite:

gcc -Wall -Wextra hello.c -o hello
Flag What it does
-Wall Enable all common warnings
-Wextra Enable extra warnings -Wall misses
-std=c17 Use the C17 standard (or -std=c11, -std=c99)
-g Add debug info for use with a debugger
-O2 Optimise the compiled program for speed

A good everyday command for students:

gcc -Wall -Wextra -std=c17 hello.c -o hello

Compiling Multiple Source Files

As your programs grow, you will split them across files. Compile them together by listing each .c file:

gcc -Wall main.c utils.c math_helpers.c -o myprogram
./myprogram

The compiler combines all three source files into a single executable. Header files (.h) are not listed — they are pulled in automatically by the #include directives in your code.

Reading Input While the Program Runs

If your program uses scanf() to read input, just type the values when it runs:

#include <stdio.h>

int main(void) {
    int n;
    printf("Enter a number: ");
    scanf("%d", &n);
    printf("You entered %d\n", n);
    return 0;
}
gcc -Wall input.c -o input
./input
Enter a number: 42
You entered 42

Common Errors and Fixes

Error message Cause and fix
gcc: command not found No compiler installed — see our install guide
hello.c: No such file or directory You are in the wrong folder — use cd to navigate to where the file is, check with ls
permission denied: ./hello Rare — fix with chmod +x hello then run again
undefined reference to 'function' You forgot to include a .c file in the compile command — list all of them
implicit declaration of function 'printf' You forgot #include <stdio.h> at the top

Tired of Typing Commands? Use an IDE

Running three commands for every change gets tedious. VS Code lets you compile and run with a single keypress, and adds code completion and a visual debugger. It is free and works great on Apple Silicon and Intel Macs.

If you prefer a faster, more modern Terminal experience, Warp Terminal adds autocomplete, command history search, and AI assistance — a nice upgrade for the compile-run-debug loop on macOS.

What’s Next

You now know how to compile and run C on macOS Tahoe. Time to practise — browse our full list of C programs with examples, from beginner exercises to sorting algorithms and data structures.


As an Amazon Associate we earn from qualifying purchases. The Warp Terminal link above is a referral link.

Recommended Book

Once you are comfortable compiling and running programs, the best book to actually learn the language is The C Programming Language by Brian Kernighan and Dennis Ritchie (K&R). Every example in it compiles and runs with the exact gcc workflow above.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>