Errors and the C Computer Language
Part 23 of the Complete C Course
Foreword: Errors and the C Computer Language
By: Chrysanthus Date Published: 11 Jun 2024
Introduction
Syntax Error
This is the wrong use of syntax. These errors are wrong expressions and wrong statements. When a statement is typed, which is wrong, that is a syntax error. Such a statement cannot be executed. For example, a programmer can mistakenly define an identifier, without a preceding object type (such as int). Under this condition, the program will not be compiled, and the executable form of it will never be obtained. During the attempted compilation, indication of the error or errors will be displayed on the screen. The line number (counting text lines from the top of the source code file) of the syntax error, may also be displayed on the screen.
Logic Errors
In this case, C understands the program very well; the program is compiled and executed. However, the program will not do what the programmer wanted it to do. It will do something slightly different or completely different. The fault is that of the programmer. For example, a loop that is required to do 10 iterations might do 5 iterations, because the programmer coded it mistakenly to do 5 iterations. Another example is that a loop might iterate infinitely, because the condition the programmer gave for the loop, made it that way. Logic Errors occur when the program is being executed. The only way to solve this problem is to test the program very well, before handing it to the customer (who asked for it).
Runtime Errors
Runtime errors occur when the program is being executed, as a result of the fact that the programmer did not take a certain factor into consideration when coding. For example, assume that the code is to divide 8 by some denominator that the user inputs. If the user inputs 2, the division will work, giving 4 as answer. If the user inputs zero, the division will not work, because 8/0 is undefined. When a runtime error occurs, the program normally crashes (and stops). To solve runtime errors, the programmer has to write extra code that will prevent the execution of the particular code segment from taking place, under certain conditions. In this division example, code has to be written, that will prevent division by zero from taking place, and possibly informing the user of the mistake he/she made by inputting zero as a denominator.
Preventing Runtime Errors
The following code illustrates how to prevent the above error (division by zero).
#include <stdio.h>
int main(int argc, char *argv[])
{
int numerator = 8;
int denominator = 2;
if (denominator != 0 )
{
int answer = numerator/denominator;
printf("%i\n", answer);
}
else
{
printf("Division by zero is not allowed!\n");
}
return 0;
}
The particular code segment here is that of the if-block. The reader should read and test the above code if he/she has not already done so. The code should be self-explanatory. Change the value of the denominator from 2 to 0 and test the code again. This time, the output should be "Division by zero is not allowed!". The code should be self-explanatory.
Well, something is still lacking. When an error occurs, the program should report the error with an output message and stop. The program should not execute the error code segment. To achieve these, use the "exit(0);" expression statement from the C stdlib library. The following program illustrates this:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int numerator = 8;
int denominator = 0;
if (denominator != 0 )
{
int answer = numerator/denominator;
printf("%i\n", answer);
}
else
{
printf("Division by zero is not allowed!\n");
exit(0);
}
return 0;
}
The else part of the if-construct has the "exit(0);" . The stdlib.h header file was included.