5 ms·
I am using it to study C. Can you tell me the reason why you wouldn't recommend it?
by valacmur98 7y ago
I am using it to study C. Can you tell me the reason why you wouldn't recommend it?
- saagarjha 7y agoI really enjoyed it, but it’s best supplemented with exposure to modern C syntax and an introduction to undefined behavior.
- ThrowawayR2 7y agoDespite being amazingly well written, the most recent edition of K&R was published in 1988. The current version of C is C18, released last year, so K&R is missing about 30 years of evolution. It's not a bad book, by any means, but it doesn't include important aspects about how C is used today.
- Gibbon1 7y agoBecause it's 30 years out of date. It's missing three revisions of the language. Teaches a lot of practices that lead to obtuse brittle code riddled with security holes. Avoid.
- hnbroseph 7y agocan you be more specific about these practices?
- Gibbon1 7y agoWhat ufo said > implements strcpy as `while(dst++ = dst++); The book is full of stuff like that.
- cma 7y agoDoesn't strcpy always have that issue? The terseness is one issue, but the security problem is that it implements strcpy at all without warning.
- iamnotacrook 7y agoYou tend to get warnings to use the _s versions of some functions (strcpy_s in this case) these days.
- cesarb 7y agoI just tried (with gcc 9.2.1), and got no warning. As far as I know, these _s versions aren't that good, and often aren't available at all; see http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1967.htm http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1967.htm which says among other things "[...] none of the popular Open Source distribution such as BSD or Linux has chosen to make either available to their users. At least one (GNU C Library) has repeatedly rejected proposals for inclusion [...]" "[...] As a result of the numerous deviations from the specification the Microsoft implementation cannot be considered conforming or portable."
- iamnotacrook 7y ago"often aren't available at all" It's in C11. Perhaps Red Hat use non-compliant compilers, or very, very old ones?
- cesarb 7y agoIt's an optional part of C11, which means that even a fully compliant compiler does not have to implement them.
- lone_haxx0r 7y agostrcpy_s has no manual entry on Ubuntu, while "man strcpy" and "man strncpy" work as expected. Why would that be?
- ufo 7y agoK&R is a well written book, and is very pleasant to read. IMO, it is one of those books that everyone should read one day or the other. The main thing to watch out about it these days is that K&R is a bit carefree about the numerous ways where you can shoot yourself in the foot using C. For example, K&R famously implements strcpy as `while(dst++ = dst++);`. These days, most people would say that code is excessively terse, and that you shouldn't be using strcpy in the first place (because it can potentially overflow the dst buffer).
- einpoklum 7y agoYou meant: while ((dst++) = (src++)) I believe.
- Gibbon1 7y agowhile ((dst++) = (src++));
- billforsternz 7y agoBoth of these replies are missing the asterisks to indicate pointer indirection. Probably HN is eating each asterisk, treating it as emphasis or something. Also terse it may be, but this (assuming the missing asterisks are restored) is a good, idiomatic implementation of strcpy. Whether strcpy is good or evil is a separate discussion.