简单实现helloworld.c的命令行参数
/*command argument test */ #include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc,char *argv[]) { int i; for (i=0;i<argc;i++) { if((strcmp(argv[i],"hello")==0)) { printf("hello test done\n"); } if((strcmp(argv[i],"test")==0)) { printf("hello test 2\n"); } if((strcmp(argv[i],"--help")==0)||(strcmp(argv[i],"-h")==0)) { printf("usages: hello hello\n usages: hello test\n"); printf("usages: hello --help show this help info\n"); printf("usages: hello -h show this help info\n"); } } return 0; }
编译后运行,结果如下图所示:
laser杨万荣