11 ms·
Essential C (2003) [pdf]
- callinyouin 6y agoThis, paired with their Pointers and Memory [1] guide are how I learned C in college. They're both pretty short and to the point, I would highly recommend. [1] http://cslibrary.stanford.edu/102/PointersAndMemory.pdf http://cslibrary.stanford.edu/102/PointersAndMemory.pdf
- emmelaich 6y agoThat and other useful links are listed in the parent page of this pdf. http://cslibrary.stanford.edu/101/ http://cslibrary.stanford.edu/101/
- cogburnd02 6y ago> the greatest pointer/recursion problem ever (advanceed) Does 'advanceed' mean 'more advanced than advanced?' [1] [1] https://www.youtube.com/watch?v=YAYKnnWCzto https://www.youtube.com/watch?v=YAYKnnWCzto
- johnisgood 6y agoI get 404 for the videos on http://cslibrary.stanford.edu/104/ http://cslibrary.stanford.edu/104/. Example: http://www.cs.stanford.edu/cslibrary/PointerFunCBig.avi http://www.cs.stanford.edu/cslibrary/PointerFunCBig.avi returns 404. :(
- dang 6y agoIf curious see also 2018 https://news.ycombinator.com/item?id=18790421 https://news.ycombinator.com/item?id=18790421 2016 https://news.ycombinator.com/item?id=11671985 https://news.ycombinator.com/item?id=11671985
- commandlinefan 6y ago> Relying on the difference between the pre and post variations of these operators is a classic area of C programmer ego showmanship. Ugh. He's talking about inline use of post-increment and pre-increment (i.e. x++ and ++x) here. This is perfectly readable to a C programmer, and sidestepping them actually makes the code harder to understand.
- miketuritzin 6y agoCan you give an example where not using them 'inline' makes code harder to understand?
- Koshkin 6y agoYou can't beat return x++; :)
- sigjuice 6y agoWhy? This should be straightforward.
- a1369209993 6y agoConsider the idiomatic way of interating backward through a array: for(i=n; i-- > 0 ;) { /* operate on a[i] */ } converting i--; to a statement at the start of block makes it less clear that it's part of the iteration idiom rather than a ad hoc adjustment that's specific to this particular logic. There are other examples, but they're either more involved or statementification is less obviously wrong.
- ramshorns 6y agoThat's the idiomatic way? Cool. The more straightforward-looking way, for(i = n-1; i >= 0; i--) { /* operate on a[i] */ } breaks if i is unsigned, like a size_t.
- a1369209993 6y agoYep. That why it's a idiom, rather than a obvious-way-of-doing-it-that-anyone-competent-would-use.
- nikki93 6y agoHmm, I think `for (i = n - 1; i >= 0; --i)` is way clearer and maybe more common? edit: Ah unsigned underflow. :O
- jimbob45 6y agoThis is a pretty neat guide if you’re cheap and have moral qualms about pirating K&R. Still, I think the best introduction to C remains K&R.
- Gibbon1 6y agoK&R is woefully out of date. Gives you no info on how to do things safely and sanely. And encourages a leet style of programming that results in catastrophic edge case bugs. As you can see in comments above where naive code that iterates backwards through an array fails when the array size is 0. Worse K&R leet style buys you absolutely nothing with a optimizing compiler written in the last 30 years.
- saagarjha 6y agoK&R is actually fairly reasonable about performance, and much less l33t than code I have seen in the wild. It is a very good introduction about the language, and although I would not ever call it "woefully out of date" I would say that it is a good idea to read more about the current state of bugs and tooling, which is not discussed because the book is a general overview of the language.
- jimbob45 6y agoI hate guides showing me how to do things safely and sanely from the get-go. Show me how to do it. If safety and sanity are priorities for me, I’ll seek those out on my own.
- flohofwoe 6y agoEh, K&R Second Edition is a very useful book also today. The only downside is that stops at C89 and hasn't been updated for C99.
- hivacruz 6y agoI highly recommend the CS50 course to get familiar with C: https://www.youtube.com/playlist?list=PLhQjrBD2T381L3iZyDTxRwOBuUt6m1FnW https://www.youtube.com/playlist?list=PLhQjrBD2T381L3iZyDTxR... Sure it doesn't get in details about the language but you get the essential and the videos are great.
- kecupochren 6y agoIt's how I started my career 7 years ago. Amazing course and lecturer
- PinkMilkshake 6y agoI'm doing CS50x at the moment and I can definitely recommend it. It got me interested in C despite trying to avoid it my entire life. David Malan is one of the best lecturers I've seen.
- stellersjay 6y agoI prefer something that is more up to date https://nostarch.com/Effective_C https://nostarch.com/Effective_C
- eternalban 6y agoThere is also Jens Gustedt's Modern C: https://modernc.gforge.inria.fr/#org81433c2 https://modernc.gforge.inria.fr/#org81433c2
- saagarjha 6y agoThis guide is short (which is always nice) but not has a couple of flaws in places in the brief skim I gave it. For example: > In particular, if you are designing a function that will be implemented on several different machines, it is a good idea to use typedefs to set up types like Int32 for 32 bit int and Int16 for 16 bit int. Use <stdint.h> please > The char constant 'A' is really just a synonym for the ordinary integer value 65 which is the ASCII value Not always, especially right after you came off a paragraph explaining how different machines have implementation-specific behaviors > The compiler can do whatever it wants in overflow situations -- typically the high order bits just vanish. This is a good time to explain what undefined behavior actually means > The // comment form is so handy that many C compilers now also support it, although it is not technically part of the C language. Part of the language since C99 > C does not have a distinct boolean type _Bool since C99 > Relying on the difference between the pre and post variations of these operators is a classic area of C programmer ego showmanship. I'm fine with you mentioning that this can be tricky, but this is more opinion than I am comfortable with in an introductory text > The value 0 is false, anything else is true. The operators evaluate left to right and stop as soon as the truth or falsity of the expression can be deduced. (Such operators are called "short circuiting") In ANSI C, these are furthermore guaranteed to use 1 to represent true, and not just some random non-zero bit pattern. Under the assumption that there are no boolean types from earlier, this is not true > The do-while is an unpopular area of the language, most everyone tries to use the straight while if at all possible. I would argue that people use do-while more than they need to > I generally stick the * on the left with the type. Not a problem, but :( > The & operator is one of the ways that pointers are set to point to things. The & operator computes a pointer to the argument to its right. The argument can be any variable which takes up space in the stack or heap And constants/globals > To avoid buffer overflow attacks, production code should check the size of the data first, to make sure it fits in the destination string. See the strlcpy() function in Appendix A. strlcpy is non-standard and probably not what you want > The programmer is allowed to cast any pointer type to any other pointer type like this to change the code the compiler generates. > p = (int * ) ( ((char * )p) + 12); // [Some spaces added by me to prevent Hacker News from eating the formatting] Only in some very specific cases… > Because the block pointer returned by malloc() is a void* (i.e. it makes no claim about the type of its pointee), a cast will probably be required when storing the void* pointer into a regular typed pointer. Casting malloc is never required (and I would say usually not a good thing to do)
- andrewstuart 6y agoI don't want to learn C but certain types of applications there's really no practical alternative.
- galenguyer 6y agoI typically see Rust marketed as a better replacement for C. In what cases isn't it a practical replacement?
- deleted 6y ago[deleted]
- edgyquant 6y agoIts a replacement for c++ not c
- saagarjha 6y agoWhen you’re targeting platforms that Rust doesn’t, have a lot of legacy code that can’t do for a new toolchain, or cannot handle dependencies.
- flohofwoe 6y agoRust is a fine replacement for C++, but not really for C, and the reasons why Rust can't be a replacement for C are very similar to why C++ can't be a replacement for C. Everybody who chooses C today has slightly different reasons to do so, but one important reason is that C is a very small language with a very small standard library, and most parts of the standard library can be ignored without losing any of the "qualities" of the C language. IME a small language feature set makes it easier to evaluate and integrate third party code, it's hard to say exactly why that is, but that's my experience anyway. Of course one could say "nobody forces you to use the whole feature set of Rust", but the same has been said in the C++ world for decades. The problem is that everybody selects their own subset of the language when having the choice.
- drran 6y agoCore Rust is very small language. It doesn't have memory allocator, so it can be used in embedded, where heap is not available. C++ cannot do that. https://doc.rust-lang.org/core/index.html https://doc.rust-lang.org/core/index.html
- orionblastar 6y agoI learned from Sam's C Primer Plus. https://www.amazon.com/Primer-Plus-5th-Stephen-Prata/dp/0672326965 https://www.amazon.com/Primer-Plus-5th-Stephen-Prata/dp/0672... My version was older than the Amazon version as I learned in 1987.
- underanalyzer 6y agoNow then, isn't that nicer? (editorial) Build programs that do something cool rather than programs which flex the language's syntax. Syntax -- who cares? Although some feel like it’s too opinionated to belong in this article I really appreciated the above. Edit To be clear the author is advocating for simpler syntax here to increase program readability. This could be taken other ways.
- hacktember 6y agoThis was the PDF used in my UNIX class! That brings back some memories...
- johnnujler 6y ago> Now then, isn't that nicer? (editorial) Build programs that do something cool rather than programs which flex the language's syntax. Syntax -- who cares? I never really got to a point of learning Haskell or Lisp up until recently, it was always this --- I can do everything with C/C++/Java/Python and I could. But the thing is it is only after learning lisp that I really got the hang of thinking in top down manner(recursively), or for that matter it took Haskell to teach me composition intuitively, which then could be extended to my main language(C++). I understand that syntax doesn't matter much, but fwiw I still think in terms of lisp syntax when writing recursive code in C++/C. So yeah, take that for you will.
- Flex247A 6y agoA very great resource!
- wyclif 6y agoI also liked his "Essential Perl" back in the day: http://cslibrary.stanford.edu/108/EssentialPerl.pdf http://cslibrary.stanford.edu/108/EssentialPerl.pdf
- heinrichhartman 6y agoWhile we are here, does anyone have a good resource about memory management strategies in C? Topics: * Best practices for C function signatures (caller allocates (which size?), callee allocates (where? which allocator?)) * Memory Ownership Models * Borrowing * Reference Counting * Garbage Collectors and C-Libraries providing this functionality * Interning Objects (Strings) * RAII [1]? And it's benefits/flaws [1] https://en.wikipedia.org/wiki/Resource_acquisition_is_initialization https://en.wikipedia.org/wiki/Resource_acquisition_is_initia... Context: I feel that understanding the C memory primitives in not that hard (stack variables, malloc/free, C++'s new). But how to use them is devilishly tricky. I have seen little information about this.
- flohofwoe 6y agoShameless plug: This generally discusses the lack of RAII in C (towards the end), and what to do about it: https://floooh.github.io/2019/09/27/modern-c-for-cpp-peeps.html https://floooh.github.io/2019/09/27/modern-c-for-cpp-peeps.h... ...and this presents a (reasonably runtime-safe) general memory management strategy using tagged-index-handles instead of pointers: https://floooh.github.io/2018/06/17/handles-vs-pointers.html https://floooh.github.io/2018/06/17/handles-vs-pointers.html The gist is basically: - don't allocate small chunks of memory all over the code base, instead move memory management into few centralized systems, and let those systems own all memory they allocate - don't use pointers as public "object references", instead use "tagged index handles" - don't use "owning pointers" at all, use pointers only as short-lived "immutable borrow references"
- ngcc_hk 6y agoAnother global (imperative style) vs object (code and data by object) vs functional ...
- heinrichhartman 6y agoThanks for this!
- ngcc_hk 6y agoNormal c is not difficult. But once get yourselves into using it in eg the hacking of game boy (still remember the confusion of the data, tile etc) and basic bare metal small machine. Just hard.
- marttt 6y ago"Teach Yourself C in 24 Hours" by Tony Zhang (1997) seems interesting as well: http://aelinik.free.fr/c/ http://aelinik.free.fr/c/ A previous HN discussion on that book: https://news.ycombinator.com/item?id=15624521 https://news.ycombinator.com/item?id=15624521 EDIT: That earlier discussion has an excellent first post. Quoting: "It bothers me so much that very few books (Kernighan) talk about WHY. WHY. WHY is a variable needed? WHY is a function needed? WHY do we use OOP? Every single book out there jumps straight into explaining objects, how to create them, constructors, blah blah blah. No one fricking talks about what's the point of all this? Teaching syntax is like muscle memory for learning Guitar. It is trivial and simply takes time. Syntax - everyone can learn and it is only one part of learning how to code. Concepts are explained on their own without building upon it. [... A list with learning resources the poster finds great ...] This is learning how to produce music. Not learning the F chord. Teaching how to code is fundamentally broken and very few books/courses do it well."