分析 C 命令行自变量Parsing C Command-Line Arguments

Microsoft 专用Microsoft Specific

在解释操作系统命令行上给出的自变量时,Microsoft C 启动代码使用下列规则:Microsoft 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.

  • 无论其中是否包含空白,双引号括起来的字符串均被解释为单个参数。A string surrounded by double quotation marks is interpreted as a single argument, regardless of white space contained within. 带引号的字符串可以嵌入在自变量内。A quoted string can be embedded in an argument. 请注意,插入符号 (^) 未被识别为转义符或者分隔符。Note that the caret (^) is not recognized as an escape character or delimiter.

  • 前面有反斜杠的双引号 \" 被解释为原义双引号 (")。A double quotation mark preceded by a backslash, \", is interpreted as a literal double quotation mark (").

  • 反斜杠按其原义解释,除非它们紧位于双引号之前。Backslashes are interpreted literally, unless they immediately precede a double quotation mark.

  • 如果偶数个反斜杠后跟双引号,则每对反斜杠 (\\) 中有一个反斜杠 (\) 被置于 argv 数组中,而双引号 (") 被解释为字符串分隔符。If an even number of backslashes is followed by a double quotation mark, then one backslash (\) is placed in the argv array for every pair of backslashes (\\), and the double quotation mark (") is interpreted as a string delimiter.

  • 如果奇数个反斜杠后跟双引号,则每对反斜杠 (\\) 中有一个反斜杠 (\) 被置于 ) is placed in the argv 数组中,而通过剩余的反斜杠,双引号被解释为反义序列,从而使原义双引号 (") 放置在 argv 中。If an odd number of backslashes is followed by a double quotation mark, then one backslash (\) is placed in the argv array for every pair of backslashes (\\) and the double quotation mark is interpreted as an escape sequence by the remaining backslash, causing a literal double quotation mark (") to be placed in argv.

    此列表通过显示命令行参数的多个示例的传递到 argv 的解释结果来阐释上述规则。This list illustrates the rules above by showing the interpreted result passed to argv for several examples of command-line arguments. 在第二列、第三列和第四列中列出的输出来自于遵循列表的 ARGS.C 程序。The output listed in the second, third, and fourth columns is from the ARGS.C program that follows the list.

命令行输入Command-Line Input argv[1]argv[1] argv[2]argv[2] argv[3]argv[3]
"a b c" d e a b c d e
"ab\"c" "\\" d ab"c \ d
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\\b c d e

示例Example

代码Code

// Parsing_C_Commandline_args.c  
// ARGS.C illustrates the following variables used for accessing  
// command-line arguments and environment variables:  
// argc  argv  envp  
//  

#include <stdio.h>  

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  
{  
    int count;  

    // Display each command-line argument.  
    printf_s( "\nCommand-line arguments:\n" );  
    for( count = 0; count < argc; count++ )  
        printf_s( "  argv[%d]   %s\n", count, argv[count] );  

    // Display each environment variable.  
    printf_s( "\nEnvironment variables:\n" );  
    while( *envp != NULL )  
        printf_s( "  %s\n", *(envp++) );  

    return;  
}  

注释Comments

此程序中输出的一个示例是:One example of output from this program is:

Command-line arguments:  
  argv[0]   C:\MSC\TEST.EXE  

Environment variables:  
  COMSPEC=C:\NT\SYSTEM32\CMD.EXE  

  PATH=c:\nt;c:\binb;c:\binr;c:\nt\system32;c:\word;c:\help;c:\msc;c:\;  
  PROMPT=[$p]   
  TEMP=c:\tmp  
  TMP=c:\tmp  
  EDITORS=c:\binr  
  WINDIR=c:\nt        

结束 Microsoft 专用END Microsoft Specific

另请参阅See Also

main 函数和程序执行main Function and Program Execution