题目
编写一个程序,按照相反的单词顺序显示命令行参数。即,如果命令行参数是see you later,程序的现实应该为later you see。
分析
这里熟悉命令行参数的用法和排列规则。
对于我使用的mac OS来说,命令行的使用方法为:
首先cd命令打开文件夹,然后用cc命令编译.c文件得到a.out文件,然后执行a.out
命令为:
./a.out see you later
命令行参数arg[0]为./a.out
arg[1]为 see
以此类推
程序
#include <stdio.h>
int main(int argc, const char * argv[]) {
int count;
printf("The command line has %d arguments:\n",argc-1);
for(count=argc-1;count>=0;count--){
printf("%d:%s ",count,argv[count]);
}
printf("\n");
return 0;
}