Essay-writing [entries|reading|network|archive]

[ userinfo | dreamwidth userinfo ]
[ archive | journal archive ]

Mon 2014-01-27 18:42

A few months ago I was talking to a friend who was intending to learn C, having previously only worked with higher-level languages. We had a conversation about the assorted (and mostly painful) culture shocks that C is liable to deliver to someone whose previous experience of programming is rather less user-hostile, and afterwards I thought, hmm, that turned out to be quite a lot of potentially useful and more or less organised information, perhaps I should write it up.

It's taken me a few months to get round to it and to fill in the last few pieces, but it's now done. In case anyone reading this finds it useful, or knows anyone else who would, I present

This is a bit of a departure from the usual kind of article I write and put on the web. It's about the only thing I've written that's purely educational in nature, in the sense that it rehashes material well known to many for the benefit of those who don't already know it, rather than presenting stuff I've invented myself and don't expect anyone to already know. For some reason that makes me feel noticeably more nervous about its reception (perhaps because there's scope to be vigorously criticised by both its intended audience and people who already know the material and think I've got it wrong). But I hope someone finds it useful, anyway!

[xpost |]

[personal profile] sunflowerinrainSun 2014-02-02 18:41


I was dropped into C on a big project for which I'd been sent on a C++ course (just as the work started, some big boss decided that C was better). It was indeed a shock. This would have been so useful!

Link Reply to this
[identity profile] feanelwa.livejournal.comMon 2014-01-27 20:31

I almost certainly will find it useful the next time I need to do some C and have forgotten the thing about pointers and arrays yet again!

Link Reply to this
[identity profile] geekette8.livejournal.comMon 2014-01-27 21:28

That looks pretty good to me.

I teach a C course for people who are assumed to have some knowledge of another high level language already (although unlike your article it *does* attempt to teach the nuts and bolts of syntax) and I shall link to your article from it, if you don't mind.

One of the things I keep repeating throughout the course is "the compiler is cleverer than you are". In my experience most of the people who have heard a bit about C, usually want to switch from Python / Java / whatever to C because they think they will be able to write faster code. They then start micro-optimising random bits of stuff (manually unrolling loops, or whatever) and it's nice to be able to show them - look, here's the assembly generated by this original thing, with varying levels of optimisation, and here's the assembly generated by your carefully hand-crafted thing, and LOOK! The compiler did better than you did!

Of course after multiple iterations of "the compiler is cleverer than you are" we then come to "... and actually the compiler isn't very clever at all" (for example with your overflow / undefined behaviour example) and then also "and sometimes the compiler is just plain wrong" (on some obscure embedded target with a gcc port done by an intern who'd never even seen or heard of the dragon book).

Link Reply to this | Thread
[personal profile] simontMon 2014-01-27 22:05

and I shall link to your article from it, if you don't mind.

Of course; feel free!

and LOOK! The compiler did better than you did!

Heh, yes :-) Though on the other hand my general experience is that it is usually possible to find something the compiler could have done better – provided you have the compiler's own output to start from and try to improve on. I don't doubt that starting from scratch it's much harder to beat the compiler, though...

"... and actually the compiler isn't very clever at all" (for example with your overflow / undefined behaviour example)

I think the problem there isn't that the compiler is not clever, it's that it has great cleverness which it's misapplying. The fact that some apparently perfectly sensible piece of code that people might reasonably want to write constitutes undefined behaviour by chapter and verse of the C standard is being taken by the compiler developers to mean "and therefore we can do whatever we like", where it ought to be being taken to mean "and therefore chapter and verse of the C standard is not the sole metric of sensible compiler behaviour"!

Link Reply to this | Parent
[personal profile] pm215Mon 2014-01-27 23:40

Object orientation is pretty standard these days: most modern languages have it in some form. C does not, because it predates it.

Technically speaking I think Simula 67 predates C by a couple of years...
Link Reply to this | Thread
[personal profile] simontTue 2014-01-28 09:07

Wording tweaked, thank you :-)

Link Reply to this | Parent
[identity profile] cartesiandaemon.livejournal.comMon 2014-01-27 23:58

Oh cool, yeah, that seems a pretty good idea.

Link Reply to this
[personal profile] andrewduckerTue 2014-01-28 08:20

Seems to have done rather well - it's shown up twice in my RSS feeds this morning :->

Link Reply to this
[identity profile] sesquipedality.livejournal.comTue 2014-01-28 13:19

This emphasises to me how out of date I am - that this should be esoteric forbidden knowledge. Which is not to say I feel superior - I have no idea how to write java and python, and this is regrettable.

Link Reply to this
[identity profile] azekeil.livejournal.comTue 2014-01-28 19:23

Very helpful :)

As someone who has dabbled in other languages but doesn't really understand C, could I suggest you extend the article with 'next steps' that help explain things like how to link against other code and call that code, and information on where to get (standard) library information.. I know there's man pages but I'm thinking more overview-y. Perhaps I just don't 'get' it enough.. my main background is perl if that puts my comments into some context :)

Link Reply to this
[identity profile] mobbsy.livejournal.comSun 2014-02-09 19:27

Very nicely done. I've been throwing this around a little at work in the past couple of weeks, and people have been very complimentary about it.

One of the contexts was I overheard somebody telling a couple of new grads that they should learn C, so I thought it an ideal test case. I will report the findings back to you.

(I find it a little scary that new grads don't come as fully formed C producing machines that just need increasing amounts of pedantry, experience and "oh ghods, you did *what*" code reviews inflicted on them.)

Link Reply to this
[ go | Previous Entry | Next Entry ]
[ add | to Memories ]