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)

  1. Visit https://www.mingw-w64.org and download the installer
  2. During installation, select the x86_64 architecture and win32 thread model
  3. Add the bin folder of your installation directory to the system PATH environment variable

Option 2: MSYS2 (more flexible)

BASH
pacman -S mingw-w64-x86_64-gcc

After installation, open a command prompt and verify:

BASH
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:

BASH
brew install gcc

macOS's built-in cc command actually invokes the Clang compiler, which can also compile C code. To confirm:

BASH
cc --version

Linux Installation

Most Linux distributions let you install GCC directly through the package manager:

BASH
sudo apt install gcc

Verify the installation:

BASH
gcc --version
💡 Tip: Both Clang on macOS and GCC on Linux can compile standard C code. For beginners, there's virtually no difference. This book uses the 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:

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.

⚠️ Note: When opening a folder in VS Code, create a dedicated folder (e.g. c-learn) and open the entire folder rather than a single file. This ensures extensions work properly.

Your First Program: Hello World

Example

C
#include <stdio.h>

int main(void) {
    printf("Hello, World!\n");
    return 0;
}
▶ Try it Yourself
TEXT
Hello, World!

Line-by-line breakdown:

💡 Tip: By convention, a program returns 0 for normal termination and a non-zero value for abnormal termination. This is how the operating system determines whether a program executed successfully.

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:

TEXT
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

BASH
gcc -o hello hello.c

Step 3: Run

BASH
./hello

On Windows:

BASH
hello.exe

Step-by-Step Compilation

If you want to understand each stage of compilation, you can run them individually:

BASH
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
🔥 Common Mistake: For daily development, 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
💡 Tip: Always compile with 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

C
#include <stdio.h>

int main(void) {
    printf("Hello, World!\n")
    return 0;
}
TEXT
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

C
#include <stdio.h>

int main(void) {
    print("Hello, World!\n");
    return 0;
}
TEXT
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

C
int main(void) {
    printf("Hello, World!\n");
    return 0;
}
TEXT
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

C
#include <stdio.h>

int main(void) {
    printf("Hello, World!\n");
    return 0;
}
TEXT
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.

⚠️ Note: The error line number may point to the line after the actual problem. If line 5 reports an error, check line 4 first. The compiler only detects the problem at the point where it can't continue, but the root cause is often on the previous line.

Project File Organization

As your programs grow more complex, it's a good idea to organize your files like this:

TEXT
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

Q After installing GCC, the command line says "gcc is not recognized as a command." What do I do?
A This means GCC's bin directory isn't in your system PATH. On Windows: right-click "This PC" → Properties → Advanced system settings → Environment Variables → edit Path → add the GCC bin directory path (e.g. C:\mingw64\bin), then restart the command prompt.
Q Should I choose VS Code or Dev-C++/Code::Blocks?
A VS Code is recommended. It's more modern, has a richer extension ecosystem, and provides better C support. Dev-C++ hasn't been updated in years, and Code::Blocks has an outdated interface with weak debugging capabilities. VS Code has a slightly steeper learning curve, but it's absolutely worth it in the long run.
Q What's the difference between compilation warnings and errors?
A Errors prevent compilation — no executable is generated. Warnings don't stop compilation, but they signal potential issues. Treat warnings as errors: compile with -Wall -Wextra and eliminate all warnings.
Q On macOS, the gcc command actually invokes Clang. Does this matter?
A Not at all for beginners. Clang is fully compatible with GCC's common options and standard C syntax. Differences only arise with GCC-specific extensions, and standard C code behaves identically on both.

📖 Summary

📝 Exercises

  1. Install GCC on your computer and verify with gcc --version; record the version number
  2. 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
  3. Intentionally create 3 compilation errors (missing semicolon, misspelled function name, missing header file), record the compiler's error messages, and practice reading error output
100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏