Contents Top The C FrameworkStarting Out Contents

Starting Out

A word of warning: The C programming language has a fearsome reputation as a difficult language to learn and correctly program. This reputation is rather undeserved. C is one of the easiest languages in which to program; the areas that give some hackers and newbies fits are easily mastered by following a few simple rules. The purpose of this text is to elucidate those rules.

Most mortals cannot learn how to program by reading books. The same is true for riding a bike or playing the piano. They learn how to program (or ride a bike or play the piano) by practicing. Assuming you are like most mortals, you will need to practice programming as well. Therefore, to start, you will need to install a C compiler and an editor.

We will assume that you are using the editor and C compiler on one of three basic types of systems, a Windows-based system that has Cygwin installed, a Mac system, or a Debian-based system such as Ubuntu (a Unix system). Instructions for ensuring you have an editor and C compiler installed on each of these three types of systems can be found at http://troll.cs.ua.edu/cs100/install.html

Once the installation process completes, type the command:

    gcc --help

and press the <Enter> key. You should be rewarded with lots of information on how to use gcc. This will also confirm that your system is configured properly and you are able to compile C programs.

The edit-compile-test cycle

Writing computer programs involves three tasks, identified below.

  1. The developer (you) enters what is called source code into a file on the system. Source code is a text version of a computer program that is readable (hopefully) by humans. The program that you use to enter the source code text into a file is called an editor.
  2. A compiler then turns the human readable version of a program into a form that is readable by the computer. The computer readable form is called an executable. The compiler is simply another program that exists on the computer. Each programming language (e.g., C, C++, Python, Java, Pascal, FORTRAN) has its own compiler (program) to convert source code into an executable.
  3. Once the compiler has created the executable, you must ensure that the code that you entered actually performs the required task. You ask the computer to run the program to ensure that it is functioning properly.

At this point, you should realize that things can go wrong during the second and third steps. If you made mistakes when entering the source code, the compiler will discover these errors (during step two). When this happens, the compiler prints some ominous messages and quits. These errors are known as compilation or syntax errors. Misspellings, errors in spacing, and missing symbols at specific locations are just a few of the possible syntax errors that a compiler will identify. Upon seeing the error messages produced by a compiler, the program writer (you) must go back to editing the source code to remedy the situation.

Once the compiler is able to successfully translate your source code into an executable (no syntax errors exist), the program can be run and tested. Sometimes errors occur at this stage. For example, the program might crash, loop forever, freeze, or produce an incorrect output. These kinds of errors are known as run-time or logic errors. These errors are due to the fact that the set of instructions (statements) that you entered in your program (the source code) are not logically coherent. Think of this as giving the wrong directions to someone trying to find Bryant-Denny Stadium. Where they end up is not what you intended. Again, the program writer (you) must go back to editing the source code to fix the problems.

Often, in the course of fixing syntax errors, the program writer introduces logic errors. Just as often, attempts to fix logic errors introduce syntax errors. And so it goes. If you are not a patient person and are easily frustrated, learning how to program will have some very unpleasant moments. On the other hand, impatience and intolerance of setbacks are not widely admired traits; sticking with programming will most assuredly cure you of these personality defects.

If all is copacetic at this point, you are ready to proceed to next chapter.

Activities

Note: all activities in assume a Unix-style system, either Linux, Mac OSX, or Cygwin on Windows.

name purpose
vim editing with vim
directories creating and using directories
absolute and relative paths moving around a Unix-like system

lusth@cs.ua.edu


Contents Top The C FrameworkStarting Out Contents