C语言编程中,经常遇到main函数中argc和argv[]这两个参数。argc是argument count的缩写,即参数的个数;argv是argument vector的缩写,即参数列表。argv[0]是程序本身的名字,argv[1]是在命令行中输入的第一个程序的参数,argv[argc]是NULL,如下所示:
#include "stdio.h" int main (int argc, char *argv[]) { printf ("the argc value is %d \n", argc); int i; for (i = 0; i <= argc; i++){ printf ("the argv[%d] value is %s \n", i, argv[i]); } return 0; } #将上述代码编译为test可执行文件,在命令行输入如下内容 /* ./test arg_1 arg_2 */ #执行结果如下: /* the argc value is 3 the argv[0] value is ./test_c_0 the argv[1] value is arg_1 the argv[2] value is arg_2 the argv[3] value is (null) */
#include "stdio.h" int main (int argc, char *argv[]) { FILE *fp; int c; fp = fopen( argv[1], "r"); while ( (c = fgetc(fp)) != EOF){ printf ("%c", c); } fclose(fp); return 0; }
原文地址:http://blog.csdn.net/vernice/article/details/46421245