Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Force 4.0 Reference - main() main code entry point function http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
 main()              Main code entry point function
------------------------------------------------------------------------------
 Declaration
   force.cmh

 Syntax
   func int main extern
   param const char( 128 ) cCmdLine

 Arguments
   cCmdLine contains the command line arguments as the user entered
   when executing the program.

 Return
   The exit code of the program.

 Description
   The main() function is a user defined routine called back by the
   Force runtime system. Each Force application must contain exactly one
   main() function, which serves as the entry point to the user code
   and governs the execution of the whole application.

   The Force runtime system yields control to main() at startup time.
   Whenever a Force application starts running, the first routine after
   finit(), which is called at runtime is the main() function.
   From there other program routines can be called, which normally
   return back to the main() function (provided that none of the
   called routines contains a quit statement).

   If the main() function is omitted from a Force application, the linker
   reports that no stack segment was found for the .exe file. This means
   that the runtime environment necessary for running the application does
   not exist. Executing such an application generally results in a system
   crash.

   The main() function differs from other declared functions in
   its relatively loose typing. The cCmdLine parameter of main() is          #
   optional, but when declared, it must follow the type given above.
   The cCmdLine parameter then contains any arguments which were
   entered in the DOS command line within one string. This is different
   from the C programming language, where the parameters are normally placed
   in an array (the argv and argc variables of C are replaced in Force by the
   argv() and argc() functions, respectively).

   If the main() function is defined as returning an int value, its return
   value will be passed to the process that called the application
   (typically DOS). If main() is defined as a procedure then its exit
   code will be accidental.

   Earlier Force versions used the force_main() symbol instead of main().
   In Force 4.0 this symbol is automatically converted to main() by the
   compiler. Consequently, using force_main() elsewhere in the code results
   in an 'Undefined symbol' error


 Example
   #define EXAMPLE_SYSTEM
   #include example.hdr

   proc Test_main
   ? "Processing..."
   endproc
   
   func int main
   param const char( 128 ) cCmdLine
   vardef
      logical lDebug
      int     iExitCode
   enddef
   ? cCmdLine                           // display command line
   // set the lDebug flag, when the program is called from DOS like this:
   //   sample -d
   lDebug := "-D" $ upper( cCmdLine )
   if exist( "c:\autoexec.bat" )        // check for something important
      Test_main()                       // subroutine call
      iExitCode := 0                    // normal exit code
   else
      iExitCode := 1                    // exit code signalling a problem
   endif
   return( iExitCode )                  // DOS receives this value
   endfunc

See Also: argc() argv() fexit() finit() quit

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson