Lesson 3 +10 XP

The Compiler and How C Programs Run

The Compiler and How C Programs Run

Going from a source file to a running program takes a few steps. In C the key tool is called a compiler (like gcc or clang).

The build pipeline

  1. You write a .c file containing source code.
  2. The preprocessor pulls in headers (the #include lines).
  3. The compiler translates it into machine code.
  4. The linker merges things into one final executable.

What is a header?

A line like #include <stdio.h> brings in declarations for standard functions such as printf. It is just instructions saying "grab this library".

Build tools

  • gcc and clang are the most popular C compilers, used from a terminal.
  • An IDE (like Visual Studio, Code::Blocks, or VS Code with extensions) wraps this so "Run" just works.

How do I compile and run?

gcc hello.c -o hello
./hello
  • gcc hello.c -o hello compiles the source into an executable called hello.
  • ./hello runs the program (on Windows just hello).

The make of libraries

Real programs rarely stand alone. They link in libraries: reusable code somebody already wrote (like the standard library libc) instead of typing everything from scratch.

TL;DR

  • Flow: ``#include`` header, compile, link, run.
  • gcc source.c -o program compiles a C file.
  • #include <stdio.h> pulls in standard functions like printf.
  • Libraries let you reuse code instead of reinventing it.