首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

命令行参数 有关问题

2012-12-17 
命令行参数 问题这一部分的内容没看明白.什么是命令行参数?有什么用?比方说 int main (char argc, char ar

命令行参数 问题
这一部分的内容没看明白.什么是命令行参数?有什么用?比方说 int main (char argc, char argv[])
这里面的两个参数有什么用? 去掉的话会有什么不良后果? 我平时写的小东西都没有这些参数的.求大神耐心指教.
[最优解释]
百度百科有详细说明,我就不复制来浪费资源了,你看看吧,你的答案都在这:http://baike.baidu.com/view/379148.htm
[其他解释]
了解命令行参数,顺便学学getopt(http://blog.csdn.net/cashey1991/article/details/7942809)的用法,非常有好处。
[其他解释]
去掉后没什么不良后果的、、
去看看关于命令行的知识吧、、
[其他解释]
建议楼主去linux下学习C,
./a.out arg1 arg2
试一下就知道了,,,
[其他解释]
这两个参数在不需要是可以不用,你完全可以定义为:


int main(void)
{
}

这两个参数argc和argv分别表示:命令行参数的个数和命令行字符串并以空格分开。比如,我们不加这两个参数是其实是:argc 为1, argv为./a.out(若生成的可执行文件时a.out),如果需要多个命令行参数时,就需要用到这两个参数。
比如:

int main(int argc, char *argv[])
{
    int i;
    for (i = 0; i < argc; i++)
    {
         fputs(argv[i],stdout);
    }
    return 0;
}

编译,然后执行:
./a.out num1 num2 num3
就会显示结果:
a.out
num1
num2
num3

[其他解释]
argc是参数的个数,默认是一个就是你程序的路径
argv是一个参数数组
[其他解释]
http://blog.csdn.net/cashey1991/article/details/7942809
[其他解释]
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 the command line. The last pointer (argv[argc]) is NULL

[其他解释]

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
 the command line. The last pointer (argv[argc]) is NULL

热点排行