Skip to content
📖 Auto vs The Little Book of CBook contents

Language Basics ​

This chapter covers the fundamental building blocks of Auto and how they map to C. You will learn about data types, variables, operators, control flow, loops, functions, and scope.

11. Data Types ​

Auto provides five basic types that map directly to C types:

Auto TypeC TypeDescription
intintInteger (typically 32-bit)
floatfloatFloating-point number
boolintBoolean (0 or 1)
charcharSingle character
strchar*String (null-terminated)

Auto's bool maps to C's int because C did not have a native _Bool until C99, and many C codebases still use int for boolean values.

The a2c transpiler adds the correct format specifier for each type: %d for int, %f for float, %c for char, %s for str.

12. Constants and Scope ​

Auto uses let for immutable bindings and var for mutable variables:

auto
let pi float = 3.14      // immutable
var count int = 0        // mutable
count = count + 1        // okay

In C, this maps to const float pi = 3.14f; and int count = 0;. Block scope works the same way in both languages — variables declared inside { } are local to that block.

13. Operators ​

Auto's operators are nearly identical to C's.

Arithmetic operators (+, -, *, /, %), comparison operators (==, !=, >, <, >=, <=), and logical operators (&&, ||) all map directly to their C equivalents.

C Only: C has bitwise assignment operators (&=, |=, ^=, <<=, >>=) and the comma operator. Auto does not provide these directly.

14. Control Flow ​

Auto offers if/else for conditional logic and is for pattern matching (which maps to C's switch).

The is expression transpiles to a C switch statement. Unlike C's switch, Auto's is does not require break statements — each arm implicitly returns and there is no fall-through.

15. Loops ​

Auto provides for for counted iteration and while for conditional looping.

The range loop for i in 0..5 maps to for (int i = 0; i < 5; i++). The while loop maps directly to C's while with identical semantics.

C Only: C has do...while and goto. Auto does not provide goto. A do...while equivalent can be written with while true and break.

16. Functions ​

Auto functions declare parameters with types and optional return types. If no return type is specified, the function returns void in C.

The last expression in a function body is the return value. The a2c transpiler inserts explicit return statements in the generated C. No forward declarations are needed — Auto handles ordering.

17. Scope and Shadowing ​

Both Auto and C use block scope. Auto also supports shadowing — re-declaring a variable with the same name in an inner scope:

auto
let x int = 10
if true {
    let x int = 20
    print(x)    // 20
}
print(x)        // 10

In C, shadowing works the same way with nested { } blocks.

18. Type Casting ​

Auto provides explicit type casts between compatible types. let y float = float(x) maps to C's (float)x. Auto does not allow unsafe casts (e.g., casting str to int); the transpiler rejects invalid conversions.

19. Recursion ​

Recursion works identically in Auto and C. The factorial function in Listing 01-05 is a classic example. Auto supports direct and mutual recursion; the a2c transpiler handles forward declarations automatically.

20. Practice: Calculator ​

Put everything together with a simple calculator.

Notice that op == "+" in Auto becomes strcmp(op, "+") == 0 in the generated C. String comparison in C requires strcmp; Auto's == operator handles this automatically.

Quick Reference ​

ConceptAutoC
Integerintint
Floatfloatfloat
Booleanboolint (0/1)
Stringstrchar*
Charactercharchar
Immutablelet x int = 5const int x = 5;
Mutablevar x int = 5int x = 5;
If/elseif { } else { }if () { } else { }
Switchis x { ... }switch (x) { ... }
For loopfor i in 0..nfor (int i=0; i<n; i++)
While loopwhile cond { }while (cond) { }
Functionfn name(params) type { }type name(params) { }
Implicit returnlast expressionexplicit return
String comparea == bstrcmp(a, b) == 0

Released under the MIT License.