Lua for C Developers
Move from C to Lua 5.1: numbers, truth rules, tables, closures, modules and metatable objects. Build a stock ledger and port complete C inventory and room-reservation programs.
C and Lua side by side
The same program in C and in Lua, from the lessons of this course.
Print and Comment
A C executable starts in main and uses library functions such as printf to produce output. A Lua file can execute statements directly. Lua needs neither a header include nor a declaration for its built-in print function. Both languages support quoted text…
C1#include <stdio.h>2 3int main(void)4{5 // Print one greeting.6 printf("%s%s\n", "Hi", ", Ada");7 return 0;8}Lua1-- Print one greeting.2print("Hi" .. ", Ada")Absence and Table Fields
C often represents optional data with a null pointer, a separate presence flag, or both. A null pointer does not remove a member from a C struct: the member remains part of the declared layout. Lua nil represents absence. A missing table field reads as nil…
C1#include <stdio.h>2 3struct Entry {4 int present;5 const char *text;6};7 8int main(void)9{10 struct Entry entry = {1, NULL};11 puts(entry.present ? "present" : "absent");12 return 0;13}Lua1local entry = {text = "ready"}2entry.text = nil3if entry.text == nil then4 print("absent")5else6 print("present")7endNumeric for and Limits
A C for statement has separate initialization, condition, and update expressions. A Lua numeric for takes a start value, a limit, and an optional step. The default step is one. Its limit is inclusive when reached, so translating a strict C upper bound…
C1#include <stdio.h>2 3int main(void)4{5 int total = 0;6 for (int i = 1; i < 3; ++i) {7 total += i;8 }9 printf("%d\n", total);10 return 0;11}Lua1local total = 02for i = 1, 3 do3 total = total + i4end5print(total)Functions and Arguments
C function declarations specify parameter and return types. Lua uses function ... end without type declarations, and local function keeps the function name local. A function is a value that can be assigned or passed to another function, much as a C function…
C1#include <stdio.h>2static int twice(int x) {3 return x * 2;4}5int main(void) {6 printf("%d\n", twice(4));7 return 0;8}Lua1local function twice(x)2 return x * 23end4print(twice(4))Arrays to Dense Tables
A C array has fixed storage, and a separate length commonly records how much of that storage is in use. Removing an element requires shifting later values and updating that length. A Lua table can represent a growable sequence using consecutive integer keys…
C1#include <stddef.h>2#include <stdio.h>3int main(void) {4 const char *items[] = {"a", "b", "c"};5 size_t length = sizeof items / sizeof items[0];6 for (size_t i = 1; i + 1 < length; ++i) {7 items[i] = items[i + 1];8 }9 --length;10 for (size_t i = 0; i < length; ++i) {11 printf("%s%s", i == 0 ? "" : ",", items[i]);12 }13 putchar('\n');14 return 0;15}Lua1local items = {"a", "b", "c"}2table.remove(items, 2)3print(table.concat(items, ","))Sorting and Comparators
C qsort and Lua table.sort both rearrange their input. Their comparator contracts differ. A C comparator receives pointers to elements and returns a negative integer, zero, or a positive integer. A Lua comparator receives values and returns a boolean that…
C1#include <stdio.h>2#include <stdlib.h>3static int compare(const void *left, const void *right) {4 int a = *(const int *)left;5 int b = *(const int *)right;6 return (a > b) - (a < b);7}8int main(void) {9 int items[] = {3, 1, 2};10 qsort(items, sizeof items / sizeof items[0], sizeof items[0], compare);11 printf("%d\n", items[0]);12 return 0;13}Lua1local items = {3, 1, 2}2table.sort(items)3print(items[1])
Syllabus
10 chapters48 lessons1 project48 challenges99 quiz questionsFirst Lua Programs
6 lessonsValues and Decisions
5 lessonsIteration and Flow
5 lessonsFunctions and Closures
5 lessonsTables as Collections
5 lessonsOrdering, Modules and Errors
5 lessonsObjects and Owned State
5 lessonsComposition and Object Lookup
6 lessonsProject: Stock Ledger
4 lessonsFinal C Ports
2 lessonsFrequently asked questions
Who is Lua for C Developers for?
For people who already write C and want to learn Lua. Each lesson starts from C you know and shows how Lua does the same thing, so you skip the beginner basics.
Do I need to know Lua already?
No. The course starts with your first Lua program. You need C basics: variables, conditions, loops, functions and collections.
How many lessons does Lua for C Developers have?
Lua for C Developers contains 48 interactive lessons you can complete at your own pace.
What does Lua for C Developers cover?
The course is organized into chapters covering: First Lua Programs, Values and Decisions, Iteration and Flow, Functions and Closures, and more.
Is Lua for C Developers free?
Yes. Every course on Coddy is free to start. You can complete lessons in your browser without any downloads or signup.