RAD Studio
ContentsIndex
PreviousUpNext
The main() Function
Name 
Description 
Every C and C++ program must have a program-startup function.
  • Console-based programs call the main function at startup.
  • Windows GUI programs call the WinMain function at startup.
Where you place the startup function is a matter of preference. Some programmers place main at the beginning of the file, others at the end. Regardless of its location, the following points about main always apply.
  • Arguments to main
  • Wildcard arguments
  • Using -p (Pascal Calling Conventions)
  • The value that main() returns
 
Three parameters (arguments) are passed to main by the CodeGear C++Builder startup routine: argc, argv, and env.
  • argc, an integer, is the number of command-line arguments passed to main, including the name of the executable itself.
  • argv is an array of pointers to strings (char *[])argv[0] is the full path name of the program being run.argv[1] points to the first string typed on the operating system command line after the program name.argv[2] points to the second string typed after the program name.argv[argc-1] points to the last argument passed to main.argv[argc] contains... more 
BCC32.EXE is the command-line compiler. The default libraries used with this compiler are C0W32.OBJ (startup code), CW32.LIB (static single-threaded runtime library), and IMPORT32.LIB (import library for Win32). 
The dynamic-link library (DLL) versions of the runtime library are contained in the BIN subdirectory of your installation. These are listed below indicating whether they are multithreaded.
Directory: BIN  
Win32 programs can create more than one thread of execution. If your program creates multiple threads, and these threads also use the C++ runtime library, you must use the CW32MT.LIB or CW32MTI library instead.
The multithread libraries provide the following functions which you use to create threads:
  • _beginthread
  • _beginthreadex
  • _beginthreadNT
The multithread libraries also provide the following corresponding functions that terminate threads:
  • _endthread
  • _endthreadex
  • _threadid a global variable that contains the current identification number of the thread also known as the thread ID).
The header file stddef.h contains the declaration of _threadid.
When you compile or link a... more 
If your program uses the exec or spawn functions to create a new process, the new process will normally inherit all of the open file handles created by the original process. Some information, however, about these handles will be lost, including the access mode used to open the file. For example, if your program opens a file for read-only access in binary mode, and then spawns a child process, the child process might corrupt the file by writing to it, or by reading from it in text mode.
To allow child processes to inherit such information about open files, you... more 
Listed below is each of the C++Builder static library names and its use.
Directory of BCB\LIB (LIB files)  
The value returned by main is the status code of the program; it must be an int. If, however, your program uses the routine exit (or _exit) to terminate, the value returned by main is the argument passed to the call to exit (or to _exit).
For example, if your program contains the call  
If you compile your program using Pascal calling conventions, you must remember to explicitly declare main as a C type. Do this with the __cdecl keyword, like this:  
Command-line arguments containing wildcard characters can be expanded to all the matching file names, much the same way DOS expands wildcards when used with commands like COPY. All you have to do to get wildcard expansion is to link your program with the WILDARGS.OBJ object file, which is included with CodeGear C++.
Note: Wildcard arguments are used only in console-mode applications.
Once WILDARGS.OBJ is linked into your program code, you can send wildcard arguments (such as *.* ) to your main function. The argument will be expanded (in the argv array) to all files matching the wildcard mask. The maximum... more 
Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!