Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1294090
  • 博文数量: 168
  • 博客积分: 2124
  • 博客等级: 大尉
  • 技术积分: 2590
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-16 23:51
文章分类

全部博文(168)

文章存档

2014年(6)

2013年(74)

2012年(71)

2011年(17)

分类: C/C++

2011-10-27 11:10:54

今天是看到 K&R C 的书上的例题,引发了对 int main(int argc,char* argv[])的学习
 
 
第一,回显程序命令行参数
 
在 d:\ 编写 程序,并命名为 c.c
 
 
#include
int main(int argc, char ** argv)
{
    int i;
    for (i=0; i < argc; i++)
        printf("Argument %d is %s.\n", i, argv[i]);
    return 0;
}
在附件中找到命令提示符  输入 (exe文件的路径)(当然不是在编译器输入了)
 
如图所示
 
 
 
 
看到得到的结果了么~?
 
啊?argc,argv是什么也明白了吧~
 
附一段 K&R C 原话
 
In environments that support C, there is a way to pass command-line arguments or parameters to a program when it begins executing. When main is called, it is called with two arguments. The first (conventionally called argc, for argument count) is the number of command-line (注意所在位置,是WIN命令提示符中,或LINUX命令行中)arguments the program was invoked with; the second (argv, for argument vector) is a pointer to an array of character strings that contain the arguments, one per string. We customarily use multiple levels of pointers to manipulate these character strings.
 
我们都知道main是一个函数,而函数就能被调用, 但是在我们初学的时候我们怎么也不会想到去调用他(因为往往语法(比如数组指针,指针数组就)把注意力全部吸引住了)~~现在明白了,是在命令行去调用他~~~
 
 
这么一来,反过头来看一下写这篇博文的出发点~~
 
<>5.10的两个函数,就是在编辑好以后通过系统调用~~~正是第一遍学习的时候没有理解“As a second example, let us make some enhancements to the pattern-finding program from Section 4.1. If you recall, we wired the search pattern deep into the program, an obviously unsatisfactory arrangement.”的意思~~“中文版翻译:我们将查找模式内置到程序中了”

(显然有略微失真,当然全书整体翻译还是很好的~~) 

 

 

阅读(14398) | 评论(0) | 转发(3) |
给主人留下些什么吧!~~