Skip to content
StudyFame

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.

ToolWhat it doesExamples
CompilerConverts C code into a runnable programGCC, Clang, MSVC
Text editorA place to type your codeVS Code, Notepad++
IDEEditor + compiler + debugger in one appCode::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)

  1. Go to msys2.org, download the installer and install it in the default folder C:\msys64.
  2. Open the app MSYS2 UCRT64 from the Start menu.
  3. Type this command and press Enter (press Y when asked):
    pacman -S mingw-w64-ucrt-x86_64-gcc
  4. 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.
  5. 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

  1. Open the Terminal app.
  2. Run: xcode-select --install and click Install.
  3. Check: gcc --version. On a Mac, gcc runs 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.

  • Install Visual Studio Code.
  • Open the Extensions panel and install C/C++ by Microsoft.
  • Open the built-in terminal (Ctrl + `) and use the gcc commands 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
PartMeaning
gccRuns the compiler.
source_file.cYour C file.
-o program_nameName 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

  1. gcc test.c -o test: compiles test.c and creates a program called test (test.exe on Windows).
  2. If there is no error, GCC prints nothing. No message means success.
  3. ./test: runs the program. ./ means "in the current folder" (needed on Linux / macOS).
  4. 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 (test or test.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: ./file or file.exe.
  • No output after compiling means success.
  • Use -Wall to see warnings.

Common Setup Mistakes and Fixes

ProblemWhy it happensFix
'gcc' is not recognized as an internal or external commandGCC is not in PATHAdd C:\msys64\ucrt64\bin to PATH and open a new terminal
Still not recognized after adding PATHThe old terminal window does not know the new PATHClose it and open a new one
test: command not found (Linux / macOS)The current folder is not searchedRun ./test, not test
No such file or directoryThe terminal is in a different folderUse cd to go to the folder that has your .c file
File saved as test.c.txtNotepad adds .txtChoose "All files" when saving, or use VS Code

C Compiler Setup Practice Questions

  1. What is a compiler?
  2. What is the full form of GCC and IDE?
  3. Write the command to compile sum.c into a program named sum.
  4. What file name does GCC use if you don't give -o?
  5. What is the difference between a warning and an error?
Show answers
  1. A program that converts the whole source code into machine code before it runs.
  2. GCC = GNU Compiler Collection. IDE = Integrated Development Environment.
  3. gcc sum.c -o sum
  4. a.exe on Windows, a.out on Linux / macOS.
  5. 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)

  1. What does GCC stand for?

    A) General C Compiler   B) GNU Compiler Collection   C) Global Code Converter   D) GNU Code Creator

  2. Which command checks if GCC is installed?

    A) gcc --version   B) gcc --check   C) c --version   D) install gcc

  3. In gcc hello.c -o hello, what does -o do?

    A) Optimizes code   B) Opens the file   C) Sets the output program name   D) Shows output

  4. Which option shows common warnings?

    A) -w   B) -Wall   C) -warn   D) -all

  5. An IDE contains:

    A) Only a compiler   B) Only an editor   C) Editor, compiler and debugger together   D) Only a browser

Show answers
  1. B: GNU Compiler Collection.
  2. A: gcc --version.
  3. C: it sets the output program name.
  4. B: -Wall.
  5. 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

GCCGNU Compiler Collection (free)
IDEIntegrated Development Environment
Check installgcc --version
Compilegcc file.c -o file
Run./file (Linux / macOS), file.exe (Windows)
Default outputa.out / a.exe
Show warnings-Wall
Windows setupMSYS2 + 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 bin folder 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.