Node:Errors, Next:Questions for Chapter 2, Previous:File names, Up:Using a compiler
Errors
Errors are mistakes that programmers make in their code. There are two main kinds of errors.
- Compile-time errors are errors caught by the compiler.
They can be syntax errors, such as typing fro
instead of for, or they can be errors caused by the incorrect
construction of your program. For example, you might tell the compiler
that a certain variable is an integer, then attempt to give it a
non-integer value such as 5.23. (See Type errors.)
The compiler lists all compile-time errors at once, with the line number at which each error occurred in the source code, and a message that explains what went wrong.
For example, suppose that, in your file
eg.c
you writey = sin (x];
instead of
y = sin (x);
(By the way, this is an example of assignment. With the equals sign (
=
), you are assigning the variabley
(causing the variabley
to contain) the sine of the variablex
. This is somewhat different from the way equals signs work in mathematics. In math, an equals sign indicates that the numbers and variables on either side of it are already equal; in C, an equals sign makes things equal. Sometimes it is useful to think of the equals sign as an abbreviation for the phrase "becomes the value of".)Ignore the syntactic details of the statements above for now, except to note that closing the
(x)
with a square bracket instead of a parenthesis is an error in C. Upon compilation, you will see something like this error message:error-->
eg.c: In function `main': eg.c:8: parse error before `]'
(If you compile the program within Emacs, you can jump directly to the error. We will discuss this feature later. See Debugging, for more information.)
A program with compile-time errors will cause the compiler to halt, and will not produce an executable. However, the compiler will check the syntax up to the last line of your source code before stopping, and it is common for a single real error, even something as simple as a missing parenthesis, to result in a huge and confusing list of nonexistent "errors" from the compiler. This can be shocking and disheartening to novices, but you'll get used to it with experience. (We will provide an example later in the book. See Debugging.)
As a rule, the best way to approach this kind of problem is to look for the first error, fix that, and then recompile. You will soon come to recognize when subsequent error messages are due to independent problems and when they are due to a cascade.
- Run-time errors are errors that occur in a compiled and running
program, sometimes long after it has been compiled.
One kind of run-time error happens when you write a running program that does not do what you intend. For example, you intend to send a letter to all drivers whose licenses will expire in June, but instead, you send a letter to all drivers whose licenses will ever expire.
Another kind of run-time error can cause your program to crash, or quit abruptly. For example, you may tell the computer to examine a part of its memory that doesn't exist, or to divide some variable by zero. Fortunately, the GNU environment is extremely stable, and very little will occur other than an error message in your terminal window when you crash a program you are writing under GNU.
If the compilation of a program is successful, then a new executable file is created.
When a programmer wishes to make alterations and corrections to a C program, these must be made in the source code, using a text editor; after making the changes, the programmer must recompile the program, or its salient parts.