4 ms·
Apart from the new features added in C11 or C17 that others have mentioned, both Beej's Guide and Modern C also cover threads and string encoding (neither of th
by thamer 5y ago
Apart from the new features added in C11 or C17 that others have mentioned, both Beej's Guide and Modern C also cover threads and string encoding (neither of these topics are covered in K&R 2nd edition, as far as I remember). Beej's Guide also has a section on internationalization and one about date/time handling.
But yes, the code is also quite different than in K&R which is relatively terse. See for example this comment in K&R about strcpy on print page 105 (page 119 of the PDF)[1], where after showing two versions of strcpy that are pretty readable and easy to follow, the book says:
> In practice, strcpy would not be written as we showed it above. Experienced C programmers would prefer
void strcpy(char *s, char *t)
{
while ((*s++ = *t++) != '\0')
;
}
Followed by a paragraph saying the condition could be simplified, and that
> the function would likely be written as
void strcpy(char *s, char *t)
{
while (*s++ = *t++)
;
}
Make of that what you will :-)
[1] https://kremlin.cc/k&r.pdf#page=119 https://kremlin.cc/k&r.pdf#page=119
- derefr 5y agoI know that while (*s++ = *t++); is the type of thing seemingly designed to reach through history to make a modern programmer spit out their tea — but I also can’t help but feel that it’s a very direct “C machine” representation of the semantics of a processor string instruction, ala x86’s REPNZ MOVSB.
- pjmlp 5y agoWhile experienced C programmers that care about security wouldn't even touch strcpy(). All those code samples will crash and burn if s and t point to the wrong locations, t happens to point to a string without a null terminator, or s points to a buffer not big enough to handle the string pointed by t.
- tom_mellior 5y ago> All those code samples will crash and burn if s and t point to the wrong locations This is true for all uses of pointers in C. The only validation you can do is whether a pointer is null. Apart from that you can never know that a pointer points to a non-wrong location. And still experienced C programmers seem to be OK with using pointers.
- pjmlp 5y agoActually no, many OSes do have APIs to validate pointer integrity. But even then, most experienced C programmers don't care about using them anyway, that is why Apple, Oracle, Microsoft, ARM, Google, Cambridge university are all leading efforts for hardware memory tagging. So it won't matter how much they care, as the OS will kill their beloved application when pointers get misused. A scenario already made reality in platforms like Solaris SPARC.
- tom_mellior 5y ago> many OSes Are outside the scope of the C language. But if you allow these special APIs, you can use them to implement a safe strcpy. Either way your point from above is still invalid.