James Learns C: Day 1

As I grow as a computer scientist, the less I feel like a computer scientist. I would like to change that by becoming truly hardcore and learning C.

This will be my journey.

A primer on my tools. I will be using Mac OS X 10.9 Mavericks. I will be using the Mac C tools that ship with OS X - the ones that come with XCode. My text editor will be vim.

I will start there - if I find I need more tools I will seek them out when such a need arises.

mkdir james_learns_c, cd james_learns_c, vim 1-hello-world.c

I will begin with the canonical hello world! app, but I will be approaching it differently. In order to extract as much meaning and information from this journey I will be diving as deep as I can into each construct that I use. This could turn into quite the rabbit hole.

#include stdio.h

int main() {
  printf("hello world!\n");
  return 0;

I compile this by typing gcc 1-hello-world.c.

I want to know what actually happens here. I know that the C language is compiled down into machine language. Machine language is the ‘code’ that the processor can understand. Here’s some machine code:

   6      5     5     5     5      6 bits
[  op  |  rs |  rt |  rd |shamt| funct]  R-type
[  op  |  rs |  rt | address/immediate]  I-type
[  op  |        target address        ]  J-type

Man, sure wish I knew what that does. For another day.

This is not the same as assembly language, which is another layer on top of machine language. These are both processor specific, as different processors have different rules.

Right, compiling. The compiler takes this .c file and turns it into machine code. There’s an ordering here, though. #include <stdio.h> is read first. This line is read by the ‘preprocessor’. The preprocessor looks for ‘directives’ that start with the #. In this case, it reads this file name and compiles it FIRST. The compiler eats a .c file and outputs a .o file. If there are multiple .c files, the compiler compiles them all into .o files. Then, a linker collects all .o ‘object’ files and creates the final program. Decompiling is hard! I need to go to sleep now.

I didn’t compile the program yet. But I’ve learned so much!


Now read this

Distributed Hash Tables: Introduction

This post assumes you have ~ 1 or more years of programming or Computer Science experience. I’d say if you’re comfortable with hash functions & some basic network ideas, there’s something here for you. This series of posts is... Continue →