Menu
CoddyTech
Course

C for Lua Developers

Move from Lua to C17: explicit types, pointers, bounded arrays, strings, ownership, structs and modular interfaces. Build a multi-file stock ledger and port two Lua programs.

Lua and C side by side

The same program in Lua and in C, from the lessons of this course.

  1. Printing and Comments

    Lua print converts each argument with tostring, separates arguments with tabs and ends the line for you; string.format raises an error when %d gets a value with no integer representation, such as 2.5. C printf trusts its format string: each conversion…

    Lua
    1print("value=" .. 7)
    C
    1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4#include <stddef.h>5#include <stdbool.h>6#include <limits.h>7#include <errno.h>8
    9int main(void) {10printf("value=%d\n",7);11return 0;12}
  2. Booleans and Branches

    Lua treats only nil and false as false: 0 and the empty string are both true. In C a scalar zero is false and a nonzero scalar is true; a non-null pointer is true even if it points at an empty string. So an empty string passes the test in both languages, but…

    Lua
    1local s = ""2print(s and "true" or "false")
    C
    1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4#include <stddef.h>5#include <stdbool.h>6#include <limits.h>7#include <errno.h>8
    9int main(void) {10const char *s=""; printf("%s\n",s?"true":"false");11return 0;12}
  3. Arrays Need Lengths

    A Lua table grows when you assign just past its end, and # reports the length of its sequence. A C array has a fixed element count, but a function array parameter is adjusted to a pointer and does not carry that count. Pass size_t length separately and…

    Lua
    1local a = {2, 4, 6}2a[#a + 1] = 83print(#a)
    C
    1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4#include <stddef.h>5#include <stdbool.h>6#include <limits.h>7#include <errno.h>8
    9int main(void) {10int a[]={2,4,6};printf("%zu\n",sizeof a/sizeof a[0]);11return 0;12}
  4. Function Arguments Are Values

    Lua passes every argument by value: a number or string is copied into the parameter, and a table argument copies the reference, so the function can change the table but assigning to the parameter only rebinds a local. C parameters also receive copies of their…

    Lua
    1local function change(n)2  n = 93end4local x = 25change(x)6print(x)
    C
    1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4#include <stddef.h>5#include <stdbool.h>6#include <limits.h>7#include <errno.h>8static void change(int n){n=9;(void)n;}9int main(void) {10int x=2;change(x);printf("%d\n",x);11return 0;12}
  5. Addresses and Dereferencing

    Lua table variables hold references: two names can share one table, but Lua has no address-of operator, no pointer arithmetic, and the garbage collector keeps a referenced table alive. In C &object obtains an address and *pointer accesses the pointed-to…

    Lua
    1local box = {2}2local alias = box3alias[1] = 54print(box[1])
    C
    1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4#include <stddef.h>5#include <stdbool.h>6#include <limits.h>7#include <errno.h>8
    9int main(void) {10int n=2;int *p=&n;*p=5;printf("%d\n",n);11return 0;12}
  6. Parsing with Status Results

    Lua tonumber returns nil instead of raising an error, and it accepts more than digits: surrounding spaces, a sign, hexadecimal and floats. C library parsers use explicit status information. sscanf reports the number of successful assignments, but matching a…

    Lua
    1local text = "12x"2print(text:match("^%d+$") ~= nil)
    C
    1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4#include <stddef.h>5#include <stdbool.h>6#include <limits.h>7#include <errno.h>8
    9int main(void) {10const char *s="12x";int ok=*s!=0;for(size_t i=0;s[i];i++)if(s[i]<'0'||s[i]>'9')ok=0;printf("%d\n",ok);11return 0;12}

Syllabus

10 chapters48 lessons1 project48 challenges99 quiz questions

More courses for Lua developers

Learn C from another language

Frequently asked questions

Who is C for Lua Developers for?

For people who already write Lua and want to learn C. Each lesson starts from Lua you know and shows how C does the same thing, so you skip the beginner basics.

Do I need to know C already?

No. The course starts with your first C program. You need Lua basics: variables, conditions, loops, functions and collections.

How many lessons does C for Lua Developers have?

C for Lua Developers contains 48 interactive lessons you can complete at your own pace.

What does C for Lua Developers cover?

The course is organized into chapters covering: Compile and Compute, Decisions and Loops, Arrays and Text, Functions and Interfaces, and more.

Is C for Lua Developers free?

Yes. Every course on Coddy is free to start. You can complete lessons in your browser without any downloads or signup.

Coddy programming languages illustration

Start C for Lua Developers for free

Get started