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
- Xcode Command Line Tools: the toolset that lets you use commands like
clang,git, andmakeon macOS - Compiler: the program that converts human-readable source into machine-readable instructions
- Object file: the intermediate
.ofile produced by compilation; not directly runnable yet - 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:
- Source file: the
.cfile you read and edit - Object file: the
.ofile generated mid-compilation - Executable: the final program you run such as
./hello
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 --versionto confirm your toolchain. - Create
hello.cand compile withclang hello.c -o hello. - Run the executable with
./hello. - Use
clang -cto 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.
💬 댓글
이 글에 대한 의견을 남겨주세요