Learn C the Hard Way: Dust Off That Compiler
- Breaking It Down
- What You Should See
- How to Break It
- Extra Credit
After you have everything installed, you need to confirm that your compiler works. The easiest way to do that is to write a C program. Since you should already know at least one programming language, I believe you can start with a small but extensive example.
ex1.c
1 #include <stdio.h>
2
3 /* This is a comment. */
4 int main(int argc, char *argv[])
5 {
6 int distance = 100;
7
8 // this is also a comment
9 printf("You are %d miles away.\n", distance);
10
11 return 0;
12 }
If you have problems getting the code up and running, watch the video for this exercise to see me do it first.
Breaking It Down
There are a few features of the C language in this code that you might or might not have figured out while you were typing it. I’ll break this down, line by line, quickly, and then we can do exercises to understand each part better. Don’t worry if you don’t understand everything in this breakdown. I am simply giving you a quick dive into C and promise you will learn all of these concepts later in the book.
Here’s a line-by-line description of the code:
- ex1.c:1 An include, and it is the way to import the contents of one file into this source file. C has a convention of using .h extensions for header files, which contain lists of functions to use in your program.
- ex1.c:3 This is a multiline comment, and you could put as many lines of text between the opening /* and closing */ characters as you want.
- ex1.c:4 A more complex version of the main function you’ve been using so far. How C programs work is that the operating system loads your program, and then it runs the function named main. For the function to be totally complete it needs to return an int and take two parameters: an int for the argument count and an array of char * strings for the arguments. Did that just fly over your head? Don’t worry, we’ll cover this soon.
- ex1.c:5 To start the body of any function, you write a { character that indicates the beginning of a block. In Python, you just did a : and indented. In other languages, you might have a begin or do word to start.
- ex1.c:6 A variable declaration and assignment at the same time. This is how you create a variable, with the syntax type name = value;. In C, statements (except for logic) end in a ; (semicolon) character.
- ex1.c:8 Another kind of comment. It works like in Python or Ruby, where the comment starts at the // and goes until the end of the line.
- ex1.c:9 A call to your old friend printf. Like in many languages, function calls work with the syntax name(arg1, arg2); and can have no arguments or any number of them. The printf function is actually kind of weird in that it can take multiple arguments. You’ll see that later.
- ex1.c:11 A return from the main function that gives the operating system (OS) your exit value. You may not be familiar with how UNIX software uses return codes, so we’ll cover that as well.
- ex1.c:12 Finally, we end the main function with a closing brace } character, and that’s the end of the program.
There’s a lot of information in this breakdown, so study it line by line and make sure you at least have a grasp of what’s going on. You won’t know everything, but you can probably guess before we continue.