TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

The Ten Commandments for C Programmers (Annotated Edition)

41 pointsby b-manover 14 years ago

6 comments

jwsover 14 years ago
I wonder when this was written. The earliest reference I found was June 1993 in an AUUG (Australian Unix Users Group) newsletter. <a href="http://books.google.com/books?id=lbsmK77EVU4C&#38;lpg=PA1&#38;pg=PA36#v=onepage&#38;q&#38;f=false" rel="nofollow">http://books.google.com/books?id=lbsmK77EVU4C&#38;lpg=PA1&#3...</a><p>1) <i>run lint</i>: Not anymore. But then I'm a -Wall -Werr kind of guy and I visit Clang.<p>2) <i>don't follow NULL</i>: Well, obviously.<p>3) <i>cast function arguments to the expected type</i>: Not important now that the types are in the declarations. But if you don't force that, then yes. Got that right. Just as people got complacent with 32bit data and address, BANG!<p>4) <i>declare return types for functions</i>: That insanity has passed from practice.<p>5) <i>check bounds</i>: Yep, still causing exploits today.<p>6) <i>check error codes</i>: Yep, and triples is about right for library/system call intensive code. But the first time fclose() returns an error and you catch it you will be happy.<p>7) <i>don't re-implement the library</i>: Beating a modern C library is hard. But: if you have a special case, you can still get a significant gain by exploiting that, after you have profiled and know you aren't engaging in petty puffery. (It should be mentioned that the author is the Henry Spencer that wrote and distributed a regex library back in the days of ftp.)<p>8) <i>use K&#38;R brace styles</i>: I care so little for brace styles I can't tell you which is K&#38;R after writing hundreds of thousands of lines of C code.<p>9) <i>external symbols must be unique in the first 6 characters</i>: Ha ha! Good riddance SCO, the last system I met where that was an issue (in <i>ar</i> libraries only I think.)<p>10) <i>all the world's a vax</i>: s/VAX/x86/ e.g. I keep a PPC (byte order) and an ARM (char is unsigned) just to keep me honest on this.
评论 #1878477 未加载
评论 #1877868 未加载
评论 #1878268 未加载
johnl87over 14 years ago
Little known fact...the only reason the brace is on the same line as the function declaration is because K&#38;R just wanted to save space in the code listings.
评论 #1877616 未加载
评论 #1878575 未加载
lisperover 14 years ago
&#62; Thou shalt not follow the NULL pointer<p>How much pain and misery could be avoided if hardware manufacturers would simply design computers so that memory location 0 was hard-wired to always contain a 0.<p>Sigh.
评论 #1877411 未加载
评论 #1877610 未加载
评论 #1877373 未加载
评论 #1877378 未加载
评论 #1878134 未加载
评论 #1878209 未加载
ldngover 14 years ago
Any chance someone "translate" it to plain modern english for us poor foreigners ? I find it quite hard to read for non-natives, not even talking about cultural references.
wyclifover 14 years ago
This is ancient. It's so old I can't remember the last time I saw it, but I'm glad somebody remembered to submit it just for kicks.
palishover 14 years ago
<i>"Also, contrary to the beliefs common among the more backward inhabitants of the Polluted Eastern Marshes, `NULL' does not have a pointer type, and must be cast to the correct type whenever it is used as a function argument."</i><p>Why? This seems like a waste of thought...
评论 #1878021 未加载