没有合适的资源?快使用搜索试试~ 我知道了~
首页Expert C Programming.pdf
资源详情
资源评论
资源推荐
Expert C Programming: Deep C Secrets
By Peter van der Linden
Introduction
C code. C code run. Run code run…please!
—Barbara Ling
All C programs do the same thing: look at a character and do nothing with it.
—Peter Weinberger
Have you ever noticed that there are plenty of C books with suggestive names like C Traps and
Pitfalls, or The C Puzzle Book, or Obfuscated C and Other Mysteries, but other programming
languages don't have books like that? There's a very good reason for this!
C programming is a craft that takes years to perfect. A reasonably sharp person can learn the basics of
C quite quickly. But it takes much longer to master the nuances of the language and to write enough
programs, and enough different programs, to become an expert. In natural language terms, this is the
difference between being able to order a cup of coffee in Paris, and (on the Metro) being able to tell a
native Parisienne where to get off. This book is an advanced text on the ANSI C programming
language. It is intended for people who are already writing C programs, and who want to quickly pick
up some of the insights and techniques of experts.
Expert programmers build up a tool kit of techniques over the years; a grab-bag of idioms, code
fragments, and deft skills. These are acquired slowly over time, learned from looking over the
shoulders of more experienced colleagues, either directly or while maintaining code written by others.
Other lessons in C are self-taught. Almost every beginning C programmer independently rediscovers
the mistake of writing:
if (i=3)
instead of:
if (i==3)
Once experienced, this painful error (doing an assignment where comparison was intended) is rarely
repeated. Some programmers have developed the habit of writing the literal first, like this:
if
(3==i)
. Then, if an equal sign is accidentally left out, the compiler will complain about an
"attempted assignment to literal." This won't protect you when comparing two variables, but every
little bit helps.
The $20 Million Bug
In Spring 1993, in the Operating System development group at SunSoft, we had a "priority one" bug
report come in describing a problem in the asynchronous I/O library. The bug was holding up the sale
of $20 million worth of hardware to a customer who specifically needed the library functionality, so
we were extremely motivated to find it. After some intensive debugging sessions, the problem was
finally traced to a statement that read :
x==2;
It was a typo for what was intended to be an assignment statement. The programmer 's finger had
bounced on the "equals" key, accidentally pressing it twice instead of once. The statement as written
compared x to 2, generated true or false, and discarded the result .
C is enough of an expression language that the compiler did not complain about a statement which
evaluated an expression, had no side-effects, and simply threw away the result. We didn't know
whether to bless our good fortune at locating the problem, or cry with frustration at such a common
typing error causing such an expensive problem. Some versions of the lint program would have
detected this problem, but it's all too easy to avoid the automatic use of this essential tool.
This book gathers together many other salutary stories. It records the wisdom of many experienced
programmers, to save the reader from having to rediscover everything independently. It acts as a guide
for territory that, while broadly familiar, still has some unexplored corners. There are extended
discussions of major topics like declarations and arrays/pointers, along with a great many hints and
mnemonics. The terminology of ANSI C is used throughout, along with translations into ordinary
English where needed.
Programming Challenge
OR
Handy Heuristic
Sample Box
Along the way, we have Programming Challenges outlined in boxes like this one.
These are suggestions for programs that you should write.
There are also Handy Heuristics in boxes of their own.
These are ideas, rules-of-thumb, or guidelines that work in practice. You can adopt them as
your own. Or you can ignore them if you already have your own guidelines that you like
better.
Convention
One convention that we have is to use the names of fruits and vegetables for variables (only in small
code fragments, not in any real program, of course):
char pear[40];
double peach;
int mango = 13;
long melon = 2001;
This makes it easy to tell what's a C reserved word, and what's a name the programmer supplied.
Some people say that you can't compare apples and oranges, but why not—they are both hand-held
round edible things that grow on trees. Once you get used to it, the fruit loops really seem to help.
There is one other convention—sometimes we repeat a key point to emphasize it. In addition, we
sometimes repeat a key point to emphasize it.
Like a gourmet recipe book, Expert C Programming has a collection of tasty morsels ready for the
reader to sample. Each chapter is divided into related but self-contained sections; it's equally easy to
read the book serially from start to finish, or to dip into it at random and review an individual topic at
length. The technical details are sprinkled with many true stories of how C programming works in
practice. Humor is an important technique for mastering new material, so each chapter ends with a
"light relief" section containing an amusing C story or piece of software folklore to give the reader a
change of pace.
Readers can use this book as a source of ideas, as a collection of C tips and idioms, or simply to learn
more about ANSI C, from an experienced compiler writer. In sum, this book has a collection of useful
ideas to help you master the fine art of ANSI C. It gathers all the information, hints, and guidelines
together in one place and presents them for your enjoyment. So grab the back of an envelope, pull out
your lucky coding pencil, settle back at a comfy terminal, and let the fun begin!
Some Light Relief—Tuning File Systems
Some aspects of C and UNIX are occasionally quite lighthearted. There's nothing wrong with well-
placed whimsy. The IBM/Motorola/Apple PowerPC architecture has an E.I.E.I.O. instruction
[1]
that
stands for "Enforce In-order Execution of I/O". In a similar spirit, there is a UNIX command,
tunefs, that sophisticated system administrators use to change the dynamic parameters of a
filesystem and improve the block layout on disk.
[1]
Probably designed by some old farmer named McDonald.
The on-line manual pages of the original tunefs, like all Berkeley commands, ended with a "Bugs"
section. In this case, it read:
Bugs:
This program should work on mounted and active file systems,
but it doesn't. Because the superblock is not kept in the
buffer cache, the program will only take effect if it is run
on dismounted file systems; if run on the root file system,
the system must be rebooted. You can tune a file system, but
you can't tune a fish.
Even better, the word-processor source had a comment in it, threatening anyone who removed that last
phrase! It said:
Take this out and a UNIX Demon will dog your steps from now
until the time_t's wrap around.
When Sun, along with the rest of the world, changed to SVr4 UNIX, we lost this gem. The SVr4
manpages don't have a "Bugs" section—they renamed it "Notes" (does that fool anyone?). The "tuna
fish" phrase disappeared, and the guilty party is probably being dogged by a UNIX demon to this day.
Preferably lpd.
Programming Challenge
Computer Dating
When will the
time_t's wrap around?
Write a program to find out.
1. Look at the definition of
time_t. This is in file /usr/include/time.h.
2. Code a program to place the highest value into a variable of type
time_t, then
pass it to
ctime() to convert it into an ASCII string. Print the string. Note that
ctime has nothing to do with the language C, it just means "convert time."
剩余289页未读,继续阅读
yang_soros
- 粉丝: 1
- 资源: 13
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf
- 建筑供配电系统相关课件.pptx
- 企业管理规章制度及管理模式.doc
- vb打开摄像头.doc
- 云计算-可信计算中认证协议改进方案.pdf
- [详细完整版]单片机编程4.ppt
- c语言常用算法.pdf
- c++经典程序代码大全.pdf
- 单片机数字时钟资料.doc
- 11项目管理前沿1.0.pptx
- 基于ssm的“魅力”繁峙宣传网站的设计与实现论文.doc
- 智慧交通综合解决方案.pptx
- 建筑防潮设计-PowerPointPresentati.pptx
- SPC统计过程控制程序.pptx
- SPC统计方法基础知识.pptx
- MW全能培训汽轮机调节保安系统PPT教学课件.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论13