News for hackers. Stuff that matters.
Re:Learn C The Hard Way by hp on Sunday July 03, 2011 @12:12AM (#2723055)
Ohloh says I've changed at least half million lines of C code (https://www.ohloh.net/accounts/rhp/positions/total) Play me a tiny violin ;-)

What kinda bugs me is that whenever people go to teach C, they make out like it _has_ to be a low-level exercise, as if writing in C suddenly means you can't use abstract data types or object-oriented style or name your functions properly or have Unicode support.

For example, people teach libc string APIs like scanf() and strtok(), which should almost never be used. (See http://vsftpd.beasts.org/IMPLEMENTATION for one take.) Instead, use http://git.gnome.org/browse/glib/tree/glib/gstring.h or write your own like http://cgit.freedesktop.org/dbus/dbus/tree/dbus/dbus-string....

If you're going to display user-visible text, you are pretty much required to link to GLib or another Unicode library, since libc doesn't have what you need (unless you want to use the old pre-unicode multi-encoding insanity).

Don't use fgets() and other pain like that, use g_file_get_contents() perhaps, or another library. (g_file_get_contents is in http://developer.gnome.org/glib/stable/glib-File-Utilities.h...)

You need help from a library other than libc to deal with portability, internationalization, security, and general sanity.

Maybe more importantly, a library will show you examples that in C you can still use all the good design principles you'd use in a higher-level language.

I told someone to "use a string class" in C recently for example, and they said "C doesn't have classes" - this is confusing syntax with concepts.

C requires more typing and more worrying about memory management, that's all. It doesn't mean that all the best practices you know can be tossed.

There's a whole lot to be said about how to write large, maintainable codebases in C, and it can even be done. It's not something I would or do choose to do these days, but it can be done.

One other thought, two of the highest-profile C codebases, the Linux kernel and the C library, have extremely weird requirements that simply do not apply to most regular programs. However, a lot of people working in C or writing about C have experience with those codebases, and it shows.

Re:Learn C The Hard Way by JoshTriplett on Sunday July 03, 2011 @12:28AM (#2723075)
The Linux kernel, weird requirements and all, actually represents an excellent example of using C well. It has high-level functions, objects, classes, abstract data types, interfaces, and many other useful abstractions to make C almost comfortable.
Re:Learn C The Hard Way by hp on Sunday July 03, 2011 @12:35AM (#2723084)
Agreed, but it's still sort of a weird case, I think. The considerations related to things like memory allocation, performance, concurrency, internationalization, security, IO, etc. are pretty different in the kernel.
Re:Learn C The Hard Way by alf on Sunday July 03, 2011 @12:25AM (#2723071)
C shouldn't be used for anything other than low level programming these days. There are other higher level programming language that would make your job a whole lot easier. Some programmers, like me, are stuck maintaining legacy C application code, but I wouldn't wish that fate for new programmers looking to learn C as another tool in the belt.
Re:Learn C The Hard Way by hp on Sunday July 03, 2011 @12:39AM (#2723090)
Maybe "low level" is a bit ambiguous.

I certainly agree that C is most appropriate when you are "low in the stack," just above the operating system and maybe implementing something like a virtual machine. I don't make a habit of writing stuff in C for no reason and the vast majority of programming these days isn't and shouldn't be in C (or C++ for that matter).

However, "low in the stack" is different from "low level" like "I refuse to use modern practices" or "I get to omit half the letters from my function names." You can be coding an on-the-metal kind of thing and still think about it in a high level way.

Re:Learn C The Hard Way by alf on Sunday July 03, 2011 @12:46AM (#2723096)
Right, agreed. :)
Re:Learn C The Hard Way by z92 on Sunday July 03, 2011 @03:03AM (#2723254)
Other uses of C besides low level programming is:

1. Building static libraries. 2. Building so and dll. 3. Binding those so/dll with higher level languages like Ruby, Python, Perl and PHP.

That is my main use of C these days. I always write the IO, user interface code in higher level languages and put all processing code in a so or dll written in C.

Re:Learn C The Hard Way by regularfry on Sunday July 03, 2011 @03:50AM (#2723307)
I've done a bit of this in Haskell, and it can be remarkably clean. It can also be a right pain, though.
Re:Learn C The Hard Way by malkia on Sunday July 03, 2011 @04:34AM (#2723360)
Yup. Same here.

Having not to deal with exceptions, STL libs, makes life a bit easier.

Re:Learn C The Hard Way by omaranto on Sunday July 03, 2011 @09:22AM (#2723648)
Of course, in most languages you can use most programming language features by "typing more", but when a certain feature is not provided by the language or a standard library or at least a de facto standard library, everybody will implement it in a slightly different, incompatible way. I assume that is what people mean by "C doesn't have classes".
Re:Learn C The Hard Way by zedshaw on Sunday July 03, 2011 @01:21AM (#2723139)
I'm glad you got so much about how my book is written from a few paragraphs in an unfinished manuscript for it.
Re:Learn C The Hard Way by speckledjim on Sunday July 03, 2011 @03:40AM (#2723295)
You should stop thinking everyone is attacking you. I think you'd be happier.

Really though, there's no content here. The fact it's been voted to #1 on hackernews shows just how bad things are.

Items should be upvoted on their merit, rather than who did them. Someone writing another book about programming C isn't newsworthy.

Re:Learn C The Hard Way by rfrey on Sunday July 03, 2011 @08:03AM (#2723546)
Someone writing another book about programming C isn't newsworthy

However, "someone who wrote a ground-breaking introductory book in xyz modern-popular-hip language is turning his attention to C, of all things" is newsworthy.

Re:Learn C The Hard Way by speckledjim on Sunday July 03, 2011 @02:15PM (#2724186)
I had to stop reading at "ground breaking". I'll assume you're being sarcastic and leave it at that :)
Re:Learn C The Hard Way by thebooktocome on Sunday July 03, 2011 @07:07AM (#2723490)
I don't really understand your comment.

1. The post is about Zed's draft; is it really so far of a leap to interpret hp's remarks as a criticism of the book?

2. Is this a fair paraphrase? "Everyone here isn't attacking you, also, your draft sucks and shouldn't have been posted on HN."

Re:Learn C The Hard Way by brown9-2 on Sunday July 03, 2011 @10:00AM (#2723699)
I think hp is making more of a comment about the general state of C books rather than this one.
Re:Learn C The Hard Way by baha_man on Sunday July 03, 2011 @09:02AM (#2723613)
' Is this a fair paraphrase? "Everyone here isn't attacking you, also, your draft sucks and shouldn't have been posted on HN."'

No, it's not. speckledjim is complaining that someone writing another book on C is not news, he doesn't say anything about the draft sucking.

Re:Learn C The Hard Way by thebooktocome on Sunday July 03, 2011 @09:16AM (#2723635)
Implied by "Really though, there's no content here." Maybe they meant the comments, though.
Re:Learn C The Hard Way by TWAndrews on Monday July 04, 2011 @12:30AM (#2725371)
I think he's referring to this topic, rather than the draft.
Re:Learn C The Hard Way by speckledjim on Sunday July 03, 2011 @02:22PM (#2724216)
1. I agree 2. He didn't post it someone else did, and no, I don't think it should have been posted.
Re:Learn C The Hard Way by Goladus on Sunday July 03, 2011 @09:52AM (#2723687)
If you know a really good beginner-oriented C programming book that is not K&R I would love to see it.
Re:Learn C The Hard Way by nonrecursive on Sunday July 03, 2011 @11:27AM (#2723825)
Re:Learn C The Hard Way by there on Sunday July 03, 2011 @11:18AM (#2723810)
"practical c programming" is not bad: http://amzn.to/m6SOB5
Re:Learn C The Hard Way by hp on Sunday July 03, 2011 @01:24AM (#2723142)
I was just going off on a generic tangent, obviously I don't know what your book will be like. Just talking about C since it's 1am and vaguely on-topic.
Re:Learn C The Hard Way by Confusion on Sunday July 03, 2011 @04:13AM (#2723337)
He is not criticising you; he's criticising what others that undertook this kind of project have done.
Re:Learn C The Hard Way by brianmatter on Monday July 04, 2011 @05:27AM (#2725963)
hey, you don't know me or shite, but you're a fucking cool guy. probably the most generous dude i've ever not met. cheers!
Re:Learn C The Hard Way by bonch on Sunday July 03, 2011 @02:20PM (#2724208)
Hey, Zed, can you tell us all how much we're losers because we don't get as many blog post hits as you? The irony was a big hit last time.
Re:Learn C The Hard Way by jnburham on Sunday July 03, 2011 @09:39PM (#2725091)