Some thoughts on C

I’ve recently started learning C after spending a good few months with Python. C is certainly a whole new ball game, there are some things that I love and some things that I’d rather do without. Here’s a quick rundown of some of the things that have made an impression on me:

  1. C feels more formal. Every programming language has a distinct ‘feel’ which you start to understand once you’ve used a fair number of them. Perl feels snappy and lightweight. Python feels quick and flexible, but there’s also this feeling of underlying power. Java feels heavy, rigid and structured, sort of like the Empire State Building. Scheme has this very mystical, arcane, almost magical feel to it. C feels formal and structured like Java, but less heavy. It certainly feels more formal than Python and compared to Scheme, actually feels kinda boring, but that may just be me.
  2. C requires more patience. Since C is purely compiled, it means that you have to wait around for everything to compile before you can get to fixing errors and this can take a lot of time if your program stretches over more than a few hundred lines. The wait is even longer if you’re running after logical errors that only show up at runtime. The multiple edit-compile-run cycles can become very frustrating, especially if you’re used to the zippiness of interpreted languages.
  3. With great power comes great responsibility. For people like me who’s first encounter with programming didn’t involve manual memory management, C opens up a realm of possibilities thanks to it’s pointers which make it very easy to directly access system memory. While direct access to memory lets you write a lot of fast, powerful code, it also brings with the worst of every systems programmers’ nightmares: memory leaks, buffer overruns, accidentally wiping parts of the memory you weren’t even supposed to know existed, the list goes on. While languages like Python, Perl and even Java would happily clean up after, there’s nothing like that in C (unless you want to write your own garbage collection routines, but that’s a whole new ball game). Programming in C certainly makes you a more careful coder.
  4. You learn more than just programming. A lot of code is written in C, including a lot of very high quality open source code. If you decide to really learn C and use it for large scale projects, you probably won’t be able to avoid learning about Makefiles, version controls, the intricacies and optimizations offered by the various C compilers, and of course a rather frugal attitude towards memory management.
  5. The text editor is your best friend. There are lots of C IDEs out there, though many of them are very good, there’s no actual standard or default and in most of the ones you try, you’ll probably find a feature or two that you want but don’t have. Though I like IDE’s myself (DrScheme for Scheme, Eric3 for Python, BlueJ for Java), I’ve decided to stick with Vim, a really nice C plugin and the command line for my C programming. Many open source programmers find this sort of setup the best deal, especially if you start using makefiles. This probably isn’t C specific, because ideally you can use this system for most languages, but C sort of rubs it in.

That’s all for today. Next we take a look at using the Vim editor for C programming.

10 thoughts on “Some thoughts on C

  1. C, like any other language, is all about styles and idioms. Learn them and you won’t have ‘nightmares’ any more.

  2. I’m jumping from Java back into C and C++, and it feels just…cool. Professional, quick, and unforgiving, C not only presumes you know what you’re doing, it DEMANDS you know what you’re doing.

    My advice: Compile with your warnings set as high as they’ll go.

  3. Though I am not commenting directly on the main point of your post, but I just thought I comment anyway. I don’t know Scheme and I can’t argue with your Python’s choice of IDE, but for Java you favor BlueJ over Eclipse and NetBeans? I thought those two are almost ‘de facto’ IDEs of the Java world, although I hear from time to time people also use IntelliJ and jEdit. There must be some super feature of BlueJ. Anyway, I think having choices is great and this is not meant to be a negative comment.

  4. “nothing like that in C (unless you want to write your own garbage collection routines, but that’s a whole new ball game)”

    “If you want to try GC in an existing application, manually editing the source code to change mallocs and frees is not necessary. In order to redirect those calls to the GC version, you basically have three options: using a macro, modifying the malloc hooks and overriding glibc’s malloc() with libgc’s malloc(). The first approach is the easiest one… ”

    Garbage Collection in C Programs

  5. @Harold: I agree completely. Compiling with high warnings is a good idea, especially if you’re just starting off.

    @Kevin: I know about Eclipse/NetBeans, but I think BlueJ is a better choice for learners and people doing small projects because it’s less complicated. Of course if I ever get to the point of doing a huge project, I’ll probably use Eclipse.

    @Isaac: Thanks a lot for those two. Very interesting and informative.

  6. I agree with Orlando about styles and idioms: most C difficulties are solved this way.

    In particular — prefer C++ and its standard library over re-inventing the wheel with C!

    The memory issue is over-rated. What you describe is the use of malloc() and free() which, in general, is never used by C++ coders.

    I wrote a post about this in reverse, i.e. some thoughts about Python from a C perspective:

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s