- Article
- 8 minutes to read
All C++ programs must have aprincipally
Occupation. If you are trying to run a C++ program without aprincipally
function, the compiler throws an error. (Dynamic link libraries and static libraries have noprincipally
function.) Theprincipally
The function is where your source code begins execution, but before any program enters theprincipally
The function will zero out all static class members without explicit initializers. In Microsoft C++, global static objects are also initialized before enteringprincipally
. Various restrictions apply to themprincipally
Function that does not apply to any other C++ function. Theprincipally
Occupation:
- It cannot be overloaded (cf.function overload).
- cannot be declared as
in line
. - cannot be declared as
static
. - The address cannot be accepted.
- It cannot be called your program.
Dieprincipally
function signature
Dieprincipally
The function has no declaration because it is built into the language. If so, the declaration syntax forprincipally
would be like this:
int main();int main(int argc, char *argv[]);
If no return value is specified inprincipally
, the compiler will return a value of zero.
Default command line arguments
The arguments forprincipally
allow for convenient parsing of command-line arguments. the guys stopargc
eargv
are defined by the language. The namesargc
eargv
are traditional, but you can name them whatever you like.
Argument definitions are as follows:
argc
An integer containing the count of the following argumentsargv. DieargcThe parameter is always greater than or equal to 1.
argv
An array of null-terminated strings representing command-line arguments entered by the user of the program. Agreement,arg[0]
is the command with which the program is invoked.argument[1]
is the first command line argument. The last command line argument isargv[argc - 1]
, earg[argc]
is always NULL.
For information on how to suppress command line processing, seeCustomize C++ command line processing.
Use
Agreement,arg[0]
is the name of the program file. However, on Windows it is possible to create a processCreateProcess. If you use the first and second arguments (lpApplicationName
elpCommandLine
),arg[0]
it must not be the name of the executable file. You can useGetModuleFileNameto get the name of the executable and its fully qualified path.
Microsoft specific extensions
The following sections describe Microsoft-specific behavior.
Diewmain
function and_tmain
macro
If you build your source code to use Unicode wide characters, you can use Microsoft specific codewmain
Entrypoint, which is the wide-character version ofprincipally
. Here is the effective declaration syntax forwmain
:
int wmain();int wmain(int argc, wchar_t *argv[]);
You can also use Microsoft specific_tmain
, which is a preprocessor macro defined intchar.h
._tmain
decideprincipally
unless_UNICODE
It's defined. In that case,_tmain
decidewmain
. Die_tmain
Macro and other macros that start with_t
are useful for code that needs to create separate versions for narrow and wide character sets. For more information, seeUsing generic text mappings.
ReturnsEmpty
do principal
As an extension of Microsoft, theprincipally
ewmain
Functions can be declared as returningEmpty
(no return value). This extension is also available in some other compilers, but its use is not recommended. It is available for symmetry ifprincipally
does not return any value.
if you explainprincipally
orwmain
as returnEmpty
, you cannot return an exit code to the parent process or the operating system using aReturnsExpression. How to return an exit code ifprincipally
orwmain
is declared asEmpty
, you must use theexitOccupation.
Dieenvp
command line argument
Dieprincipally
orwmain
Signatures allow an optional Microsoft-specific extension to access environment variables. This extension is also common in other compilers for Windows and UNIX systems. The nameenvp
it's traditional, but you can name the environment parameter whatever you like. Here are the effective declarations for the argument lists that include the environment parameter:
int main(int argc, char* argv[], char* envp[]);int wmain(int argc, wchar_t* argv[], wchar_t* envp[]);
envp
The voteenvp
Parameter is an array of strings representing variables defined in the user's environment. This array is terminated by a NULL input. Can be declared as an array of pointersCharacters
(char *envp[]
) or as a pointer to pointer toCharacters
(char **envp
). If your program useswmain
rather thanprincipally
, Use owchar_t
data type insteadCharacters
.
The environment block to pass toprincipally
ewmain
it is a "frozen" copy of the current environment. If you later change the environment by callingputenv
or_wputenv
, the current environment (as returned byTenv
or_wgetenv
it's at_environment
or_wenviron
variable) changes, but the block pointed to byenvp
will not change. For more information on how to suppress environment processing, seeCustomize C++ command line processing. Dieenvp
argument is compliant with the C89 standard, but not C++ standards.
examples of arguments tooprincipally
The following example shows how to use theargc
,argv
, eenvp
arguments forprincipally
:
// argument_definitions.cpp// compile with: /EHsc#include <iostream>#include <string.h>using namespace std;int main( int argc, char *argv[], char *envp[] ){ bool numberLines = INCORRECT; // Default is no line number. // If /n is passed to the .exe, show a numbered list // of the environment variables. if ( (argc == 2) && _stricmp( argv[1], "/n" ) == 0 ) numberLines = true; // Loop through the list of strings until a NULL is encountered. for ( int i = 0; envp[i] != NULL; ++i ) { if ( numberLines ) cout << i << ": "; // Append numbers if /n specified cout << envp[i] << "\n"; }}
Parsing C++ Command Line Arguments
The command line parsing rules used by Microsoft C/C++ code are Microsoft specific. The runtime initialization code uses these rules when interpreting the arguments given on the operating system command line:
Arguments are separated by spaces, which are either a space or a tab.
The first argument (
arg[0]
) receives special treatment. Represents the name of the program. As a valid path name must be, the parts are enclosed in double quotes ("
) They are allowed. Double quotes are not included in thearg[0]
Exit. Quoted parts prevent a space or tab character from being interpreted as the end of the argument. Later rules in this list do not apply.A string enclosed in double quotes is interpreted as a single argument, which can contain spaces. A quoted string can be embedded in an argument. The circumflex accent (
^
) is not recognized as an escape or separator character. Within a quoted string, a pair of double quotes is interpreted as a single escaped double quote. If the command line ends before a closing double quote is encountered, all characters read so far are printed as the last argument.A double quote preceded by a backslash (
\"
) is interpreted as literal double quotes ("
).Backslashes are interpreted literally unless they immediately precede a double quote.
If an even number of backslashes are followed by double quotes, then a backslash (
\
) is placed in theargv
Array for each pair of backslashes (\\
) and double quotes ("
) is interpreted as a string delimiter.If an odd number of backslashes are followed by double quotes, then a backslash (
\
) is placed in theargv
Array for each pair of backslashes (\\
). The double quote is interpreted as an escape sequence by the remaining backslash, resulting in a literal double quote ("
) insertargv
.
Example parsing command-line arguments
The following program demonstrates how to pass command line arguments:
// command_line_arguments.cpp// compile with: /EHsc#include <iostream>using namespace std;int main( int argc, // number of strings in argv array char *argv[], // array of command line arguments strings char * envp[] ) // array of environment variables strings{ int count; // Displays each command line argument. cout << "\nCommand line arguments:\n"; for( count = 0; count < argc; count++ ) cout << " argv[" << count << "] " << argv[count] << "\n";}
Command line analysis results
The following table shows example inputs and expected outputs that illustrate the rules in the previous list.
command line input | argument[1] | argument[2] | argument[3] |
---|---|---|---|
"abc" d.h | abc | d | e |
a\\b d"e f"g h | a\\b | de fg | h |
A B C D | a\"b | c | d |
a\\\\"b c" d e | a\\bc | d | e |
A B C D | A B C D |
wildcard extension
Microsoft compiler allows you to optionally use itplaceholdercharacter, the question mark (?
) and asterisk (*
) to specify the file name and path arguments on the command line.
Command line arguments are processed by an internal routine in the runtime initialization code, which by default does not expand wildcards into separate stringsargv
string array. You can enable wildcard expansion by including thesetargv.obj
Archive (wsetargv.obj
file forwmain
) was behind/Shortcut
compiler options or theirSHORTCUT
Command Line.
For more information about runtime initialization linker options, seelink options.
Customize C++ command line processing
If your program does not accept command-line arguments, you can suppress the command-line handler to save space. To suppress its use, include thenorg.obj
file (for bothprincipally
ewmain
) was behind/Shortcut
compiler options or theirSHORTCUT
Command Line.
Likewise, if you never access through the environment tableenvp
argument, you can suppress the inner environment processing routine. To suppress its use, include thenoenv.obj
file (for bothprincipally
ewmain
) was behind/Shortcut
compiler options or theirSHORTCUT
Command Line.
Your program can call thegenerate
orexecutive
Family of routines in the C runtime library. If this is the case, you should not suppress the environment handler as it is used to pass an environment from the parent process to the child process.
See too
basic concept