您的位置:首页 > 大数据 > 人工智能

main函数的三个参数

2018-01-01 00:54 337 查看

函数原型:

int main( int argc, char *argv[], char *envp[] )

The main function marks the beginning and end of program execution. A C or C++ program must have one function named main. If your code adheres to the Unicode programming model, you can use the wide-character version of main, which is wmain.


函数参数:

argc

An integer specifying how many arguments are passed to the program from the command line. Because the program name is considered an argument, argc is at least 1.


argv

An array of null-terminated strings. It can be declared as an array of pointers to char (char *argv[ ] or wchar_t *argv[ ] for wmain) or as a pointer to pointers to char (char **argv or wchar_t **argv for wmain). The first string (argv[0]) is the program name, and each following string is an argument passed to the program from th
4000
e command line. The last pointer (argv[argc]) is NULL.


envp

A pointer to an array of environment strings. 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 end of the array is indicated by a NULL pointer. 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++.


三个参数:

argc表示有多少个命令行参数,第一个就是执行程序名,所以argc最少为1。

argv是具体的参数。

envp是系统的环境变量,很少有介绍的。“名称=值”的形式,以NULL结束。

其他:

*envp: 安符串数组。envp[] 的每一个元素都包含ENVVAR=value形式的字符串。其中ENVVAR为环境变量如PATH或87。value 为ENVVAR的对应值如C:\DOS, C:\TURBOC(对于PATH) 或YES(对于87)。

Turbo C2.0启动时总是把这三个参数传递给main()函数, 可以在用户程序中说明(或不说明)它们, 如果说明了部分(或全部)参数, 它们就成为main()子程序的局部变量。


Test Code:

int main( int argc, char *argv[], char *envp[] )
{
for ( int i = 0; i < argc; ++i )
{
printf( "%d : %s \n", i, argv[ i ] );
}

for( int i = 0; envp[i] != NULL; ++i )
{
printf( "%d : %s \n", i, envp[ i ] );
}
return 0;
}


< 部分摘自 CSDN>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐