Jump to: navigation, search

Chapter 0 - A gentle introduction to the C Programming Language

Prerequisites

To start learning the C Programming Language, you will need some tools: at the very least, you will need a text editor and an implementation of the C language, both of them installed and working. This tutorial assumes you have both of those. If you don't yet have a working setup, you need to solve that problem before continuing with this tutorial.

Your first program

We're going to start off very, very simply, with the simplest, shortest C program that can be written. Here, then, is the simplest.c program:

int main(void){return 0;}
simplest.c program explanation

This program does almost nothing. In fact, the only thing it does is indicate (very quietly) that it worked. It does this via the return statement, which we'll come to in good time.

The program may look a little intimidating, but we can make it less so by the use of whitespace (broadly, these are spaces, tabs, and newlines). Here is exactly the same program again, but this time written in a way that most people would consider a little more friendly:

int main(void)
{
  return 0;
}

And already we have, not a one-line program, but a four-line program. But now it's easier to read.

C programs consist of units that are known as functions. This program has one function, whose name is main. This is a very special function in C, because it is the entry point for the program -- the point at which execution begins. After some basic information about the kind of function we are dealing with, the function itself begins with the { and ends with the }.

Between the braces -- { and } -- lies the function's body, the set of instructions that will be executed when this program runs.

Our function body consists of just one line:

  return 0;

The return statement is used to cede control from this function to whatever it was that called the function. Since main is the entry point to the program, we can assume that the program's flow of control returns from main to the calling environment. The 0 simply means, in this instance, that everything worked just fine.

The semicolon after the 0 is a statement terminator. It tells your C implementation that this is the end of the statement. Until the semicolon is reached, the statement is not yet finished. This means that we are free, if we choose, to split the program up even further. We could, for example, write the return statement like this:

  return
  0
  ;

Such a simple line does not need this expansive treatment, but later on we will see longer statements, where splitting the statement over several lines will make the code easier to read. By requiring a statement terminator, C gives us the freedom to split statements in this way.

Let's take a closer look at that first line:

int main(void)

Every function has a name. This function's name is main.

Functions also have a return type. This function's return type is int. The word int is short for integer (a whole number). The return type indicates the kind of value that the function will make available to whatever it is that calls it. In the next chapter, we will see how C functions can call other C functions, and at that point the idea of a return value will become clearer.

Finally, functions have a parameter list. We can pass information to functions via the parameter list, but this is not a requirement. For functions that do not need to accept information in this way, the void keyword is used to indicate that no parameters are expected.

When we describe a function by giving its return type, its name, and its list of parameters, that description is known as a prototype. Thus, the function prototype for main is:

int main(void)

The following image is a call graph of our simple program:

This call graph shows all the functions that our main function calls. Since main doesn't call any functions (return is a keyword, not a function), it's a very simple call graph. This will begin to make more sense when you read Chapter 1.

Things to do

Learn how to use your compiler. To do this, break out the documentation, read it, and use the simplest.c program given above as an example program. You need to be able to:

  1. enter the program text into a text editor (not a word processor, unless you're very firm with it and insist that it saves files in text format);
  2. save the program into a file;
  3. tell your compiler to compile the file into an executable C program;
  4. run the program.

When you run the program, it will appear that nothing is happening (because we didn't program any significant behaviour into the the C source). But as long as you don't get an error message, you can assume it worked.

Then it's time to experiment. Try changing the name of the function from main to Main, save the change, and re-compile. What does the compiler tell you?

Change it back to main. Now try changing something else. No matter what change you make, you'll need to:

  1. make the change;
  2. save the file;
  3. re-compile;
  4. look at what the compiler is telling you;
  5. undo the change and save the file again, to fix everything.

Here are some suggested changes (to be tried one at a time!):

  1. remove the semicolon from the return statement
  2. change return to retune
  3. remove the 0
  4. remove the opening brace {
  5. remove the closing brace }
  6. move the return statement outside the function body
  7. remove the parentheses from int main(void)

In each case, the compiler will produce a diagnostic message telling you that something is wrong. Diagnostic messages don't always make it clear what is wrong, but they do tell you that something is wrong. This exercise will help you to learn the kinds of messages that crop up. Because, in each case, you know precisely what you changed, you'll begin to learn how to associate particular messages with particular kinds of errors. That way, when you make similar errors in the future (and you will - we all do!), you'll have a fighting chance of fixing them.

Summary

In this chapter, you learned that a C program consists of functions. A function is a group of instructions that can be thought of as a unit. Functions consist of a return type, a name, a parameter list, and a function body enclosed in braces. The special function main is the entry point to the program.

In the next chapter, we will see how C functions can call other C functions.

Progress

Terminology
  • function
  • main
  • parameter list
  • statement terminator
  • function prototype
Personal tools