
Ask HN: Books to learn advanced C programming - vshan
Hello,<p>I&#x27;ve recently been doing a lot of systems programming and low-level stuff. I&#x27;m hacking away on MIT&#x27;s xv6 kernel for my OS course. I want to improve my C skills, and want to learn the limits of what can be done with C, the preprocessor etc. I&#x27;d be really grateful if some one could direct me to resources that would help me out in this regard.<p>I want to basically learn how to write robust, readable and production-quality C which doesn&#x27;t crash to a segfault.<p>Also, is there a oft-used style guide for writing error-handling code in C?<p>Thank you.
======
wrp
For good practices in writing high-reliability C, look at these:

[http://lars-lab.jpl.nasa.gov/JPL_Coding_Standard_C.pdf](http://lars-
lab.jpl.nasa.gov/JPL_Coding_Standard_C.pdf)

[http://www.leshatton.org/MISRA_comp_1105.html](http://www.leshatton.org/MISRA_comp_1105.html)

[http://www.leshatton.org/ISOC_subset1103.html](http://www.leshatton.org/ISOC_subset1103.html)

[http://www.embedded.com/electronics-blogs/embedded-
systems-d...](http://www.embedded.com/electronics-blogs/embedded-systems-
design-magazine-archive/4216555/Embedded-Systems-Design--May-2011)

For the related topic of secure code, these books are good:

Mark Dowd. 2006. The Art of Software Security Assessment.

Robert Seacord. 2013. Secure Coding in C and C++, 2e.

For details on what is really going on inside C:

Paul Anderson. 1998. Advanced C: Tips and Techniques.

Kenneth Louden. 1997. Compiler Construction: Principles and Practice. Chapter
7.

For C gotchas and general good practices:

[http://www.cs.tufts.edu/comp/40/reference/CTrapsAndPitfalls....](http://www.cs.tufts.edu/comp/40/reference/CTrapsAndPitfalls.pdf)

Allen Holub. 1995. Enough Rope to Shoot Yourself in the Foot.

P.J. Plauger. 1991. The Standard C Library. (It is partly a reference, but
also has detailed discussion of the technical considerations faced in writing
reliable code.)

~~~
davismwfl
This is a good list. Reading and learning about the embedded world can help
anyone grasp recovery and error handling techniques. Also, in C a solid
understanding of errno and how to properly utilize it is important.

Also, at least personally, I stick to 3 key basic rules in C/C++ (I have a lot
more, but these are important):

    
    
      1. Functions should do 1 thing and only 1 thing.  The minute a function tries to do 2 or 3 things, or it goes over 50-60 lines (with a few exceptions around conditionals) it is trying to do to much.   
     
      2. Check all parameters for validity before doing anything else in the function.
     
      3. Check all return values.  
     

Really solid and reliable code is usually way less complex then what you see
in a lot of code bases. That doesn't mean it isn't a super complex problem
getting solved, just that the reliability is gained by having simplicity at
the lowest levels. As someone mentioned Doom is a good example of a solid code
base that does some pretty complex things, but cleanly and simply overall.

------
tumba
I suggest C Interfaces and Implementations: Techniques for Creating Reusable
Software by David Hanson.[1] It focuses on helpful engineering practices for
reliably and efficiently implementing algorithms in C.

I also second the recommendation for Peter van der Linden's Expert C
Programming for its masterful treatment of how C really functions, including
the surprisingly frequent areas in which real-world constructs produce
"undefined behavior" from the perspective of the C standard.

[1] [http://www.amazon.com/Interfaces-Implementations-
Techniques-...](http://www.amazon.com/Interfaces-Implementations-Techniques-
Creating-Reusable/dp/0201498413)

------
lastofus
"Deep C Secrets" is quite good.

[http://www.amazon.com/Expert-Programming-Peter-van-
Linden/dp...](http://www.amazon.com/Expert-Programming-Peter-van-
Linden/dp/0131774298)

------
kiloreux
Of course there is the C programming language k&r and also Expert C
programming which kind of cool also, but if you want to make really great
software and write great code, then your only is not books but real OSS, go
write some small project, improve it, present it to community and have it
criticized , and so on in a loop, look for big projects source and learn from
them, one fairly interesting project is radare2[1], it's a really cool
project, please feel free to choose but look at projects already made and try
to understand, you will learn a bunch from doing that.

[1] [https://github.com/radare/radare2](https://github.com/radare/radare2)

------
ThatGeoGuy
Since it has yet to be mentioned yet, I'll suggest Modern C by Jens Gustadt
[1]. The book itself starts from basics and works up, but I found it to be
much more reliable and filled with more practical advice than the common K&R
recommendation. Section / Level 4 is not quite in it's final form yet; however
the book is mostly complete and gives a lot to think about. I suggest working
through it, even if you skip the first few levels because of your base
knowledge.

[1] [http://icube-icps.unistra.fr/img_auth.php/d/db/ModernC.pdf](http://icube-
icps.unistra.fr/img_auth.php/d/db/ModernC.pdf)

------
ahanjura
Expert C Programming: Deep C Secrets bu Peter Van Der Linden is a classic and
must see.( __ _Highly recommended_ __)

C Interfaces and Implementations: Techniques for Creating Reusable Software by
David R Handon is another must check out.

------
ryanlm
Start writing every project, except web projects in C. You'll be an expert in
a few years. You'll get really good at string handling. You'll start to "think
in bytes".

------
blacksqr
_Write Portable Code_ by Brian Hook is excellent. Under the guise of teaching
the writing of portable code, he teaches you to write good code, just as you
describe.

------
arjn
The comp C faq is a good place once you have a little bit of C experience.

[http://c-faq.com/](http://c-faq.com/)

------
jti107
this isnt a book...but i've heard the doom source code is quite good.

~~~
hga
Certainly reading a lot of good C code is a part of this; for me, reading the
predecessor _Lions ' Commentary on UNIX 6th Edition, with Source Code_
([https://en.wikipedia.org/wiki/Lions%27_Commentary_on_UNIX_6t...](https://en.wikipedia.org/wiki/Lions%27_Commentary_on_UNIX_6th_Edition,_with_Source_Code))
was a key part of my learning serious C in the early '90s.

Plus of course hacking on programs, most often various ersatz Emacs versions
that would run on a PDP-11. I.e. something I _really_ wanted to get working to
my satisfaction.

