Sample Video Frame
Exercise 1: Dust Off That Compiler
2024 Note: I mention a "PDF" in the video but that's not available anymore. Also, your error output might be different as compilers may have improved their error detection.
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.
#include <stdio.h>
/* This is a comment. */
int main(int argc, char *argv[])
{
int distance = 100;
// this is also a comment
printf("You are %d miles away.\n", distance);
return 0;
}
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 may 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 multi line
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 namedmain
. For the function to be totally complete it needs to return anint
and take two parameters: anint
for the argument count and an array ofchar *
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 abegin
ordo
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 syntaxname(arg1, arg2);
and can have no arguments or any number of them. Theprintf
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.
What You Should See
You can put this into an ex1.c
and then run the commands shown here in this sample shell output. If you're not sure how this works, watch the video that goes with this exercise to see me do it.
View Source file ex1.sh-session Only
$ make ex1
cc -Wall -g ex1.c -o ex1
$ ./ex1
You are 100 miles away.
$
The first command make
is a tool that knows how to build C programs (and many others). When you run it and give it ex1
you are telling make
to look for the ex1.c
file, run the compiler to build it, and leave the results in a file named ex1
. This ex1
file is an executable that you can run with ./ex1
, which outputs your results.
How to Break It
In this book, I'm going to have a small section for each program teaching you how to break the program if it's possible. I'll have you do odd things to the programs, run them in weird ways, or change code so that you can see crashes and compiler errors.
For this program, simply try removing things at random and still get it to compile. Just make a guess at what you can remove, recompile it, and then see what you get for an error.
Extra Credit
Open the
ex1
file in your text editor and change or delete random parts. Try running it and see what happens.Print out five more lines of text or something more complex than "hello world."
Run
man 3 printf
and read about this function and many others.For each line, write out the symbols you don't understand and see if you can guess what they mean. Write a little chart on paper with your guess so you can check it later to see if you got it right.
Register for Learn C the Hard Way
Register today for the course and get the all currently available videos and lessons, plus all future modules for no extra charge.