C: Setup: Install GCC & VS Code, Run Your First Program
Setting up a development environment is like outfitting a kitchen — once the stove (compiler) and utensils (editor) are in place, you can finally start cooking (writing code).
⚡ Quick start (3 steps): ① Install a compiler (WinLibs or MSYS2 on Windows, built-in
ccon macOS,sudo apt install gccon Linux) → ② createhello.c→ ③ compile withgcc -o hello hello.cand run./hello(hello.exeon Windows). Section 1 explains each step.
TL;DR: A C toolchain needs three things: a compiler (MinGW-w64 or MSYS2 on Windows, built-in
ccon macOS, GCC on Linux), an editor (VS Code + the C/C++ extension), and UTF-8 source files. Compile withgcc -Wall -Wextra -std=c99 -o hello hello.c, then run./hello(hello.exeon Windows). Ifgccis not recognized, fix PATH; if output is garbled, check the file encoding and runchcp 65001.
1. Installing the GCC Compiler
Setting up a C environment takes three things: a compiler (MinGW-w64 or MSYS2 on Windows, the built-in Clang on macOS, GCC on Linux), an editor (VS Code with the C/C++ extension works well), and a folder that stores source files as UTF-8. Once installed, compile with gcc -Wall -Wextra -std=c99 -o hello hello.c and run the generated program.
(1) Windows Installation
On Windows, we recommend installing MinGW-w64 or MSYS2, both of which provide a Windows port of GCC.
Option 1: MinGW-w64 (recommended for beginners)
- Visit https://www.mingw-w64.org and download the installer
- During installation, select the
x86_64architecture andwin32thread model - Add the
binfolder of your installation directory to the systemPATHenvironment variable
Option 2: MSYS2 (more flexible)
pacman -S mingw-w64-x86_64-gcc
After installation, open a command prompt and verify:
gcc --version
If you see a version number, the installation was successful.
(2) macOS Installation
macOS ships with Clang (the cc command), which already compiles standard C — no installation needed:
cc --version
If you want GCC, install it with Homebrew:
brew install gcc
⚠️ Note: Homebrew installs a versioned binary (for example gcc-14), not a command named gcc. Check what you got:
ls /opt/homebrew/bin/gcc-* 2>/dev/null || ls /usr/local/bin/gcc-*
Then either:
- replace every
gccbelow with the real name (gcc-14or whichever version you installed), or - add an alias:
echo 'alias gcc=gcc-14' >> ~/.zshrc && source ~/.zshrc.
cc is enough. Just substitute cc for gcc in the commands below.
(3) Linux Installation
Most Linux distributions let you install GCC directly through the package manager:
sudo apt install gcc
Verify the installation:
gcc --version
gcc throughout — on macOS, substitute cc or your installed gcc-14.
2. Configuring VS Code
VS Code is a free code editor from Microsoft — lightweight and rich with extensions, making it an excellent choice for writing C code.
(1) Installing VS Code
Go to https://code.visualstudio.com to download and install.
(2) Installing Essential Extensions
Open VS Code, press Ctrl+Shift+X to open the Extensions marketplace, then search for and install:
| Extension | Purpose |
|---|---|
| C/C++ (Microsoft) | Syntax highlighting, IntelliSense, debugging support |
| Code Runner (Jun Han) | Run code with one click — press Ctrl+Alt+N to execute |
(3) Configuring the C/C++ Extension
Press Ctrl+Shift+P, type C/C++: Edit Configurations (UI), and make sure the following settings are correct:
- Compiler path: Enter the GCC installation path, e.g.
C:/mingw64/bin/gcc.exe - IntelliSense mode: Select
gcc-x64 - C standard: Select
c99
(4) One-Click Run Configuration
After installing Code Runner, press Ctrl+Alt+N to run the current C file. If you prefer running manually in the terminal, you can also use the command line directly.
c-learn) and open the entire folder rather than a single file. This ensures extensions work properly.
(5) Fixing Garbled Non-ASCII Output (Windows)
On Windows the console often uses a legacy code page (GBK/ANSI), so non-ASCII text printed by printf can come out garbled. Check three things:
- Source encoding: in VS Code, confirm the file encoding (bottom-right) is
UTF-8, not GBK/ANSI. - Terminal code page: run
chcp 65001before executing the program to switch the console to UTF-8. - In-program setting (Windows only):
// Preprocessor directive
#include <stdio.h>
// Preprocessor directive
#ifdef _WIN32
// Preprocessor directive
#include <windows.h>
// Preprocessor directive
#endif
// Function definition
int main(void) {
// Preprocessor directive
#ifdef _WIN32
SetConsoleOutputCP(65001); /* switch console output to UTF-8 */
// Preprocessor directive
#endif
// I/O operation
printf("Hello, C!\n");
// Return result
return 0;
}
3. Your First Program: Hello World
▶ Example
▶ Try it YourselfHello, World!
Line-by-line breakdown:
#include <stdio.h>— Tells the compiler to include the standard I/O header file, whereprintfis declaredint main(void)— Defines the main function, the program's entry point.intmeans it returns an integer,voidmeans it takes no parametersprintf("Hello, World!\n")— Calls the formatted output function;\nis the newline characterreturn 0— Returns 0 to the operating system, indicating the program ended normally
4. The Compilation and Execution Pipeline
C is a compiled language — code can't run directly; it must first be compiled into machine code. The full pipeline looks like this:
Source code (.c) → Preprocessing → Compilation → Assembly → Linking → Executable
(1) Step-by-Step Instructions
Step 1: Write the source file
Create a new file called hello.c in VS Code, type in the Hello World code above, and save it.
Step 2: Compile
gcc -o hello hello.c
gcc— Invokes the compiler-o hello— Specifies the output filename ashello(on Windows,.exeis appended automatically)hello.c— The source file to compile
Step 3: Run
./hello
On Windows:
hello.exe
(2) Step-by-Step Compilation
If you want to understand each stage of compilation, you can run them individually:
gcc -E hello.c -o hello.i
gcc -S hello.i -o hello.s
gcc -c hello.s -o hello.o
gcc hello.o -o hello
| Stage | Flag | Output | Description |
|---|---|---|---|
| Preprocessing | -E |
.i file |
Expands macros, processes #include, removes comments |
| Compilation | -S |
.s file |
Translates preprocessed code into assembly |
| Assembly | -c |
.o file |
Translates assembly into machine code (object file) |
| Linking | No special flag | Executable | Merges object files and libraries into the final program |
gcc -o hello hello.c is all you need. Step-by-step compilation is mainly useful for understanding the process or troubleshooting issues.
(3) Common Compiler Flags
| Flag | Purpose | Example |
|---|---|---|
-o |
Specify output filename | gcc -o prog main.c |
-Wall |
Enable all common warnings | gcc -Wall -o prog main.c |
-Wextra |
Enable extra warnings | gcc -Wextra -o prog main.c |
-std=c99 |
Specify the C standard | gcc -std=c99 -o prog main.c |
-g |
Generate debug information | gcc -g -o prog main.c |
-O2 |
Optimization level 2 | gcc -O2 -o prog main.c |
gcc -Wall -Wextra -std=c99 -o prog main.c. Warnings are your friends — they help you catch many potential bugs.
▶ Example: Turn on Warnings and Catch an Uninitialized Variable
▶ Try it Yourselfwarning: 'total' is used uninitialized [-Wuninitialized]
Fix it by changing int total; to int total = 0; and recompiling; the warning disappears.
▶ Example: Inspect the Four Compilation Artifacts
gcc -E hello.c -o hello.i # preprocess
gcc -S hello.i -o hello.s # compile to assembly
gcc -c hello.s -o hello.o # assemble to object code
gcc hello.o -o hello # link into an executable
hello.i hello.s hello.o hello
For daily work, gcc -o hello hello.c does all four steps at once; use the separate steps to understand the pipeline or debug link errors.
5. Common Compilation Errors
As a beginner, you will encounter compilation errors. Don't panic — learning to read error messages is more important than avoiding errors.
(1) Missing Semicolon
// Preprocessor directive
#include <stdio.h>
// Function definition
int main(void) {
// I/O operation
printf("Hello, World!\n")
// Return result
return 0;
}
hello.c:4:33: error: expected ';' before 'return'
The error message tells you: filename:line:column: error description. Here, a semicolon is missing at the end of line 4.
(2) Undeclared Identifier
// Preprocessor directive
#include <stdio.h>
// Function definition
int main(void) {
print("Hello, World!\n");
// Return result
return 0;
}
hello.c:4:5: warning: implicit declaration of function 'print'
You typed print instead of printf — the compiler doesn't recognize this function. Just check the spelling.
(3) Missing Header File
// Function definition
int main(void) {
// I/O operation
printf("Hello, World!\n");
// Return result
return 0;
}
hello.c:2:5: warning: implicit declaration of function 'printf'
You forgot #include <stdio.h>, so the compiler doesn't know the declaration of printf.
(4) Mismatched Braces
// Preprocessor directive
#include <stdio.h>
// Function definition
int main(void) {
// I/O operation
printf("Hello, World!\n");
// Return result
return 0;
hello.c:5:1: error: expected declaration or statement at end of input
This code is missing the final }. The compiler does not say "you forgot a brace"; it reports that something is missing at the end of the file. When you see expected declaration or statement at end of input, look upward for the nearest unclosed {.
6. Project File Organization
As your programs grow more complex, it's a good idea to organize your files like this:
c-learn/
├── ch02/
│ └── hello.c
├── ch03/
│ └── syntax.c
└── ch04/
└── variables.c
One folder per chapter, lowercase English filenames, and names that clearly describe the content.
❓ FAQ
bin folder is not on PATH. Add C:\mingw64\bin (your actual path) to the system Path, reopen the terminal, and verify with where gcc and gcc --version.bin to PATH. Choose MSYS2 if you want a package manager and Unix tools; use WSL if you already work in Linux. Install only one.Ctrl+Shift+P → C/C++: Edit Configurations (UI), point the compiler to ...\bin\gcc.exe, select gcc-x64, and reopen the folder.chcp 65001 on Windows), and Windows programs may need SetConsoleOutputCP(65001)../hello after gcc hello.c?gcc only compiles; it does not run the program. -o hello names the output, so you run ./hello (hello.exe on Windows). Without -o, Linux and macOS produce a.out.-Wall -Wextra catches uninitialized variables, implicit declarations, and format mismatches that later become real crashes.gcc --version reports Apple Clang — is that a problem?gcc maps to Clang, which compiles standard C correctly. If you want GNU GCC, brew install gcc installs a versioned command such as gcc-14, not gcc.📖 Summary
- GCC is the most widely used C compiler — use MinGW-w64/MSYS2 on Windows, Homebrew on macOS, and apt on Linux
- VS Code with the C/C++ extension and Code Runner extension provides a lightweight, efficient C development setup
- A C program goes through four stages from source code to executable: preprocessing → compilation → assembly → linking
- Always compile with
gcc -Wall -Wextra -std=c99and make a habit of reading warnings - Compilation error messages include the filename, line number, and description — learning to read them is a core skill
📝 Exercises
- Install GCC on your computer and verify with
gcc --version; record the version number - Write a program that outputs your name and a greeting (e.g. "Hi, I'm Alex, and I'm learning C!"), then compile and run it
- Intentionally create 3 compilation errors (missing semicolon, misspelled function name, missing header file), record the compiler's error messages, and practice reading error output