__argc、__argv、__wargv__argc, __argv, __wargv

__argc 全局变量是传递给程序的命令行参数的数量计数。The __argc global variable is a count of the number of command-line arguments passed to the program. __argv 是一个指向包含程序参数的单字节字符或多字节字符字符串的数组的指针,__wargv 是一个指向包含程序参数的宽字符字符串的数组的指针。__argv is a pointer to an array of single-byte-character or multi-byte-character strings that contain the program arguments, and __wargv is a pointer to an array of wide-character strings that contain the program arguments. 这些全局变量提供了 mainwmain 参数。These global variables provide the arguments to main or wmain.


extern int __argc;  
extern char ** __argv;  
extern wchar_t ** __wargv;  


在使用 main 函数的程序中,将在程序启动时通过用于启动该程序的命令行来初始化 __argc__argvIn a program that uses the main function, __argc and __argv are initialized at program startup by using the command line that's used to start the program. 会将命令行解析为单个自变量并展开通配符。The command line is parsed into individual arguments, and wildcards are expanded. 会将参数计数分配给 __argc 并在堆上分配参数字符串,以及将指向参数数组的指针分配给 __argvThe count of arguments is assigned to __argc and the argument strings are allocated on the heap, and a pointer to the array of arguments is assigned to __argv. 在编译为使用宽字符和 wmain 函数的程序中,将解析参数并将通配符展开为宽字符字符串,以及将指向参数字符串的数组的指针分配给 __wargvIn a program compiled to use wide characters and a wmain function, the arguments are parsed and wildcards are expanded as wide-character strings, and a pointer to the array of argument strings is assigned to __wargv.

对于可移植代码,我们建议你使用传递给 main 的参数来获取程序中的命令行参数。For portable code, we recommend you use the arguments passed to main to get the command-line arguments in your program.

一般文本例程映射Generic-Text Routine Mappings

Tchar.h 例程Tchar.h routine 未定义 _UNICODE_UNICODE not defined 已定义 _UNICODE_UNICODE defined
__targv __argv __wargv


全局变量Global variable 必需的标头Required header
__argc, __argv, __wargv__argc, __argv, __wargv <stdlib.h>、<cstdlib> (C++)<stdlib.h>, <cstdlib> (C++)

__argc__argv__wargv 是 Microsoft 扩展。__argc, __argv, and __wargv are Microsoft extensions. 有关兼容性信息,请参阅兼容性For compatibility information, see Compatibility.

请参阅See Also

全局变量 Global Variables
main:程序启动 main: Program Startup
使用 wmain 代替 mainUsing wmain Instead of main