Understanding Unix/Linux Programming note:chapter 1:执行可执行文件时的参数传递
2013-10-09 01:19
591 查看
1、在执行生成的可执行文件时,后面不跟任何参数,则默认传递给可执行文件的 参数个数是1,参数内容是:执行该可执行文件时的命令字符串。
2、在执行生成的可执行文件时,后面跟参数,则传递可执行文件的参数个数为:1+参数的个数。
如下例所示:
执行命令:./pre01
输出:
ac = 1
av[0] = ./pre01
执行命令:./pre01 pre01.c
输出:
ac = 2
av[0] = ./pre01
av[1] = pre01.c
2、在执行生成的可执行文件时,后面跟参数,则传递可执行文件的参数个数为:1+参数的个数。
如下例所示:
//to demonstrate the use of argument #include <stdio.h> int main(int ac, char *av[]) { printf("ac = %d\n", ac); for (int i = 0; NULL != av[i]; i++) { printf("av[%d] = %s\n", i, av[i]); } return 0; }
执行命令:./pre01
输出:
ac = 1
av[0] = ./pre01
执行命令:./pre01 pre01.c
输出:
ac = 2
av[0] = ./pre01
av[1] = pre01.c
相关文章推荐
- Understanding Unix/Linux Programming note:chapter 5:连接控制:学习stty
- Understanding Unix/Linux Programming note:chapter 1:more函数的流程图
- Understanding Unix/Linux Programming note:chapter 6:为用户编程:终端控制和信号
- Understanding Unix/Linux Programming note:chapter 2, chapter 3, chapter 4
- Understanding Unix/Linux Programming 笔记:chapter 15:进程间通信(IPC)
- Understanding Unix/Linux Programming 笔记:chapter 7:事件驱动编程:编写一个视频游戏
- Understanding Unix/Linux Programming 笔记:chapter 8:进程和程序:编写命令解释器sh
- Understanding Unix/Linux Programming 笔记:chapter 14:线程机制:并发函数的使用
- Understanding Unix/Linux Programming-设备文件与磁盘连接的概念
- Understanding Unix/Linux Programming-文件、设备和流
- Understanding Unix/Linux Programming 笔记:chapter 12:连接和协议:编写Web服务器
- Understanding Unix/Linux Programming 笔记:chapter 9:可编程的shell、shell变量和环境; chapter 10:I/O重定向和管道
- Understanding Unix/Linux Programming 笔记:chapter 13:基于数据报(Datagram)的编程:编写许可证服务器
- Understanding Unix/Linux Programming 笔记:chapter 11:连接到近端或远端的进程:服务器与Socket(套接字)
- UNIX/LINUX 平台可执行文件格式分析
- Understanding Unix/Linux Programming-信号与play_again4.c的准备知识
- UNIX/LINUX 平台可执行文件格式分析
- Understanding Unix/Linux Programming-用户程序play_again4.c
- UNIX/LINUX 平台可执行文件格式分析
- UNIX/LINUX 平台可执行文件格式分析