命令行参数 问题
这一部分的内容没看明白.什么是命令行参数?有什么用?比方说 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)
{
}
int main(int argc, char *argv[])
{
int i;
for (i = 0; i < argc; i++)
{
fputs(argv[i],stdout);
}
return 0;
}
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