源代码如下:
/*************************************************************************
*fileName: test.c
*description: test the scull.c
*author: Kevin
*create time: 2007-9-13
*modify info: -
*************************************************************************/
#include <stdio.h>
int main(int argc, char **argv)
{
int k;
for(k=0;k<=argc;k++)
{
printf("argv[%d]is %s/n",k,argv[k]);
}
printf("over/n");
return -1;
}
程序运行如下:
[root@localhost src]# ./test.out 1 2 3 "fas" 4
argv[0]is ./test.out
argv[1]is 1
argv[2]is 2
argv[3]is 3
argv[4]is fas
argv[5]is 4
argv[6]is (null)
over
加入了inp.c 的代码了 后为:
/*************************************************************************
*fileName: test.c
*description: test the scull.c
*author: Kevin
*create time: 2007-9-13
*modify info: -
*************************************************************************/
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv)
{
char *prgname;
int k,size;
size = 5;
prgname = argv[0];
printf("string prgname is %s/n",prgname);
switch (prgname[strlen(prgname)-1]) {
case 'w': size = 2; break;
case 'l': size = 4; break;
case 'b': case 'p': default:
size = 1;
}
for(k=0;k<=argc;k++)
{
printf("argv[%d]is %s/n",k,argv[k]);
}
printf("size is %d/n",size);
return -1;
}
运行结果为:
[root@localhost src]# ./test.out 1 w
string prgname is ./test.out
argv[0]is ./test.out
argv[1]is 1
argv[2]is w
argv[3]is (null)
size is 1
[root@localhost src]# ./test 1 w
string prgname is ./test
argv[0]is ./test
argv[1]is 1
argv[2]is w
argv[3]is (null)
size is 1
[root@localhost src]# ./tesw 1 w
string prgname is ./tesw
argv[0]is ./tesw
argv[1]is 1
argv[2]is w
argv[3]is (null)
size is 2
[root@localhost src]#
总结:
1 要改变size的值只需要程序运行的函数名改成相应的关键字即可。有两种实现方法,一是在gcc编译的时候改名字,二是编译好后直接重命名。
2 关于main函数的参数的使用。
/*************************************************************************
*fileName: test.c
*description: test the scull.c
*author: Kevin
*create time: 2007-9-13
*modify info: -
*************************************************************************/
#include <stdio.h>
int main(int argc, char **argv)
{
int k;
for(k=0;k<=argc;k++)
{
printf("argv[%d]is %s/n",k,argv[k]);
}
printf("over/n");
return -1;
}
程序运行如下:
[root@localhost src]# ./test.out 1 2 3 "fas" 4
argv[0]is ./test.out
argv[1]is 1
argv[2]is 2
argv[3]is 3
argv[4]is fas
argv[5]is 4
argv[6]is (null)
over
加入了inp.c 的代码了 后为:
/*************************************************************************
*fileName: test.c
*description: test the scull.c
*author: Kevin
*create time: 2007-9-13
*modify info: -
*************************************************************************/
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv)
{
char *prgname;
int k,size;
size = 5;
prgname = argv[0];
printf("string prgname is %s/n",prgname);
switch (prgname[strlen(prgname)-1]) {
case 'w': size = 2; break;
case 'l': size = 4; break;
case 'b': case 'p': default:
size = 1;
}
for(k=0;k<=argc;k++)
{
printf("argv[%d]is %s/n",k,argv[k]);
}
printf("size is %d/n",size);
return -1;
}
运行结果为:
[root@localhost src]# ./test.out 1 w
string prgname is ./test.out
argv[0]is ./test.out
argv[1]is 1
argv[2]is w
argv[3]is (null)
size is 1
[root@localhost src]# ./test 1 w
string prgname is ./test
argv[0]is ./test
argv[1]is 1
argv[2]is w
argv[3]is (null)
size is 1
[root@localhost src]# ./tesw 1 w
string prgname is ./tesw
argv[0]is ./tesw
argv[1]is 1
argv[2]is w
argv[3]is (null)
size is 2
[root@localhost src]#
总结:
1 要改变size的值只需要程序运行的函数名改成相应的关键字即可。有两种实现方法,一是在gcc编译的时候改名字,二是编译好后直接重命名。
2 关于main函数的参数的使用。
本文通过两个示例程序,详细解析了C语言中main函数的参数使用方式,并展示了如何根据程序名称的不同来调整程序的行为。
2100

被折叠的 条评论
为什么被折叠?



