How to Install a C Compiler (GCC)
To run a C program, your computer needs a C compiler. The compiler turns your C code into a program the computer can run.
The most popular free C compiler is GCC (GNU Compiler Collection). In this lesson you will install it on Windows, macOS or Linux, and run your first program from the command line.
What You Will Learn
- What a compiler and an IDE are.
- How to install GCC on Windows, macOS and Linux.
- How to check that GCC is installed.
- How to compile and run a C program with one command.
- How to fix the most common setup errors.
Compiler, Editor and IDE Explained
Exam definition: A compiler is a program that converts the whole source code written in a high-level language into machine code at once, before the program runs.
| Tool | What it does | Examples |
|---|---|---|
| Compiler | Converts C code into a runnable program | GCC, Clang, MSVC |
| Text editor | A place to type your code | VS Code, Notepad++ |
| IDE | Editor + compiler + debugger in one app | Code::Blocks, Dev-C++, Visual Studio |
IDE means Integrated Development Environment. "Integrated" means everything is joined together in one app.
Install GCC on Windows (using MSYS2)
- Go to msys2.org, download the installer and install it in the default folder
C:\msys64. - Open the app MSYS2 UCRT64 from the Start menu.
- Type this command and press Enter (press Y when asked):
pacman -S mingw-w64-ucrt-x86_64-gcc - Add GCC to the PATH so Windows can find it: search "Environment Variables" → Edit the system environment variables → Environment Variables → select Path → Edit → New → type
C:\msys64\ucrt64\bin→ OK. - Close and open a new Command Prompt, then check:
gcc --version
Easier option for beginners: install Code::Blocks and choose the setup file that has "mingw" in its name. It comes with GCC already inside.
Install GCC on macOS
- Open the Terminal app.
- Run:
xcode-select --installand click Install. - Check:
gcc --version. On a Mac,gccruns Apple's Clang compiler, which works the same way for C.
Install GCC on Linux (Ubuntu / Debian)
sudo apt update
sudo apt install build-essential
gcc --version
On Fedora, use sudo dnf install gcc.
Set up VS Code (optional but recommended)
- Install Visual Studio Code.
- Open the Extensions panel and install C/C++ by Microsoft.
- Open the built-in terminal (Ctrl + `) and use the
gcccommands below.
Memory trick: Setup in 3 words: Install → PATH → Check. Install GCC, add it to PATH (Windows), check with gcc --version.
GCC Command Syntax
gcc source_file.c -o program_name
| Part | Meaning |
|---|---|
gcc | Runs the compiler. |
source_file.c | Your C file. |
-o program_name | Name of the program to create. Without -o, GCC names it a.exe (Windows) or a.out (Linux / macOS). |
-Wall (optional) | Shows all common warnings. Always use it while learning. |
-std=c17 (optional) | Uses a specific C standard (C17 here). |
Example 1: Test Your Compiler with a C Program
Save this code in a file named test.c:
#include <stdio.h>
int main(void)
{
printf("My C compiler is working!\n");
return 0;
}
Open a terminal in the same folder and run:
gcc test.c -o test
./test (Linux / macOS)
test.exe (Windows Command Prompt)
Output
My C compiler is working!
Code Explanation
gcc test.c -o test: compilestest.cand creates a program calledtest(test.exeon Windows).- If there is no error, GCC prints nothing. No message means success.
./test: runs the program../means "in the current folder" (needed on Linux / macOS).- The program prints the message and ends.
How It Works
- When you type
gcc, the system looks for the compiler in the folders listed in PATH. - GCC reads
test.c, checks it for errors and turns it into machine code. - It saves the result as a new file (
testortest.exe). - You can now run this file again and again without compiling, until you change the code.
Example 2: Turn On Warnings with -Wall
Warnings are hints about possible problems. This program has a variable that is never used:
#include <stdio.h>
int main(void)
{
int x;
printf("Hello\n");
return 0;
}
gcc -Wall warn.c -o warn
Output
warn.c: In function 'main': warn.c:5:9: warning: unused variable 'x' [-Wunused-variable]
A warning does not stop the program from being created, but it tells you something may be wrong. An error stops compilation completely.
Important Points to Remember
- GCC = GNU Compiler Collection. It is free.
- Check installation with
gcc --version. - Compile:
gcc file.c -o file. Run:./fileorfile.exe. - No output after compiling means success.
- Use
-Wallto see warnings.
Common Setup Mistakes and Fixes
| Problem | Why it happens | Fix |
|---|---|---|
'gcc' is not recognized as an internal or external command | GCC is not in PATH | Add C:\msys64\ucrt64\bin to PATH and open a new terminal |
| Still not recognized after adding PATH | The old terminal window does not know the new PATH | Close it and open a new one |
test: command not found (Linux / macOS) | The current folder is not searched | Run ./test, not test |
No such file or directory | The terminal is in a different folder | Use cd to go to the folder that has your .c file |
File saved as test.c.txt | Notepad adds .txt | Choose "All files" when saving, or use VS Code |
C Compiler Setup Practice Questions
- What is a compiler?
- What is the full form of GCC and IDE?
- Write the command to compile
sum.cinto a program namedsum. - What file name does GCC use if you don't give
-o? - What is the difference between a warning and an error?
Show answers
- A program that converts the whole source code into machine code before it runs.
- GCC = GNU Compiler Collection. IDE = Integrated Development Environment.
gcc sum.c -o suma.exeon Windows,a.outon Linux / macOS.- A warning shows a possible problem but the program is still created. An error stops compilation.
Coding Exercise
Install GCC, then write a program about.c that prints your name and the output of this line: printf("Compiled on: %s\n", __DATE__);. Compile it with -Wall and run it.
Show solution
#include <stdio.h>
int main(void)
{
printf("Name: Riya Sharma\n");
printf("Compiled on: %s\n", __DATE__);
return 0;
}
gcc -Wall about.c -o about
./about
__DATE__ is filled in by the compiler with the date you compiled the program, like Nov 15 2026.
Quick Quiz: C Compiler Setup (MCQ)
-
What does GCC stand for?
A) General C Compiler B) GNU Compiler Collection C) Global Code Converter D) GNU Code Creator
-
Which command checks if GCC is installed?
A)
gcc --versionB)gcc --checkC)c --versionD)install gcc -
In
gcc hello.c -o hello, what does-odo?A) Optimizes code B) Opens the file C) Sets the output program name D) Shows output
-
Which option shows common warnings?
A)
-wB)-WallC)-warnD)-all -
An IDE contains:
A) Only a compiler B) Only an editor C) Editor, compiler and debugger together D) Only a browser
Show answers
- B: GNU Compiler Collection.
- A:
gcc --version. - C: it sets the output program name.
- B:
-Wall. - C: editor, compiler and debugger together.
Quick Revision for Exams and Interviews
Read this before your exam. The highlighted words are the answer.
Must-remember facts
| GCC | GNU Compiler Collection (free) |
|---|---|
| IDE | Integrated Development Environment |
| Check install | gcc --version |
| Compile | gcc file.c -o file |
| Run | ./file (Linux / macOS), file.exe (Windows) |
| Default output | a.out / a.exe |
| Show warnings | -Wall |
| Windows setup | MSYS2 + add C:\msys64\ucrt64\bin to PATH |
Frequently asked questions
- What is the difference between a compiler and an IDE?
- A compiler converts code into a program. An IDE is an app with editor + compiler + debugger together.
- What does "gcc is not recognized" mean?
- GCC is not in PATH. Add its
binfolder to PATH and open a new terminal. - Why do we write ./ before the program name on Linux?
./means "run it from the current folder".- What is the difference between a warning and an error?
- A warning still creates the program; an error stops compilation.
Remember
- Setup: Install → PATH → Check.
- No message after
gcc= success.
Next Topic
Next lesson: Structure of a C Program. You will learn the six parts of a C program that every exam asks about.