Where to begin?

I’m currently working on introductory material on programming in C. And this is the main question I’m facing: when introducing something new, how far back do you have to zoom out for everyone to understand the context?

Blank canvas #

My first approach is to start with nothing, just an empty program.c, and try to compile that into an executable file.

Errors #

Of course an empty file is not a valid C program, and the compiler will not hesitate to tell that to you with a big, fat error message.

Undefined symbols for architecture x86_64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Since we haven’t written anything, there really is no penalty in seeing this error, and we can then dig deeper and understand what it is that it’s trying to say. Which is - a C program has to have an entry point in the code, that tells the OS “start executing code from this location”. In the case for C, the entry point is defined by the “main()” function. And now you know how the OS interacts with your program without having written any code, just by reading and following up on an error message.

We fear the unexpected #

My hope is that this could introduce the notion of appreciating warnings and error message from the very start. Because if you start by copying some sort of code into your editor and then compiling it without much effort, this trains you to think that code just works. But as soon as you start writing your own code, lo and behold, you are inevitably introduced to an avalanche of unexpected error messages.

The keyword here is - unexpected. We don’t really want to be surprised, we like anticipating things, and my theory is that when you start by making lots of errors and find out why they happen, you can train yourself to better anticipate mistakes and correct them. And, most importantly, you then know why something is a mistake.

 
778
Kudos
 
778
Kudos

Now read this

Where to begin? Entry points

We began with a blank canvas. Even though an empty file is technically a valid C program (thanks hacker news!), still, we can’t get an executable without the linker failing. This is for good reason, the operating system requires a... Continue →