[C Series 2] Prepare C On macOS and Run Your First Compile

한국어 버전

We already outlined why we are learning C and the overall roadmap. Now it is time to execute: make sure your Mac can compile and run C code. The first thing you should do after deciding to learn C is to experience the full flow—how source code becomes an executable on your own machine.

New Terms In This Post

  1. Xcode Command Line Tools: the toolset that lets you use commands like clang, git, and make on macOS
  2. Compiler: the program that converts human-readable source into machine-readable instructions
  3. Object file: the intermediate .o file produced by compilation; not directly runnable yet
  4. Linking: the process that bundles object files and libraries into the final executable

Core Ideas

Study Notes

  • Time required: 30–40 minutes
  • Prerequisites: basic Terminal usage on macOS
  • Goal: compile and run a C file with clang

You can usually start C development on a Mac without installing the full Xcode app. Xcode Command Line Tools already ships with clang, and that is what we use for this post.

On macOS the version string often looks like Apple clang version .... Do not sweat the exact numbers; just verify that the clang command runs on your Mac.

Let’s clarify the key file types:

C requires an explicit compile -> link -> run cycle. The code does not run immediately after you save the file.

Follow Along in Code

Check for Developer Tools

First confirm whether the developer tools are present.

clang --version

If you see the version, you are ready. If you get a message like xcode-select: note: no developer tools were found, install the tools:

xcode-select --install

After installation, verify that the developer path is set:

xcode-select -p

You should see something like /Applications/Xcode.app/Contents/Developer or /Library/Developer/CommandLineTools.

Create Your First C File

Create a working folder and add hello.c:

#include <stdio.h>

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

This prints Hello, C! and exits cleanly. Focus on learning the save → compile → run flow before worrying about every syntax detail.

Compile and Run

Run these commands in the Terminal:

clang hello.c -o hello
./hello

The first command reads hello.c and outputs an executable named hello. Compilation and linking both happen in that single line. The second command runs the executable in the current folder.

A successful run prints:

Hello, C!

Remember you executed hello, not hello.c. \n represents a newline, so the cursor moves to the next line after printing.

Nail the Basic Flow First

At the outset, being confident with these two lines is enough:

clang hello.c -o hello
./hello

That is the “core execution flow.” The rest of this section is optional detail for when you want to peek under the hood.

Inspect Each Compilation Stage

As hinted in Part 1, C code runs after multiple stages. Learn their names now and verify how each step transforms the files.

1) Preprocessing Only

clang -E hello.c

-E stops after preprocessing. You will see #include <stdio.h> expand into a huge block of declarations.

2) Produce an Object File

clang -c hello.c -o hello.o

-c stops after compilation. You now have hello.o, which is close to machine code but not yet linked, so you cannot run it.

3) Create the Executable

clang hello.o -o hello

This step links everything, including printf from the standard library. For small programs clang hello.c -o hello is fine, but once you have multiple source files this separation becomes essential.

The First Errors You Will See

Errors are normal. Expect these three early on:

  • clang: error: no such file or directory: 'hello.c': you are in the wrong folder or the filename does not match.
  • zsh: command not found: clang (the exact wording can vary by shell): install Xcode Command Line Tools.
  • permission denied: the file is not executable or the path is incorrect.

In the early stages, focus on distinguishing “I am editing the source file” from “I am running the executable.” Linker errors become more common once you use multiple files or external functions, so for now just learn the name.

Practical Variation: Print a Year

Try this variant:

#include <stdio.h>

int main(void) {
    int year = 2026;
    printf("C study starts in %d.\n", year);
    return 0;
}

Compile and run again to repeat the edit → compile → run loop. In C you almost always need to recompile after changing the source.

Why This Matters

If you learned Python or JavaScript first, you may be used to saving a file and running it immediately. C separates compilation and linking, so you see how code becomes a program in detail.

Master these four checkpoints for this session:

  • Use clang --version to confirm your toolchain.
  • Create hello.c and compile with clang hello.c -o hello.
  • Run the executable with ./hello.
  • Use clang -c to see that object files exist between source and executable.

Having this tactile experience makes later topics—headers, multiple source files, build automation—far less intimidating.

Practice in CodeSandbox

The sandbox below uses CodeSandbox's Universal starter. For C, the key learning loop is still compile and run in the terminal, so recreate the lesson code as a source file and repeat that cycle directly.

Live Practice

C Practice Sandbox

CodeSandbox

Run the starter project in CodeSandbox, compare it with the lesson code, and keep experimenting.

Universal starterCterminal
  1. Fork the starter and create a practice file such as hello.c
  2. Paste in the lesson code and compile it if clang or gcc is available
  3. Edit the code, rebuild it, and compare the new output

For C, the terminal build loop matters more than a browser preview. Compiler availability can vary by environment, so first confirm that clang or gcc is present in the Universal starter.

Practice

  • Follow: create hello.c and run clang hello.c -o hello, ./hello in order.
  • Extend: change the print message and add a number variable, then confirm the output only updates after recompilation.
  • Debug: intentionally type helloo.c and interpret the error message.
  • Completion check: explain the difference between source, object, and executable files and retype the compile commands yourself.

Wrap-Up

The goal of this post is not to cram syntax but to experience “how a C program runs on my Mac.” Next we will look at variables and primitive types to see how C stores different values in memory.

💬 댓글

이 글에 대한 의견을 남겨주세요