The following is a plug-in for executing the Lua script in C language. You can drag the Lua file to the EXE to run it, or bind the 1.lua file in the current directory by default.
Header file and Lua. the location of LIB depends on your own situation, conio. the H header file is only used to allow getch () to read arbitrary characters after each execution and is not displayed. conio can be removed if conio cannot be compiled. h. Replace getch () with getchar ()
# Include <stdio. h> # include <conio. h> extern "C" {# include "G: \ library \ Lua \ lua-5.2.3 \ SRC \ Lua. H "# include" G: \ library \ Lua \ lua-5.2.3 \ SRC \ lualib. H "# include" G: \ library \ Lua \ lua-5.2.3 \ SRC \ lauxlib. H "} # pragma comment (Lib," C: \ Lua. lib ") int main (INT argc, char * argv []) {char * P; If (argc> = 2) {P = argv [1];} else {P = "1.lua";} printf (" binding: % s \ n ", P); While (1) {lua_state * l = lual_newstate (); lual_openlibs (L ); lual_do File (L, P); lua_close (l); printf ("execution completed! \ N "); getch ();} return 0 ;}
1. Some basic demos in The Lua File
B = 10 -- the variable exists when the value is not nil. Print (B) B = nilprint (B) print ("type:") print (type ('hello ')) print (type (10*3) print (type (1.0*2.3) print (type (print) print (type (true) print (type (NiL )) print (type (x) print ('string: ') page = [[# include <stdio. h> int main () {return 0 ;}] -- stores string Io. write (PAGE) print ('10' * 5) -- convert string to print (1 + 8) -- convert number to stringprint ('abc '.. 'de') -- Link string print ('enter a line and convert it to a number') line = Io. read () n = tonumber (line) if n = nil thenprint (line .. 'Cannot be converted to number') error (line .. 'cannot be converted to number') -- error will directly end running elseprint ('after conversion :'.. n) endprint (tostring (10) = '10') print (10 .. ''= '10 ')
Demo of the basic Lua type