Unless otherwise noted, any function that accepts valid indices can also be called with pseudo-indices, which represent some Lua values that are accessible to C code but which are not in the stack. Pseudo-indices are used to access the thread environment, the function environment, the registry, and the upvalues of a C function (see C Closures).
The thread environment (where global variables live) is always at pseudo-index LUA_GLOBALSINDEX
The environment of the running C function is always at pseudo-index LUA_ENVIRONINDEX
To access and change the value of global variables, you can use regular table operations over an environment table. For instance, to access the value of a global variable, do
lua_getfield(L, LUA_GLOBALSINDEX, varname);