Trying out C
Learning C has been on my list for some time now and for the last few weeks I’ve been using an excellent guide to learn more about C. That guide is Beej’s Guide to C Programming, which I stumbled across when looking at Build your own Redis with C/C++. The Guide has been an incredibly useful walkthrough of C, starting with the basics and working your way up. I’m about half way through the guide (I’m learning about Macros now) and so far each chapter has been great. Each example in the book I’ve been typing out and compiling using llvm since I’m on a Mac. Everything has compiled and given me more context on how C works.
Since I’m not all the way through the guide yet and I want to keep this short, I’ll share just a few things I’ve learned so far:
- Go’s Atoi in strconv seems to have come directly from C’s atoi.
- macOS uses Clang, which aliases gcc by default
- bool wasn’t added until C99, you’d use 0 for false and everything else was true. You have to include the stdbool.h header to use it.
- Everything passed into a function is copied, but if you have a pointer that points to a pointer, well you still are pointing to the same thing.
- Use void when your function takes no parameters to explicitly state that and avoid issues with function prototypes.
Lastly, Pointers. I’m still working through a lot on them but its been fun so far! It is interesting that Arrays and Pointers seem to be essentially the same thing? Like how in a main call you can have these two ways to refer to the arguments passed in.
int main(int argc, char[] *argv) { }
and the pointer way:
int main(int argc, char **argv) { }
Once I’m done with Beej’s Guide to C Programming, I’ll pick back up on Crafting Interpreters and work through the second half which uses C to implement a bytecode virtual machine.