TRT
Sign in

Foundations · Module 0 · Lesson 1 of 3

Installing a Compiler and Your First C Program

Install a C compiler, write a Hello World program, compile it into an executable, run it, and fix common beginner mistakes.

FreeAbout 14 minutes

By the end you can

  • Install a C compiler (GCC or Clang) and verify it works from the terminal
  • Write a minimal C program with a main function and a printf statement
  • Compile a C source file with gcc or clang and run the resulting executable
  • Read compiler error messages and fix syntax mistakes such as missing semicolons

01 · A question first

You open a text editor, type a few lines, save the file, and nothing happens. A C source file is just plain text until a compiler translates it into machine code the CPU can execute. Which command turns your text file into a program you can actually run, and what do you do when the compiler complains?

Make a guess, then step through the trace below to check it.

02 · Watch it run

From source code to a running Hello World programstep 1 of 3
#include <stdio.h> int main(void) {    printf("Hello, world!\n");    return 0;}
  1. file 0hello.c: source text(looked at in this step)
  2. file 1executable: none
  3. file 2terminal: waiting

You write the program in a text editor and save it as hello.c. It is plain text on disk.

Machine work: create hello.c on disk

03 · The idea

Start with the problem

Imagine you have just opened a blank file in a text editor. You type these lines:

c
#include <stdio.h>
 
int main(void) {
    printf("Hello, world!\n");
    return 0;
}

You save the file as hello.c. You double-click the file. Nothing happens. You open it again. Still nothing.

This is the first surprise for anyone learning C: a C source file is just text. It does not run by itself. Unlike some languages where you can press a "run" button inside an editor, C source code must first be translated by a compiler into machine code — instructions your CPU understands — and saved as an executable file. Only then can you run it.

The most common beginner question is: which program does the translating, and which command do I type? This lesson answers that, step by step, assuming you have never compiled anything before.

What you need before you begin

You need two things:

  1. A text editor. Any plain-text editor works — VS Code, Sublime Text, Vim, Emacs, Notepad, or even the built-in editor in your terminal. Do not use a word processor such as Microsoft Word or Google Docs, because those add hidden formatting that will confuse the compiler.

  2. A C compiler. The compiler is the program that translates your text into machine code. The two most common are:

    • GCC (GNU Compiler Collection) — the command is gcc. Standard on Linux and installable on macOS and Windows.
    • Clang — the command is clang. Default on macOS and installable everywhere else.

Either one works for this track. I will use gcc in the examples, but clang is interchangeable.

Checking whether a compiler is already installed

Open your terminal (Command Prompt or PowerShell on Windows, Terminal on macOS, a terminal app on Linux) and type:

bash
gcc --version

If you see version information such as gcc (GCC) 13.2.0, you are ready to go. If the terminal says command not found: gcc or 'gcc' is not recognized, you need to install it:

  • Linux (Ubuntu/Debian): sudo apt update && sudo apt install build-essential
  • macOS: xcode-select --install (installs Clang and the command-line tools)
  • Windows: Install MinGW-w64 or use WSL (Windows Subsystem for Linux) and follow the Linux steps inside WSL.

After installing, close and reopen your terminal, then run gcc --version again to confirm.

Writing your first program

Type the Hello World program above into your editor and save it as hello.c. Let me explain what each line does, because every line matters:

c
#include <stdio.h>

The #include is a preprocessor directive. It tells the compiler to bring in the standard input/output header, stdio.h, which declares printf and other text functions. Without this line, the compiler would not know what printf means and would report an error.

c
int main(void) {

This defines the main function. Execution of your program always begins at main. The int means main returns an integer to the operating system when it finishes (a status code: 0 usually means success). The (void) means main takes no inputs from outside. The opening brace { starts the body of the function.

c
    printf("Hello, world!\n");

printf is a function that prints text to the terminal. The text inside the double quotes is a string literal. The \n is an escape sequence that means "newline" — it moves the cursor to the next line so your next prompt appears on a fresh line. The semicolon ; ends the statement. Every statement in C ends with a semicolon.

c
    return 0;

This ends main and returns the status code 0 to the operating system, signaling that the program finished successfully.

c
}

The closing brace ends the main function.

Compiling and running

With hello.c saved, go back to your terminal and make sure you are in the same folder as the file. Then compile it:

bash
gcc hello.c -o hello

Breaking down the command:

  • gcc — invokes the compiler driver.
  • hello.c — your source file, the input.
  • -o hello — tells the compiler to name the output executable hello. (The -o stands for "output.")

If there are no errors, the terminal prints nothing and simply returns to the prompt. That is good news: it means compilation succeeded and a new file named hello now exists in your folder. On Linux and macOS you can see it with ls; on Windows it will be hello.exe.

Now run it:

bash
./hello

(The ./ means "run the file named hello in the current folder.")

You should see:

text
Hello, world!

Congratulations — you have written, compiled, and run your first C program.

What actually happened

The gcc command did not perform one magic step. It ran a pipeline of four tools in sequence:

  1. Preprocessor — handles #include and macros.
  2. Compiler — checks your syntax and turns the code into assembly.
  3. Assembler — turns assembly into machine code in an object file.
  4. Linker — combines your object file with the standard library into the final executable.

The compiler driver (gcc) orchestrates all four so you only type one command. You will learn each stage in detail in the next module.

Reading compiler error messages

Beginners make mistakes, and that is expected. The compiler will catch many of them before your program ever runs. Consider this broken version:

c
#include <stdio.h>
 
int main(void) {
    printf("Oops, missing semicolon")
    return 0;
}

If you compile this:

bash
gcc broken.c -o broken

You will see something like:

text
broken.c:4:38: error: expected ';' after expression
    4 |     printf("Oops, missing semicolon")
      |                                      ^
      |                                      ;
1 error generated.

Let me read that error:

  • broken.c:4:38 means the problem is in broken.c, on line 4, column 38.
  • expected ';' before 'return' means the compiler found return where it expected a semicolon ending the previous statement.

The fix is to add the missing semicolon after the printf line, then compile again. Compiler messages are your friend — they tell you the file, the line, and usually what is wrong. Read them from top to bottom and fix the first error first, because one early mistake can create a cascade of confusing later messages.

Common beginner mistakes

MistakeWhat goes wrongFix
Missing semicolon at end of a statementSyntax error; no executable producedAdd ;
Missing #include <stdio.h>Compiler does not know printfAdd the include line
Typing main as Main or MAINC is case-sensitive; execution never startsUse lowercase main
Forgetting return 0;Often still works, but omitting the status is sloppyAdd return 0;
Using smart/curly quotes “ ” instead of straight quotes " "Syntax error in the stringUse straight double quotes

Where it is used

Every C programmer, from a student writing their first loop to an engineer building the Linux kernel, follows this same edit-compile-run cycle. Understanding that the compiler is a separate tool — and learning to read its error messages — is the habit that separates programmers who get stuck from programmers who move forward.

In the next lesson, you will learn how to store values in variables, how to assign them, and how to combine them with expressions so your programs can do more than print fixed text.

04 · Prove it

Question 1 of 3 · answer from memory

Which command compiles the source file hello.c into an executable named hello?

05 · Practice

Something wrong with this lesson? Report a problem

Not started

Sign in to save your learning progress.

Sign in to save