Setting Up Your Development Environment
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).
Installing the GCC Compiler
GCC (GNU Compiler Collection) is the most popular C compiler — free and cross-platform. You need to install it before you can compile C code into an executable program.
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.
macOS Installation
macOS doesn't come with GCC, but you can install it via Homebrew:
brew install gcc
macOS's built-in cc command actually invokes the Clang compiler, which can also compile C code. To confirm:
cc --version
Linux Installation
Most Linux distributions let you install GCC directly through the package manager:
sudo apt install gcc
Verify the installation:
gcc --version
gcc command throughout.
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.
Installing VS Code
Go to https://code.visualstudio.com to download and install.
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 |
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
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.
Your First Program: Hello World
Example
#include <stdio.h>
int main(void) {
printf("Hello, World!\n");
return 0;
}
Hello, 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
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
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
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.
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.
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.
Missing Semicolon
#include <stdio.h>
int main(void) {
printf("Hello, World!\n")
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.
Undeclared Identifier
#include <stdio.h>
int main(void) {
print("Hello, World!\n");
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.
Missing Header File
int main(void) {
printf("Hello, World!\n");
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.
Mismatched Braces
#include <stdio.h>
int main(void) {
printf("Hello, World!\n");
return 0;
}
hello.c:5:1: error: expected declaration or statement at end of input
While this code looks fine, if you accidentally omit a } while writing, you'll get this kind of error. The compiler is telling you "something's missing at the end of the file" — usually an unclosed brace.
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
📖 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



