Video Coming Soon...

Created by Zed A. Shaw Updated 2026-11-26 20:48:52

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:

View Source file ex2.c Only

#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 to ex1.

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:

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

  1. Open the ex1 file in your text editor and change or delete random parts. Try compiling and running it to see what happens.
  2. Print out five more lines of text or something more complex than "hello world."
  3. Run man 3 printf and read about this function and many others. On Windows you can search man7.org.
  4. 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.
  5. Can you write a description of what this code does?
  6. Can you re-create the code from that description if you're allowed to peek at the original when you get stuck?
  7. 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.

Previous Lesson Next Lesson

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.