Well I just finished reading K&R. I got it for my machine structures class in Fall '09, but we were never assigned any exercises out of it so I never bothered reading it because I already knew C. Since then, I've read several mentions of what a great book it is from the likes of Linus Torvalds and Rob Pike among others, so I decided to give it a read.
My impression: what...the...fuck? Are you serious? The examples in K&R are often cited as ``the official way to code in C,'' but 90% of them are badly designed on purpose so they only utilize features that have been introduced up to that point in the book. That may make it a great beginner's book, I guess, but as a reference or example of best practices, lolno.
I got the same feeling when first reading it but then I realized it was the right way to code in C.
Name:
Anonymous2011-10-04 15:59
>>2
It's illogical to say that. According to K&R, which of these is correct?
void print(char s[]);
void print(char *s);
The book uses both. Of course, the first is only used until pointers are introduced, so the latter is obviously correct. But the fact that the book has so many conflicting style and design practices makes it unacceptable as a guideline.
90% of them are badly designed on purpose so they only utilize features that have been introduced up to that point in the book. That may make it a great beginner's book, I guess, but as a reference or example of best practices, lolno.
Which is what it aims to be. The Appendices are meant to be references, the code examples are part of the learning process. What did you expect? That the book would show you the most ZOMG OPTIMIZED way of doing something before the concepts used to do that were even introduced?
>>3
C programmers tend to overvalue K&R, when IMO it's actually really crappy, as you also devised. Opinions vary a lot and there's still the attempt to use the argument-from-authority to ilegitimize criticism.
I believe that there's no need to read K&R if you're fluent with regards to the most recent standard. Stylistical decisions, at least when they actually matter, are taken as a group decision in which your personal tastes will rarely be reflected as a whole. The ultimate goal is simply to be productive, instead of being liturgically acceptable according to some obscure programming cult.
With regards to your example, I'd use the former (char s[]) to render clear that the function expects an array of some sort, specially if the array has a fixed size (in which you would put the array size in the declaration), and the latter otherwise.
>>6
First's _not_ a VLA. It's simply a pointer, even sintactically speaking. It comes from the fact that arrays are always (and have always been) passed as references. It has nothing to do with VLA whatsoever. VLAs have been introduced in C99, and have pretty obvious uses.
With regards to your example, I'd use the former (char s[]) to render clear that the function expects an array of some sort, specially if the array has a fixed size (in which you would put the array size in the declaration), and the latter otherwise.
Well the function's called print, s is obviously a string. When dealing with output, it's pointless to note the size of the array the string is stored in because it should be terminated by a null character, and if it's not, you're still going to end up printing junk even if you stop at the end of the array.
I would personally write such a function:
void print(char *s) {
while (*s != '\0')
putchar(*s++);
}
Declaring char s[] in this case wouldn't be as clear. In fact, K&R says it's illegal, but gcc will compile it without warning.
Name:
Anonymous2011-10-04 20:26
>>9
Yeah, I should have made clearer that when strings are involved, char* is preferred. Sometimes, however, you're dealing not with strings, but with byte arrays of fixed size (which are also char-typed), for example, inside block cipher routines. In these cases I would go with char[] (and maybe further qualify it with the array size, static and restrict if applicable).
Array-to-pointer argument decay is standard behavior, thus I suppose GCC is right (and K&R is wrong).
Number of Turing awards earned by K&R: 1
Number of Turing awards earned by OP: 0
Name:
FrozenVoid2011-10-05 0:29
I don't read books such as K&R,SICP or TAOCP.
Unless the book is provided for free and is relevant to my current/specific needs i won't read it. And i don't intend to waste my time reading entire books, i just briefly check the relevant charters. If your book relies on previous charters or any complex concepts requiring to read the entire book, tough luck: i won't read it.
You should invest in some very cheap very out of date technical books. They are fun to read and cost between 20 - 50 cents. It's true that programming and related activities take practice to learn, but sometimes without instruction your approach to solving problems may be too limited, as you are simply not yet aware of the better alternatives. Sifting through a long book on the subject will give you an idea of what's out there.
Name:
Anonymous2011-10-05 2:54
agreed. an hour of book reading and 4 hours of coding will usually teach you more than 8 hours of coding. at least for the first few years.
Name:
Anonymous2011-10-05 8:51
You should note that it gives a detailed and simultaneously concise description of the smallest subtlities of the language, aside from being beginner friendly. Thus is serves the dual purpose of a reference manual and a guide to the apprentice.
Name:
Anonymous2011-10-05 10:11
Who would win?
A man that reads his k&r sicp and taocp or
the man that learned via experimenting and reading others code
The first one. Since a good programmer comes once in a blue moon, the man who learned via experimenting and reading others' code is bound to be terrible!
>>18
The one that both experimented with code and read eir books.
Also, please use spivak pronouns instead of implicitly coercing programmers into men.
Name:
Anonymous2011-10-05 10:26
>>18
the man with higher IQ. My SAT was 1440, what was yers?
Name:
Anonymous2011-10-05 10:40
>>1 I've read several mentions of what a great book it is from the likes of Linus Torvalds and Rob Pike among others, so I decided to give it a read.
You're are naive as a little girl. These persons are paid to advertise various merchandise, including books.
Name:
Anonymous2011-10-05 10:54
Scheme is an excellent lube.
God says...
C:\KLANSMAN-HANDBOOK\KILL-THE-NIGGER.PDF
i masturbated and
thought of python
my phalli hurts
Name:
Anonymous2013-08-31 13:17
now the question is, is it going to end like Bokurano (unlikely) or Mai-Hime(much more likely)?
Name:
Anonymous2013-08-31 14:02
You fail because you're stupid
Name:
Anonymous2013-08-31 14:47
I think one of the momusus (excluding Sayu) should develop a mean and bitchy persona for variety. Not half-assed shit though, like really sharp-witted. Not against the other girls though, just against everyone else. Had I not known how Ayumi is I'd think she'd be perfect for that.
Name:
Anonymous2013-08-31 15:32
Beat Yasaka's Ordeal (試練 means trail and also ordeal, but it is funny how the official translation is different for both).