C: Setup: Install GCC & VS Code, Run Your First Program

🗓 Last updated: 2026-11-30👤 Written by: Gray✓ Reviewed by: Dana🏷 Category: C Tutorial

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 cc on macOS, sudo apt install gcc on Linux) → ② create hello.c → ③ compile with gcc -o hello hello.c and run ./hello (hello.exe on Windows). Section 1 explains each step.

TL;DR: A C toolchain needs three things: a compiler (MinGW-w64 or MSYS2 on Windows, built-in cc on macOS, GCC on Linux), an editor (VS Code + the C/C++ extension), and UTF-8 source files. Compile with gcc -Wall -Wextra -std=c99 -o hello hello.c, then run ./hello (hello.exe on Windows). If gcc is not recognized, fix PATH; if output is garbled, check the file encoding and run chcp 65001.

Reference entries in this section: printf int void #include

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)

  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.

(2) macOS Installation

macOS ships with Clang (the cc command), which already compiles standard C — no installation needed:

BASH
cc --version

If you want GCC, install it with Homebrew:

BASH
brew install gcc

⚠️ Note: Homebrew installs a versioned binary (for example gcc-14), not a command named gcc. Check what you got:

BASH
ls /opt/homebrew/bin/gcc-* 2>/dev/null || ls /usr/local/bin/gcc-*

Then either:

💡 For beginners GCC is optional: 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:

BASH
sudo apt install gcc

Verify the installation:

BASH
gcc --version
💡 Tip: Both Clang on macOS and GCC on Linux compile standard C; for beginners there is virtually no difference. This book writes 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:

(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.

⚠️ 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.

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

  1. Source encoding: in VS Code, confirm the file encoding (bottom-right) is UTF-8, not GBK/ANSI.
  2. Terminal code page: run chcp 65001 before executing the program to switch the console to UTF-8.
  3. In-program setting (Windows only):
C
// 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;
}
💡 If you just want to get the first program running, use plain ASCII output first and deal with encoding later.


3. Your First Program: Hello World

▶ Example

▶ 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.


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:

TEXT
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

BASH
gcc -o hello hello.c

Step 3: Run

BASH
./hello

On Windows:

BASH
hello.exe

(2) 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.

(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
💡 Tip: Always compile with 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 Yourself
TEXT
warning: '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

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

C
// Preprocessor directive
#include <stdio.h>

// Function definition
int main(void) {
    // I/O operation
    printf("Hello, World!\n")
    // Return result
    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.

(2) Undeclared Identifier

C
// Preprocessor directive
#include <stdio.h>

// Function definition
int main(void) {
    print("Hello, World!\n");
    // Return result
    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.

(3) Missing Header File

C
// Function definition
int main(void) {
    // I/O operation
    printf("Hello, World!\n");
    // Return result
    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.

(4) Mismatched Braces

C
// Preprocessor directive
#include <stdio.h>

// Function definition
int main(void) {
    // I/O operation
    printf("Hello, World!\n");
    // Return result
    return 0;
TEXT
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 {.

⚠️ 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.


6. 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.

🔗 Official docs: GCC Manual · cppreference C Reference

Reference entries in this section: printf int void #include

❓ FAQ

Q Windows says "gcc is not recognized" — how do I fix it?
A GCC's 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.
Q Should I install MinGW-w64, MSYS2, or WSL?
A For beginners, MinGW-w64 (the WinLibs package) is simplest: unzip and add 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.
Q VS Code says it cannot open source file "stdio.h" — what now?
A The compiler path is usually wrong. Press Ctrl+Shift+P → C/C++: Edit Configurations (UI), point the compiler to ...\bin\gcc.exe, select gcc-x64, and reopen the folder.
Q Why is my console output garbled?
A Check three things: the file is saved as UTF-8 in VS Code, the terminal uses UTF-8 (chcp 65001 on Windows), and Windows programs may need SetConsoleOutputCP(65001).
Q Why do I need ./hello after gcc hello.c?
A 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.
Q Do I have to fix every compiler warning?
A Yes, treat them as bugs. -Wall -Wextra catches uninitialized variables, implicit declarations, and format mismatches that later become real crashes.
Q On macOS gcc --version reports Apple Clang — is that a problem?
A No. On macOS 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

📝 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
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

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

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