Video Coming Soon...
02: Your First C Program
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.
Here's the code to use:
#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.
Building It
To build this code you need to run the compiler with all of these options:
cc -g -Wall -std=c23 ex1.c -o ex1
This configures the following options:
-g- Enable debug symbols, which allows you to debug the code.
-Wall- Turn on "all" warnings, which actually doesn't turn on all warnings and not even really the most useful ones, but it's a good start.
-pedantic- This set pedantic mode, which is about as annoying as a pedantic person, but it will help you catch a few more errors.
-std=c23- Set the C standard to the 2023 standard, also called C23.
ex1.c- This is your C code file.
-o ex1- Outputs (
-o) the result of compiling your code toex1.
But this is annoying so we'll create a small Makefile that simply has these options configured for you:
View Source file Makefile Only
CFLAGS=-Wall -g -pedantic -std=c23
Now to compile all of your code you use make and the name of the program you want. The make tool will know that to make ex1 it needs to find ex1.c, and run the compiler for you:
make ex1
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.hextensions 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 functionyou'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 anintand take two parameters: anintfor 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 abeginordoword 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. Theprintffunction 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.
Imposture Check
- Open the
ex1file in your text editor and change or delete random parts. Try compiling and running it to see what happens. - Print out five more lines of text or something more complex than "hello world."
- Run
man 3 printfand read about this function and many others. On Windows you can search man7.org. - 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.
- Can you write a description of what this code does?
- Can you re-create the code from that description if you're allowed to peek at the original when you get stuck?
- Do you feel like you "got lucky?" Why? You just did it again without having to refer to it that much. If you still do, then we'll do a lot more of this until you realize it's not "just luck."
WARNING: Never ever take these assessments to be grades that you can fail. This is a range of questions to get you to look at your abilities. If you answer these questions honestly you'll find that you are capable or that you need to do more work. If you're capable, then carry on. If you need to do more work, do the work. The only way to fail this is to give up.
Register for From Impostor to Programmer
Register today for the course and get the all currently available videos and lessons, plus all future modules for no extra charge.