Skip to content

Module 1 · Getting Started with C — topic 3 of 4

Installing and Configuring the Environment

Install a free C compiler (GCC or Clang), set up VS Code, and learn the compile–run cycle from the terminal on Windows, macOS and Linux.

Beginner 15 min read·Lesson 3 of 37 in C Programming

Learning objectives

  • Install GCC or Clang on your operating system
  • Compile a program from the terminal and run the executable
  • Turn on useful compiler warnings

Definition

A C development environment consists of a compiler toolchain that translates source code into executables, an editor for writing code, and a terminal for running the compiler and the resulting programs.

Introduction

To run C you need a compiler. The two most widely used free compilers are GCC (GNU Compiler Collection) and Clang (part of LLVM). Both follow the ISO standard closely, are used in industry, and are what university labs typically provide.

You also need an editor. Any text editor works, but VS Code with the C/C++ extension gives syntax highlighting, error squiggles and a built-in terminal at no cost.

Why it matters

Understanding the command-line compile cycle means you are never stuck when an IDE's “Run” button breaks, and it is exactly what you will use in Linux labs, on servers and in exams.

Compiler warnings catch real bugs — using an uninitialised variable, a wrong printf format, an accidental = instead of ==. Turning them on is the cheapest debugging you will ever do.

Detailed explanation

Windows: install MSYS2 from msys2.org, open the *MSYS2 UCRT64* terminal and run pacman -S mingw-w64-ucrt-x86_64-gcc. Add C:\msys64\ucrt64\bin to your PATH so gcc works in any terminal.

macOS: run xcode-select --install. This installs Apple's Clang; the command gcc is an alias for it.

Linux (Ubuntu/Debian): run sudo apt install build-essential, which installs GCC, make and the standard headers.

Check the installation with gcc --version. Then compile with gcc hello.c -o hello: -o hello names the output file. On Windows the file is hello.exe; run it with .\hello (PowerShell) or ./hello (Linux/macOS).

FlagMeaning
-o nameName the output executable
-std=c11Compile as ISO C11
-Wall -WextraEnable most useful warnings
-gInclude debug information for a debugger
-O2Optimise for speed
-lmLink the maths library (Linux)

Syntax

Terminal
gcc -std=c11 -Wall -Wextra -g hello.c -o hello
./hello
  • Compile once after every change; the executable does not update itself.
  • On Windows PowerShell run .\hello.exe instead of ./hello.
  • Use the same flags for every exercise so warnings are consistent.

Code examples

Checking the compiler

Terminal
gcc --version

This prints the compiler version, for example gcc (Rev2, Built by MSYS2 project) 14.2.0. If you see “command not found”, the compiler is not on your PATH.

Your first compile

C
#include <stdio.h>
int main(void) {
printf("Toolchain works!\n");
return 0;
}
Output
Toolchain works!

Save as hello.c, run gcc hello.c -o hello, then run ./hello.

More examples

A warning that saves you

C
#include <stdio.h>
int main(void) {
int score = 9;
if (score == 10)
printf("Perfect\n");
else
printf("Score: %d\n", score);
return 0;
}
Output
Score: 9

If you accidentally write if (score = 10), -Wall warns “suggest parentheses around assignment used as truth value” — and the program would wrongly print “Perfect”.

Step-by-step explanation

  • int score = 9; declares and initialises the variable.
  • score == 10 compares; it is false, so the else branch runs.
  • printf("Score: %d\n", score); prints the value.
  • With the typo score = 10, the assignment would store 10 and the condition would be true — a bug -Wall points out.

Expected output

Running “A warning that saves you” prints exactly:

TextOutput
Score: 9

Practical applications

  • Build servers compile C projects with exactly these commands, usually driven by make or CMake.
  • Embedded developers use cross-compilers (e.g. arm-none-eabi-gcc) with the same flags.
  • Open-source projects require code to compile without warnings under -Wall -Wextra.

Best practices

  • Keep each exercise in its own folder.
  • Always compile with -Wall -Wextra and fix every warning.
  • Learn the terminal commands even if you use an IDE's Run button.

Common mistakes

Running an old executable

Avoid

# edit hello.c
./hello # still shows old output

Prefer

# edit hello.c
gcc hello.c -o hello
./hello

The executable is a separate file. Recompile after every change.

Naming the output after the source

Avoid

gcc hello.c -o hello.c

Prefer

gcc hello.c -o hello

-o hello.c overwrites your source file with machine code, destroying your work.

Important notes

  • Turbo C is obsolete and pre-standard; avoid it. Code from Turbo C tutorials that uses <conio.h>, clrscr() or void main() is not standard C.
  • On Linux, programs using <math.h> functions such as sqrt may need -lm at the end of the command.

Key takeaways

  • Install GCC (MSYS2 on Windows, Xcode tools on macOS, build-essential on Linux).
  • Compile with gcc -std=c11 -Wall -Wextra file.c -o name, then run the executable.
  • Warnings are bugs waiting to happen — fix them.

Try it yourself

  1. 1
    Install a compiler and paste the output of gcc --version into a comment at the top of your first program.
  2. 2
    Write if (x = 5) in a test program, compile with and without -Wall, and compare the messages.

Finished reading? Mark it complete to update your dashboard.

Suggested practice

3 questions test this exact topic. Answers and explanations are revealed after you submit.

  • BeginnerIn `gcc hello.c -o hello`, what does `-o hello` do?
  • BeginnerWhy should you compile with `-Wall -Wextra`?
  • BeginnerYou edit hello.c and run `./hello`, but the old output appears. What is the most likely reason?
Practise this topic