The code snippets used in this module if any are Visual C++ .Net 2003 dominated and if compiled using Visual C++ .Net 2005 you need to use the /clr:oldSyntax option). You may want to compare the native C++ main() and command line argument that can be found at C/C++ command line arguments page. The following are the topics available in this module.
The C++ main() or Program Startup
A special function called main() is the starting point of execution for all C and C++ programs. If you are writing code that adheres to the Unicode programming model, you can use the wide-character version of main(), wmain(). The main() function is not predefined by the compiler; rather, it must be supplied in the program text. The declaration syntax for main is:
The following is Microsoft Specific. The declaration syntax for wmain() is as follows:
You can also use _tmain(), which is defined in TCHAR.h. _tmain() will resolve to main unless _UNICODE is defined, in which case _tmain() will resolve to wmain(). The types for argc and argv are defined by the language. The names argc, argv, and envp are traditional, but are not required by the compiler. Alternately, the main() and wmain() functions can be declared as returning void (no return value). If you declare main() or wmain() as returning void, you cannot return an exit code to the parent process or operating system using a return statement; to return an exit code when main() or wmain() is declared as void, you must use the exit() function.
The Argument Definitions
The arguments in the prototype are shown below.
This allows convenient command-line parsing of arguments and, optionally, access to environment variables. The argument definitions are as follows:
An integer that contains the count of arguments that follow in argv. The argc parameter is always greater than or equal to 1.
An array of null-terminated strings representing command-line arguments entered by the user of the program. By convention, argv is the command with which the program is invoked (program name), argv is the first command-line argument, and so on, until argv[argc], which is always NULL. The first command-line argument is always argv and the last one is argv[argc – 1].
The envp array, which is a common extension in many UNIX® systems, is used in Microsoft C++. It is an array of strings representing the variables set in the user's environment. This array is terminated by a NULL entry. It can be declared as an array of pointers to char (char *envp[ ]) or as a pointer to pointers to char (char **envp). If your program uses wmain() instead of main(), use the wchar_t data type instead of char. The environment block passed to main() and wmain() is a "frozen" copy of the current environment. If you subsequently change the environment via a call to putenv or _wputenv, the current environment (as returned by getenv()/_wgetenv() and the _environ/ _wenviron variable) will change, but the block pointed to by envp will not change. This argument is ANSI compatible in C, but not in C++.
By convention, argv is the command with which the program is invoked. However, it is possible to spawn a process using CreateProcess() and if you use both the first and second arguments (lpApplicationName and lpCommandLine), argv may not be the executable name; use GetModuleFileName() to retrieve the executable name, and its fully-qualified path. The following example shows how to use the argc, argv, and envp arguments to main():
// command line argument
// compile with: /EHsc
using namespace std;
int main(int argc, char *argv, char *envp)
int iNumberLines = 0; // Default is no line numbers.
// If /n is passed to the .exe, display numbered listing
// of environment variables.
if ((argc == 2) && _stricmp(argv, "/n" ) == 0)
iNumberLines = 1;
// Walk through list of strings until a NULL is encountered.
for(int i = 0; envp[i] != NULL; ++i )
cout<<i<<": " <<envp[i]<<"\n";
The following are the sample outputs (run at command prompt):
F:\vc2005project\cplus\debug>cplus test some argument
F:\vc2005project\cplus\debug>cplus test argument
0: ALLUSERSPROFILE=C:\Documents and Settings\All Users.WINDOWS
1: APPDATA=C:\Documents and Settings\Johnny\Application Data
3: CommonProgramFiles=C:\Program Files\Common Files
8: HOMEPATH=\Documents and Settings\Johnny
9: INCLUDE=c:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\include\
10: LIB=c:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\Lib\
14: Path=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\Common Files\Adobe\AGL;C:\Pr
ogram Files\Microsoft SQL Server\90\Tools\binn\
17: PROCESSOR_IDENTIFIER=x86 Family 15 Model 3 Stepping 4, GenuineIntel
20: ProgramFiles=C:\Program Files
29: USERPROFILE=C:\Documents and Settings\Johnny
30: VS71COMNTOOLS=c:\Program Files\Microsoft Visual Studio .NET 2003\Common7\Tools\
31: VS80COMNTOOLS=C:\Program Files\Microsoft Visual Studio 8\Common7\Tools\
Customizing C++ Command-Line Processing
This information is Microsoft Specific. If your program does not take command-line arguments, you can save a small amount of space by suppressing use of the library routine that performs command-line processing. This routine is called _setargv(). To suppress its use, define a routine that does nothing in the file containing the main() function, and name it _setargv(). The call to _setargv() is then satisfied by your definition of _setargv(), and the library version is not loaded. Similarly, if you never access the environment table through the envp argument, you can provide your own empty routine to be used in place of _setenvp(), the environment-processing routine. Just as with the _setargv() function, _setenvp() must be declared as extern "C". Your program might make calls to the spawn or exec family of routines in the C run-time library. If this is the case, you should not suppress the environment-processing routine, since this routine is used to pass an environment from the parent process to the child process. You can use wildcards, the question mark (?) and asterisk (*), to specify filename and path arguments on the command line. By default, _setargv() expands wildcards into separate strings in the argv string array. If no matches are found for the wildcard argument, the argument is passed literally.