Lua Introduction to Lua C API

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!


  • lua_State *L = lua_open(); // Create a new VM state; Lua 5.0
  • lua_State *L = luaL_newstate(); // Create a new VM state; Lua 5.1+
  • int luaL_dofile(lua_State *L, const char *filename); // Run a lua script with the given filename using the specified lua_State
  • void luaL_openlibs(lua_State *L); // Load all standard libraries into the specified lua_State
  • void lua_close(lua_State *L); // Close VM state and release any resources inside
  • void lua_call(lua_State *L, int nargs, int nresults); // Call the luavalue at index -(nargs + 1)


Lua as well provides a proper C API to it's Virtual Machine. In contrary to VM itself, C API interface is stack based. So, most of the functions intended to be used with data is either adding some stuff on-top of virtual stack, or removing from it. Also, all the API calls must be used carefully within stack and it's limitations.

In general, anything available on Lua language can be done using it's C API. Also, there is some addition functionality like direct access to internal registry, change behavior of standard memory allocator or garbage collector.

You can compile provided Lua C API examples by executing following on Your terminal:

$ gcc -Wall ./example.c -llua -ldl -lm

Got any Lua Question?