C语言程序设计 第一章C语言概述.3

Weaknesses

C's weaknesses arise from the same source as many of its strengths: C's closeness to the machine. Here are a few of C's most notorious problems:

C语言的缺点和它的某些优点本是同根生,均来自C语言与机器的紧密性.以下是其存在的一些问题。


 C programs can be error-prone. C's flexibility makes it an error-prone lan-guage. Programming mistakes that would be caught in many other languages can't be detected by a C compiler. In this respect. C is a lot like assembly language, where most errors aren't detected until the program is run. To make matters worse, C contains a number of pitfalls for the unwary. In later chapters. we'll see how an extra semicolon can create an infinite loop or a missing & symbol can cause a program crash.

C程序可能会漏洞百出。c语言的灵活性使得它成为一种会漏洞百出的语言。许多其他语言可以发现的编程错误,c语言编译器却无法检查到。从这方面来说,c语言与汇编语言极为相似,因为直到程序运行时汇编语言才能检查到大多数错误。更糟的是,c语言还包含大量不易觉察的隐患。在后续的几章中,我们将会看到,一个额外的分号可能会导致无限循环,再或者一个遗漏的&可能会引发程序崩溃。


 C programs can be difficult to understand. Although C is a small language by most measures. it has a number of features that aren't found in all programming languages (and that consequently are often misunderstood). These features can be combined in a great variety of ways. many of which-although obvious to the original author of a program-can be hard for others to understand. Another problem is the terse nature of' C programs. C was designed at a time when interactive communication with computers was tedious at best. As a result, C was purposefully kept terse to minimize the time required to enter and edit programs. C's flexibility can also be a negative factor; programmers who are too clever for their own good can make programs almost impossible to understand.

C程序可能会难以理解。虽然根据大多数衡量标准c语言是一种小型语言,但是它也有许多其他通用语言没有的特性(并且常常被误解)。这些特性可以用多种方式结合使用,其中的一些结合尽管编程者心知肚明,怛是其他人恐怕难以理解。另一个问题就是c程序简明扼要的特性。c语言产生的时候正是人机交互最为单调乏味的时期。由此产生的后果是c语言为了保持简明将录入和编辑程序的用时减到最少。c语言的灵活性也可能是一个负面因素,某些程序员实在太高明了,甚至可以编写出除了他们自己几乎没人可以读得懂的程序。


 C programs can be difficult to modify. Large programs written in C can be hard to change if they haven't been designed with maintenance in mind. Modern programming languages usually provide features such as classes and packages that support the division of a large program into more manageable pieces. C, unfortunately, lacks such features.

C程序可能会难以修改。用c语言编写大规模程序时,如果在设计中没有考虑到维护的问题,那么将很难修改。现代的编程语言通常都会提供一种称为“模块”(“单元”或者“包”)的语言特性,这一特性可以把一个大规模的程序分解成许多可管理的块。遗憾的是,c语言恰恰缺少这样的特性。


                                Obfuscated C

Even C's most ardent admirers admit that C code can be hard to read. The annual International Obfuscated C Code Contest actually encourages contestants to write the most confusing C programs possible. The winners are truly baffling, as 1990's"Best Small Program" shows:

即使是那些最热爱C语言的人也不得不承认c代码难以阅读。每年l旋的国际模糊c陀码大赛(Intemational Obfuscated C Code Contest)竟然鼓励参赛者编写最难以理解的c程序。获莫作品实在让人感觉莫名其妙。例如,1991年的“最佳小程序”如下

  

This program, written by Doron Osovlanski and Baruch Nissenbaum, prints all solutions to the Eight Queens problem (the problem of placing eight queens on a chessboard in such a way that no queen attacks any other queen). In fact, it works for any number of queens between four and 99. For more winning programs, visit www.ioccc.org, the contest's web site.

这个程序是由Doron Osovlanski和Baruch Nissenbaum共同编写的,内容是要打印出八皇后问题的全部解决方案。(此问题要求在一个棋盘上放置8个皇后,并且要求皇后之间不会出现相互“攻击”的现象。)事实上,此程序可用于求解皇后数量在4 99范围内的全部问题。其他获奖程序可以在Don Libes编写的ObfuscatedC and Other Mysteries-书中找到。


Effective Use of C

Using C effectively requires taking advantage of C's strengths while avoiding its weaknesses. Here are a few suggestions:

高效地使用C语言要求在利用C语言优点的同时要避免它的缺点。以下是一些建议


 Learn how to avoid C pitfalls. Hints for avoiding pitfdlls are scattered throughout this book-just look for the△ symbol. For a more extensive list of pitfalls.see Andrew Koenig's C Traps and Pitfalls (Reading, Mass.: Addison-Wesley.1989). Modern compilers will detect common pitfalls and issue wamings. but no compiler spots them all.

学习如何规避C语言的缺陷。规避缺陷的提示遍布全书,只要寻找到△符号。如果想看到更详尽的缺陷列表,可以参考Andrew Koenig的《C陷阱与缺陷》④一书。现代编译器将可以检查到常见的缺陷并且发出警告,但是没有一个编译器可以侦察出全部缺陷。


 Use software tools to make programs more reliable. C programmers are prolific tool builders (and users). One of the most famous C tools is named lint.lint,which is traditionally provided with UNIX. can subject a program to a more extensive error analysis than most C compilers.If lint (or a similar program) is available, it's a good idea to use it. Another useful tool is a debugger. Because of the nature of C, many bugs can't be detected by a C compiler;these show up instead in the form of run-time errors or incorrect output. Consequently. using a good debugger is practically mandatory for C programmers.

使用软件工具使程序更加可靠。C程序员是众多软件工具的制造者(和使用者)。lint是其中一个最著名的c语言工具。lint(传统上由UNIX系统提供)与大多数c语言编译器相比,可以对程序进行更加广泛的错误分析。如果可以得到lint(或某个类似程序),那么使用它应该是个好主意。另一个有益的工具则是调试工具。由于C语言的本性,许多错误无法被C语言编译器查出。这些错误会以运行时错误或不正确输出的形式表现出来。因此,在实践中C程序员都必须能够很好地使用调试工具


 Take advantage of existing code libraries. One of the benefits of using C is that so many other people also use it; it's a good bet that they've written code you can employ in your own programs. C code is often bundled into libraries(collections of functions); obtaining a suitable library is a good way to reduce errors-and save considerable programming effort. Libraries for common tasks, including user-interface development. graphics. communications. database management, and networking. are readily available. Some libraries are in the public domain. some are open source, and some are sold commercially.

利用现有的代码库。大家都在使用C是一种好处。把别人编写好的代码用于自己的程序是一个非常好的主意。C代码经常被打包进库(函数集合)。获取适当的库既可以大大减少错误,也可以节省相当多的编程工作,用于常见任务的库很容易获得,常见任务包括用户界面开发、图形学、通信、数据库管理以及网络等。有些库是公用的,而有些则是作为商品销售的。


Adopt a sensible set of coding conventions. A coding convention is a style rule that a programmer has decided to adopt even though it's not enforced by the language. Well-chosen conventions help make programs more uniform.easier to read. and easier to modify. Conventions are important when using any programming language. but especially so with C. As noted above. C's highly flexible nature makes it possible for programmers to write code that is all but unreadable. The programming examples in this book follow one set of conventions, but there are other. equally valid. conventions in use. (We'll discuss some of the altenatives from time to time.) Which set you use is less important than adopting some conventions and sticking to them.

采用一套切合实际的编码规范。编码规范是一套设计准则,即使语言本身没有强制要求,程序员也必须遵守。适当的规范可以使程序更加统一,并且易于阅读和修改。使用任何一种编程语言都需要规范,尤其是c语言。正如前面所说的,c语言本身具有较高的灵活习性,这使得程序员编写的代码可能会难以理解。本书的编程示例遵循一套编码规范,但是,还有另外一些同样有效的规范可以使用。(本书将穿插讨论一些可供选择的方法。)选用哪种并不是最重要的,重要的是必须采纳某些规范并且坚持使用它们。


Avoid "tricks" and overly complex code. C encourages programming tricks. There are usually several ways to accomplish a given task in C; programmers are often tempted to choose the method that's most concise. Don't get carried away; the shortest solution is often the hardest to comprehend. In this book.I'll illustrate a style that's reasonably concise but still understandable.

避免“投机取巧”和极度复杂的代码。C语言鼓励使用编程技巧。通常用C语言完成某项指定任务时会有多种解决途径,程序员经常会尝试选择最简洁的方式。但是,千万不要没有节制,因为最简略的解决方式往往也是最难以理解的。书中将举例说明什么是简洁合理且易于理解的方式。


 Stick to the standard. Most C compilers provide language features and library functions that aren't part of the C89 or C99 standards. For portability, it's best to avoid using nonstandard features and libraries unless they're absolutely necessary.

使用标准C,少用经典C。大多数C语吉编译器都提供不属于标准C内容的特性和库函数。除非确有必要,否则最好尽量避免使用这些特性和库函数。

上一章C语言程序设计 第一章C语言概述.2

prominent 卓越的

inherent n.固着的,固有的

inherit vt. 继承;遗传而得

notorious adj. 臭名昭著的, 声名狼藉的

terse adj. 简洁的,精练的,扼要的

tedious adj. 乏味的, 单调的

obfuscate vt. 使模糊,使混乱

baffling 令人困惑的

scattered adj. 分散的;稀稀拉拉的

conventions 常规


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值