7 ms·
Can anyone recommend an intermediate to advanced C book? I feel like most C material I've been exposed to in the past lacks what a modern C project should have
by teeray 9y ago
Can anyone recommend an intermediate to advanced C book? I feel like most C material I've been exposed to in the past lacks what a modern C project should have when it comes to best practices, code organization, build tools (should I use make for this, cmake, where does autotools fit? what about clang vs gcc?), linters (valgrind?), testing, etc. I recognize that much of this falls outside of C, but all of these tools seem like things C projects routinely grapple with.
- deleted 9y ago[deleted]
- crispinb 9y agoYou could take a look at Ben Klemens’ 21st Century C from O’Reilly. It covers that precise territory. I can’t recommend it as I haven’t read it yet — it has been on my eyes-bigger-than-my-time-and-energy book pile for a couple of years.
- fipar 9y agoI really liked this one: http://shop.oreilly.com/product/0636920033677.do http://shop.oreilly.com/product/0636920033677.do (21st century C, in case the link is wrong as I’m on my phone now)
- a3n 9y agoSecond.
- monaghanboy 9y agoI would recommend https://www.amazon.com/C-Programming-Modern-Approach-2nd/dp/0393979504 https://www.amazon.com/C-Programming-Modern-Approach-2nd/dp/.... There's specifically a section on organizing large projects IIRC.
- fellellor 9y agoThis is a very thorough, detailed book on the topic. It's pretty awesome.
- rjammala 9y agoOne of the best books: Computer Systems: A Programmer's Perspective (3rd Edition)
- donkey-hotei 9y agoI second this. Seriously the best textbook on systems programming I've worked through especially when accompanied with the famous CMU labs[0]. Anyone who works thoroughly through this book can become a master systems programmer. [0]: http://csapp.cs.cmu.edu/3e/labs.html http://csapp.cs.cmu.edu/3e/labs.html
- FourSigma 9y agoI am currently going through this book. Any advice on how to extract the most value out of it?
- donkey-hotei 9y agoSure! I recommend sitting with the book, a pen, and a notebook at a cafe or wherever you like and write solutions to the practice problems you see sprinkled in each chapter as you read every single word. Then choose a few of the homework problems and do those, some will require a computer. Most of all, work through the labs and don't cheat yourself by looking at other (probably not very good) solutions posted online! Solving the labs with the textbook and TLPI[0] as a reference is how I got the most out of the course. A list of the assignments, as they're done at CMU, is posted below[1]. Good luck! [0]: http://www.man7.org/tlpi/ http://www.man7.org/tlpi/ [1]: https://www.cs.cmu.edu/~213/assignments.html https://www.cs.cmu.edu/~213/assignments.html
- FourSigma 9y agoAwesome! Thank you for the insight!
- jimmahoney 9y agoYes - I use this and the CMU labs for an undergrad course, and it's excellent.
- thdn 9y agoTake a look at this one, you wont regret http://icube-icps.unistra.fr/img_auth.php/d/db/ModernC.pdf http://icube-icps.unistra.fr/img_auth.php/d/db/ModernC.pdf
- federicoponzi 9y agoHey, thanks a lot! Went through some pages around, and looks like a very informative book. Is there a printed version?
- bello 9y agoI really enjoyed "Deep C Secrets" as an intermediate C book: Expert C Programming: Deep C Secrets https://www.amazon.com/dp/0131774298/ https://www.amazon.com/dp/0131774298/ A little dated, still lots of relevant knowledge though.
- sifoo 9y agoOne of my all time C favorites.
- geokon 9y agoWhat I liked about this book is that it's very upfront about how messed up C is. I read about half of it and it made me never want to write any C ever again. An especially memorable part for me was how it took 3 pages of language lawyering to explain why this doesn't compile: foo(const char **p) { } main(int argc, char **argv) { foo(argv); }
- int_19h 9y agoI don't see this as a good example of "how messed up C is". It doesn't compile because you're violating const correctness, and any other language with similarly sound correctness requirements would flag it in a similar way. If anything, this is one of those rare cases where C chooses correctness over convenience. It takes quite a bit to explain because the "common sense" is that if one level of pointer indirection allows you to pass non-const where const is expected, then two levels shouldn't be any different. But common sense is wrong, and the compiler is right. And it doesn't require any language lawyering, either - all you need to do is slightly tweak the example to show why exactly it is unsafe.
- rcxdude 9y agoA better example of messed up in that example is how it's not a compile-time error (but is very nasty undefined behavior) for the function without an explicit return type (so defaulting to int) to end without returning anything.
- sifoo 9y agoAs far as books go; K&R contains a lot of wisdom despite it's apparent simplicity; and the language hasn't really changed that much since last edition. But to answer some of your questions: CMake is by far the most convenient build tool I've come across for C so far, the link below will give you an idea of how it looks. I find linters mostly a waste of time , but I run all my tests through valgrind before committing; I don't miss programming in C without valgrind at all; it's also an excellent profiler in combination with KCacheGrind. For testing, simple functions and bit of macro magic on top goes pretty far; C programmers tend to value simplicity, you won't find that many epic unit testing adventure frameworks out there. clang or gcc doesn't really matter, I prefer clang's error messages; and both support GNU extensions, which solve most of the boring problems with coding in C. https://github.com/basic-gongfu/cixl/blob/master/CMakeLists.txt https://github.com/basic-gongfu/cixl/blob/master/CMakeLists....
- earenndil 9y agoTo answer your questions: * Clang vs gcc: doesn't really matter. Clang has slightly better compile times. GCC has really advanced but in some cases still has inferior warning/error messages. Best practice is not to really use GNU c features unless you want to. If you do want to, decide if you want to just use the subset supported by clang or the full shabang from gcc. Beyond that: if you want to support both, test on both before release and do release builds using whichever produces faster or smaller (choose whichever metric you prefer) binaries. * Build systems: plain old make is sufficient in most cases. I would recommend autoconf over cmake just because it's significantly simpler, although cmake has better cross-platform support (it can generate visual studio build files, for instance). I wouldn't use something outside of make, cmake, or autoconf, even if it seems like it's better in every way than those, because it probably isn't, and even if it is, you lose out on the battle-tested, available-everywhere, and widely-used nature of the above build systems. I shouldn't have to install a build system to build your program, and if I do, I should be able to use that build system to build a significant of other programs too. * Linting: not really necessary IME. Aim for no compiler warnings, though. * Yes, use valgrind. Anything it complains about, fix. Uninitialized values and out-of-bounds memory accesses are significantly more important and worrisome than memory leaks (because they represent potential attack vectors), but still, it won't complain about something unless it's actually something that should be complained about. * Testing: like the other commenter said, just a little bit of macro magic and you're golden. * Code organization: headers in include/, source files in src/. You can separate src/ into subdirectories if your project grows to sufficient complexity that the source files become difficult to wrangle. Separating the headers into separate directories is probably not necessary, however. * Not directly c-related, but pick a SANE code style and stick to it. * Debugging: make a separate target that compiles in debug symbols and disables optimizations, and use gdb (or lldb) on it. You don't need much to get started: 99% of the time, all I do is "break main", "run" ("r" for short), "backtrace" ("bt" for short), "frame <number>" (to switch between stack frames), and "print <variable>" ("p <variable>" for short); it's taken me quite far.
- electricslpnsld 9y ago> Clang vs gcc: doesn't really matter. Clang seems to have better sanitizer support, which can be lighter weight initial passes before running valgrind.
- halayli 9y agoOne of the most challenging exercises in any language is to learn how to organize your code and modularize it at a large scale. I recommend reading Postgres's source code. But don't wander aimlessly, set a goal. For example, figure out how an sql select query is parsed and processed, how types are deduced, etc and learn the programming patterns applied as you go through. I often recommend postgres because it's highly commented, and easy to understand.
- leoc 9y agoI'm not claiming any C ability myself, but in addition to things which have already been mentioned: the old Steve Summit comp.lang.c C FAQ http://c-faq.com/ http://c-faq.com/ was only updated up to 2005 but is still hard to ignore. For the Linux C interface there's https://nostarch.com/tlpi https://nostarch.com/tlpi and http://www.apuebook.com/apue3e.html http://www.apuebook.com/apue3e.html / http://www.unpbook.com/ http://www.unpbook.com/ , all old to different degrees. http://www.danlj.org/lad/ http://www.danlj.org/lad/ was great but even the second edition http://www.worldcat.org/title/linux-application-development-second-edition/oclc/938915494 http://www.worldcat.org/title/linux-application-development-... is probably out of date by now. ( https://9p.io/cm/cs/upe/index.html https://9p.io/cm/cs/upe/index.html is most certainly out of date, but more than that it's almost an anti-C-interface book by comparison to the C-API-centric view of Unix which has become prevalent since, in some part due to the popularity of K&R... https://www.cs.princeton.edu/~bwk/tpop.webpage/ https://www.cs.princeton.edu/~bwk/tpop.webpage/ and http://www.catb.org/esr/writings/taoup/ http://www.catb.org/esr/writings/taoup/ are in the same "anti-C-book" vein.)
- pksadiq 9y agoI would recommend C: A Reference Manual (by Samuel P. Harbison and Guy L. Steele Jr.) along with K&R The C programming language. C has lots of edges where it's easy for a beginner to make mistakes. Say for example the minimal range of a char is -127 to 127 (yes, C cares 1's complement machines) or 0 to 255. Not knowing this may result in a code working in one machine while broken on others. So learning the standard is a must, especially, the undefined behaviors.