C Primer

Already Know C?: This section is entirely optional. If you are already comfortable programming in C, skip the primer and proceed directly to the algorithms content.

This source uses C extensively to demonstrate algorithms and data structures. As discussed in the Introduction, that choice is intentional. The objective, however, is not to teach C for its own sake.

Higher-level programming languages provide valuable abstractions that allow us to solve problems without continually reasoning about memory representation, indirection, allocation, or many details of program execution. Those abstractions are useful precisely because they hide complexity. During learning, however, the same layers can hide the mechanisms we are trying to understand. C gives us a useful way to selectively pull those layers back.

By working closer to representation, memory, and execution, we can examine how data structures and algorithms are actually constructed rather than learning only how to use existing implementations. The goal is to develop a sufficiently strong mental model that the underlying concepts can be recognized and applied in unfamiliar languages, systems, and problem domains.

Memorizing an implementation is of little value if the knowledge cannot survive a change in context. The purpose of C in this source is therefore pedagogical: it provides a platform for developing understanding that can later be carried elsewhere.

Scope

This is not a comprehensive C course. It introduces enough C and supporting systems knowledge to begin working through the algorithms and data structures in this source.

Topics are introduced when they become useful rather than in the order of a traditional language reference. Readers who are new to C should expect to consult documentation and other references while learning the language in parallel with the algorithms content.

This primer is intentionally hands-on. Readers should be prepared to use debuggers, inspect memory and process state, examine compiler output, and use other system tools to observe how C programs interact with the machine. Where practical, concepts will be investigated directly rather than presented only as rules.

The recurring approach is:

flowchart LR
    A[Predict] --> B[Build]
    B --> C[Observe]
    C --> D[Explain]
    D --> E[Modify]

A Systems-Oriented Approach

Some topics in this primer may initially appear to belong more to operating systems or computer architecture than to C itself:

  • processes
  • virtual memory
  • memory mappings
  • stack frames
  • executable files
  • compiler behavior

They are included only where they improve our ability to reason about C programs.

Implementation Note: Throughout this primer, we will distinguish among the C language, the compiler, the operating system, and the hardware. Behavior observed on Linux is not automatically required by C, and an operation supported by the underlying machine is not necessarily valid according to the language.

Getting Started

Most introductions to C begin with syntax.

We will begin only slightly higher than the machine itself: by creating a small C program and following it from source code to executable.

From there, we can ask a more interesting question:

We begin, as tradition demands, with Hello, World!.