An Introduction to C Programming


TABLE OF CONTENTS

Introduction

This is a practical introduction to the C programming language. It is my belief that the best way to learn programming is to jump right in. We will skip the details and the oddities, and concentrate on concrete examples that do useful things. Some topics will be used before they have been properly defined, but you should be able to grasp the sense of what is going on, and you may expect the explanation to come shortly thereafter.

Back to TABLE OF CONTENTS.

Example Program: Hello World

Before you can do anything with C, you need to have access to a computer, have an account, know how to use an editor, and so on. I'm not going to touch these topics, but our first exercise will test whether you have mastered them on your own so that we can move past these details and get into C. Our goal in this first exercise is to do "whatever it takes" to create, compile, and run a "Hello, world!" program.

You will need to create a file something like that in hello.c.

On my (UNIX) system, I can run this program with the commands:

    cc hello.c
    a.out
  
which should cause the appearance of the message:
    Hello, world!
  
Do what you have to do until you can get this program to work on your system.

The "work" of the program is carried out by the PRINTF statement. The other statements in the program were not part of the original "algorithm" (say hello), but are details of the "implementation".

In particular, note how the program is prefaced with some strange INCLUDE statements. The "body" of the program begins with the MAIN statement followed by a left curly bracket, and ends with a right curly bracket.

Back to TABLE OF CONTENTS.

The Square Root Algorithm

The heart of programming is the implementation of algorithms. Our first algorithm is a method for approximating the square root of a nonnegative number. The algorithm can be regarded as a special case of Newton's method for nonlinear equations. It goes like this:

We've left some details sketchy here, but there's enough information to write down a program something like that in root.c.

The algorithm that we discussed earlier is clearly part of this program, although there is lots more going on. For now, perhaps, there are two interesting things to discuss about the implementation of the algorithm. The first is the FOR ( ) { statement, which we can think of, in this context, as simply specifying that the statements up to the matching right curly bracket should be repeated 10 times. The second thing to notice is the statement

        y = ( y + ( x / y ) ) / 2.0;
  
which corresponds to the averaging process of the algorithm. We simply want to note that the equals sign "=" does not represent equality, but rather assignment. The variable y on the left hand side is to be assigned the value on the right hand side. Make sure you understand this, so that a statement like "x = x + 1;" does not confuse you.

Another interesting thing about the equals sign is that it seems that it is used exactly in those statements we associate with the original algorithm. Now if the other statements aren't assigning values to a variable, what are they there for?

Notice the comment statements. In the simplest case, a comment statement is one which begins with the begin-comment-marker /* and ends with the end-comment-marker */. It is not necessary for the comment to fit on one line. In fact, as soon as the begin-comment-marker is seen, everything that follows in the file is assumed to be comments, until the matching end-comment-marker is encountered.

Another statement that is simple to understand is the input/output statement; such statements are used to allow the program to print out results or to accept new data as it is running. In this example, the PRINTF statements are used to let us know how the value of y is changing.

Finally, there are some variable declaration statements. In this case, they specify that x and y are the names of "real" or "floating point" numbers, while the variable named i is to be treated as an integer.

Back to TABLE OF CONTENTS.


Back to the home page.

Last revised on 18 January 2001.