PPL30 Day 25: C

Sometimes a review is as important, or perhaps more so, than learning a new thing. That is why the C programming language is on the PPL30 list. I’ve long felt that I did not so much learn C in college as I passed a class that happened to cover C. C intrigues me. C feels so close to the metal, as they say. It’s for that reason that C feels like a macho language. Once in a while it’s nice to take a step back from my fancy hipster language, Ruby, and play with a macho language like C.

Macho Man

This is part of the Peer Pressure Learning 30 series. To learn more about what in the hell that means, see the intro to the series, A Good Time To Learn.

Installation

You are on your own. You need gcc, or clang or something. If you’re on a UNIX, you’ve probably got it already. If you’re on Windows, please imagine me shrugging and looking confused.

Resources

I did a lot of Googling. Consequently, I might have reviewed a thing or two that does not get mentioned in the following list. I’m sorry. These are the ones that stuck out.

The C Programming Language

I have had K&R for years, though I’ve never finished it. (Also, due to an oversight on my part, I have the first edition – not the ANSI C version.) For some reason, I begin reading it every year, and then get distracted by something that is, of course, more important at the time. In any case, it’s a classic. If you ask a group of programmers where you should start with C, they’ll tell you K&R.

I used it lightly to review a few things.

HowStuffWorks: C

I am not joking; there is a How Stuff Works: The C Programming Language entry. It’s actually pretty decent, from what I’ve read thus far. I used it to review pointers and malloc(), and free(), mostly.

libebb

I thought that I should read through some C code, even if I had a hard time making sense of it. The first C program that comes to mind is Ebb, an app server for Ruby apps along the lines of Thin and Mongrel. Ebb is a Ruby binding to libebb, so, I thought I’d browse the libebb source. It looks like fairly straightforward work to me, but that’s not to say that I’ve got it all pinned down and I’m ready to write my own Ebb.

What I Think I Now Know

I think I remember and understand more than I thought I did! That is comforting. However silly, I found C intimidating when I was learning to program. Coming back around to it while feeling like I have a solid grasp of other languages has rendered it much less frightening.

For fun, I thought I’d continue the tradition of writing a FizzBuzz for the languages I cover.

<% coderay(:lang => :c, :line_numbers => :inline) do -%> #include #include

int main() { int i; for (i = 0; i <= 100; i++) { fizzbuzz(i); } }

int fizzbuzz(int num) { char fizzbuzz[9]; if (num % 3 == 0) { strcpy(fizzbuzz, “Fizz”); } else if (num % 5 == 0) { strcat(fizzbuzz, “Buzz”); } else { sprintf(fizzbuzz, “%d”, num); } printf(“%s\n”, fizzbuzz); } <% end %>

If you’re curious, and wish to run this yourself, but have no C experience: copy the code to a file named fizzbuzz.c. Then, in a terminal, in the directory that contains that file:

gcc fizzbuzz.c -o fizzbuzz
./fizzbuzz

This is assuming you have gcc installed, which you will if you’ve installed the developer tools on OSX, or you’re on a Linux distribution that isn’t a bit silly, or you type sudo apt-get install build-essential on Ubuntu. (Ubuntu is a bit silly, though I still use it on a machine or two.)

This has newly inpsired me to embark upon a journey that culminates in finishing K&R. It’s embarrassing to have owned it for so many years without finishing it. I hope that by increasing the embarrassment and shame by publicly disclosing that fact, I motivate myself to read the first page, last page, and even most of the pages in between.

This entry was the one that seemed most daunting 25 days ago. I don’t know what it is with me and C. In any case, I’m happy that it’s over, but just a little disappointed, as well.


It’s only fitting that we go from C to Objective C, right? [[[ That is what I’ll be covering tomorrow. ]]]