Microsoft C/C++ startup code uses the following rules when interpreting arguments given on the operating system command line:
- Arguments are delimited by white space, which is either a space or a tab.
- The caret character (^) is not recognized as an escape character or delimiter. The character is handled completely by the command-line parser in the operating system before being passed to the argv array in the program.
- A string surrounded by double quotation marks (“string“) is interpreted as a single argument, regardless of white space contained within. A quoted string can be embedded in an argument.
- A double quotation mark preceded by a backslash (“) is interpreted as a literal double quotation mark character (“).
- Backslashes are interpreted literally, unless they immediately precede a double quotation mark.
- If an even number of backslashes is followed by a double quotation mark, one backslash is placed in the argv array for every pair of backslashes, and the double quotation mark is interpreted as a string delimiter.
- If an odd number of backslashes is followed by a double quotation mark, one backslash is placed in the argv array for every pair of backslashes, and the double quotation mark is “escaped” by the remaining backslash, causing a literal double quotation mark (“) to be placed in argv.
The following program demonstrates how command-line arguments are passed:
// compile with: /EHsc
using namespace std;
int main( int argc, // Number of strings in array argv
char *argv, // Array of command-line argument strings
char *envp ) // Array of environment variable strings
// Display each command-line argument.
cout << "nCommand-line arguments:n";
for( count = 0; count < argc; count++ )
cout << " argv[" << count << "] "
<< argv[count] << "n";
Xem nguồn tại MSDN.