What Are Compile Time Errors? Give Some Examples. How Are They Detected?
What Are Compile Time Errors? Give Some Examples. How Are They Detected?
Answer-
Compile time errors are detected at the time that the program is translated into machine
language. Some programming construct that the programmer has used is not recognised by the
compiler. This is usually because the programmer has either made a simple typing slip, or
because he or she has not completely understood the rules of the language as they apply to the
programming construct. In some sense, compile time errors are the easiest to correct, because
they always result in an error message. Unless the error is so severe that the compiler cannot
continue, the translation process will continue with the possibility of detecting further errors. It
is possible that the errors detected are not ``genuine'' errors, but consequences of earlier errors.
A simple example of this is the problems caused if there is an error in the declaration of a
variable. The compiler will not recognise the variable as such when it is used, and it will
complain about `` Undeclared variable''. In order to recover from this, many compilers make
some assumption about the variable and continue, so that other errors can be found. Typically
this will involve assuming that the variable has been declared of type int, and continuing. This
assumption may itself cause further compilation errors if an int variable is not acceptable.
A common cause of apparently correct programs causing compilation errors is a comment which
is not terminated correctly. Apart from the // comment which terminates at the end of the line,
C++ supports a comment that starts with /* and extends until a matching */ character pair.
Omitting the closing */ causes havoc! Similar problems occur with missing quotation marks on
strings " or characters '.
Typing errors in variable identifiers and keywords can be difficult to spot: watch out for
confusion between the letter l (el) and the digit 1 (one). Other characters that often cause
errors, particularly when transcribed from handwritten notes are z and 2, s and 5, and o and 0.
The compiler also distinguishes between upper and lower case letters. It is not good practice to
have identifiers which only differ in the case of some letters.
A good strategy for dealing with compile time errors is to identify the first error reported. Once
the first error is identified, usually either in the statement in which it is reported, or possibly the
previous statement, one can attempt to decide which errors are consequences of that error.
These can safely be ignored. Errors which cannot be explained in this way should be investigated
too. Once as many as possible of the errors have been corrected, the program should be
recompiled, and the cycle of error correction should be repeated.
Many compilers give warnings as well as error messages. These relate to unusual, but legal,
constructs in C++ programs.