Skip to content
📖 Auto vs Modern CBook contents

Modern C Through Auto ​

An adaptation of Jens Gustedt's Modern C for the Auto programming language, translated through the a2c transpiler pipeline.

Why This Book? ​

Jens Gustedt's Modern C is one of the most rigorous introductions to the C programming language available. It treats C not as a legacy artifact, but as a living language with modern idioms -- designated initializers, size_t for sizes, EXIT_SUCCESS for program termination, and type-safe patterns that avoid the pitfalls of older C style.

This book adapts those lessons for Auto, a systems programming language that compiles to C through the a2c transpiler. Auto provides a cleaner surface syntax -- let instead of type-first declarations, for i in 0..n instead of for (int i = 0; i < n; i++), is instead of switch -- while retaining C's operational semantics.

The goal is not to hide C. It is to teach C's concepts through Auto's clarity, then show you exactly what C code is produced. Every Auto program in this book has a corresponding C translation that you can inspect, compile, and run.

Who Is This Book For? ​

This book is for three audiences:

  1. C learners who want a gentler on-ramp to Modern C's rigorous approach. Auto's syntax lets you focus on concepts without fighting with semicolons, format strings, and header files.
  2. Auto programmers who want to understand what happens under the hood. Knowing the C mapping makes you a better Auto programmer.
  3. Systems programmers who work in both languages and want a clear correspondence between high-level intent and low-level implementation.

You do not need prior C experience. You do need basic programming literacy -- variables, functions, loops -- in any language.

Modern C vs. Traditional C ​

The phrase "Modern C" refers to the style promoted by the C17 and C23 standards, not the K&R style that many people associate with C. Key differences include:

  • Designated initializers instead of positional initialization.
  • size_t for sizes and counts instead of int.
  • EXIT_SUCCESS and EXIT_FAILURE instead of raw 0 and 1.
  • bool from <stdbool.h> instead of rolling your own boolean.
  • Compound literals for anonymous array and struct values.
  • restrict qualifiers for aliasing optimization hints.

Auto adopts all of these as defaults. When a2c generates C, it uses modern idioms.

The Four Levels ​

Modern C organizes its material into four levels of increasing depth. We follow the same structure:

  • Encounter (Chapters 0-3): Getting started, program structure, control flow. You meet the language.
  • Acquaintance (Chapters 4-7): Types, pointers, I/O, and how values live in memory.
  • Cognition (Chapters 8-11): Style, functions, arrays, and heap allocation. You understand the model.
  • Experience (Chapters 12-15): Preprocessor, performance, concurrency, and building real programs.

This volume covers the Encounter level. By the end, you will understand how Auto programs are structured, how they flow, and what C code they produce.

Each level builds on the one before. You should read the Encounter chapters in order. Later levels will assume you have mastered the material here.

The Encounter chapters lay the foundation for everything that follows. Even if you have programmed in C before, read them carefully -- the Auto-to-C mapping introduces concepts that later chapters build on.

What You Will Learn in This Volume ​

By the end of this Encounter volume, you will be able to:

  • Write, compile, and run Auto programs that transpile to C.
  • Understand the mapping between Auto syntax and C output.
  • Use variables, arrays, functions, and control flow in both languages.
  • Read C code generated by the a2c transpiler and understand every line.
  • Recognize the modern C idioms that Auto promotes as defaults.

How to Read This Book ​

Each chapter presents a concept from Modern C in three layers:

  1. The concept itself -- explained in plain language, with reference to how C handles it.
  2. Auto syntax -- how you write it in Auto, which is simpler and more regular.
  3. The transpiled C -- what a2c produces, so you understand the mapping.

Code blocks are labeled clearly:

auto
// This is Auto source code
let x int = 42
c
// This is the C output from a2c
int x = 42;

Complete programs appear as Listings with both the Auto source and the expected C output. You can find the files in the listings/ directory alongside this book.

We recommend reading with a terminal open. Type the examples. Run them. Modify them. Break them. Understanding comes from doing.

Side-by-Side Comparisons ​

Throughout the book, you will see Auto and C code presented side by side. The Auto code is always on the left (or first), the C code on the right (or second). This is intentional: you should learn to mentally translate between the two. The Auto syntax is what you write; the C syntax is what actually runs.

When you see a side-by-side comparison, try to identify each difference before reading the explanation. This exercise builds the intuition you need to read generated C fluently.

The a2c Transpiler Pipeline ​

The a2c transpiler converts Auto source code to C. The pipeline works as follows:

Auto source (.at)
    |
    v
a2c parser -- parses Auto syntax into an AST
    |
    v
a2c type checker -- infers types, validates semantics
    |
    v
a2c code generator -- emits C code
    |
    v
C source (.c) -- compiled with any C compiler (gcc, clang, MSVC)

You invoke it in two steps:

bash
auto a2c          # Transpile .at files to .c
auto b            # Build: invoke the C compiler

The auto a2c command produces human-readable C. The auto b command compiles that C into an executable. At any point, you can inspect the generated C to understand exactly what your Auto program is doing at the C level.

What a2c Does Not Do ​

The transpiler is deliberately simple. It does not:

  • Optimize your code (that is the C compiler's job).
  • Manage memory for you (pointers and allocation are manual, as in C).
  • Provide a garbage collector or runtime system.
  • Change the semantics of your program.

What you write in Auto is what runs in C. The transpiler is a syntax bridge, not a magic box.

The Runtime ​

Auto ships with a minimal C runtime (stdlib/runtime.c) that includes the standard headers most programs need: <stdio.h>, <stdlib.h>, <string.h>, <stdbool.h>, <stdint.h>, and <inttypes.h>. The a2c transpiler includes the appropriate headers based on which features your program uses. You rarely need to think about includes -- they are handled automatically.

Conventions ​

Code Blocks ​

Code blocks in this book use one of three labels:

  • auto -- Auto source code, the input to a2c.
  • c -- C code, either written manually for comparison or produced by a2c.
  • bash -- Shell commands for building and running programs.

Listings ​

Listings are complete, runnable programs stored in the listings/ directory. Each listing contains:

  • main.at -- The Auto source.
  • main.expected.c -- The expected C output from a2c.
  • pac.at -- The package configuration file.

C Deep Dive Callouts ​

C Deep Dive: These callouts explore C-specific details that go beyond what Auto exposes directly. They are for readers who want to understand the underlying C mechanics -- the calling convention, the memory layout, the undefined behavior traps. You can skip them on a first reading and return later.

Takeaways ​

Takeaway: Key principles are highlighted in takeaway boxes. These distill the most important lesson from a section into a single sentence. Collect them as your personal reference.

Quick Reference ​

Modern C ConceptC SyntaxAuto Equivalent
Main functionint main(void)fn main()
Variable declarationint x = 5;let x int = 5
Mutable variableint x = 5; (same)var x int = 5
Arraydouble A[5] = {...};let A [5]float = [5]float{...}
For loopfor (int i = 0; i < n; i++)for i in 0..n
While loopwhile (cond) { }while cond { }
If/elseif (cond) { } else { }if cond { } else { }
Switchswitch (x) { case ... }is x { ... }
Printprintf("%s\n", "text");print("text")
Success exitreturn EXIT_SUCCESS;(implicit, return 0)
Type aliastypedef int Width;type Width int
Structstruct Point { int x; int y; };type Point { x int, y int }
Designated init(struct Point){.x = 1, .y = 2}Point(1, 2)
Boolean typebool (via <stdbool.h>)bool
Size typesize_t (via <stddef.h>)uint

About the Original Book ​

Jens Gustedt's Modern C (second edition) is published by Manning Publications and is also available freely online. It is organized into four levels -- Encounter, Acquaintance, Cognition, and Experience -- that progressively deepen the reader's understanding of C.

We have adapted the structure and many of the examples from Modern C, but this book is not a replacement for the original. If you want to master C itself -- every corner case, every undefined behavior pitfall, every optimization opportunity -- read Gustedt's book directly. Our goal here is to make those same concepts accessible through Auto's cleaner syntax, with the C mapping always visible.

Acknowledgments ​

This adaptation stands on the shoulders of:

  • Jens Gustedt, for writing Modern C and making it freely available.
  • The C standards committee (ISO/IEC JTC1/SC22/WG14), for evolving C into a modern language.
  • The Auto language project, for providing the a2c transpiler and toolchain.

What Comes Next ​

Chapter 1 gets you started with your first Auto program, adapted from the opening example of Modern C. You will see how arrays, loops, and output work in both languages. Chapter 2 breaks down program structure -- declarations, definitions, and statements. Chapter 3 covers control flow: conditionals, iterations, and multiple selection.

Let us begin.

Released under the MIT License.