0Pricing
Lua Academy · Lesson

Calling Lua Functions from C

Load and execute Lua scripts and functions from C code.

Overview

To call a Lua function from C: push the function, push arguments, call lua_pcall, then read the results from the stack.

Loading a Lua Script

luaL_dofile(L, filename) loads and executes a Lua file. Or use luaL_loadfile + lua_pcall for separate load/call.

// C code
if (luaL_dofile(L, "config.lua") != LUA_OK) {
  fprintf(stderr, "Error: %s\n", lua_tostring(L, -1));
  lua_pop(L, 1);
}

All lessons in this course

  1. The Lua C API Stack Model
  2. Calling Lua Functions from C
  3. Registering C Functions in Lua
  4. Managing State and Error Handling
← Back to Lua Academy