I read the following documentation about compiler and interpreter somewhere :-
A compiler searches all the errors of a program and lists them. If the program is error free then it converts the code of program into machine code and then the program can be executed by separate commands.
An interpreter checks the errors of a program statement by statement. After checking one statement, it converts that statement into machine code and then executes that statement. The process continues until the last statement of program occurs.
My doubt came from the following code:
int main()
{
printf("hello")
scanf("%d",&j);
return 0;
}
I am using MINGW GCC
compiler. When I compile the above code following things happen:
Firstly I get the error
error: expected ';' before 'scanf()'
After I correct the above error then I get the second error
error: 'j' undeclared (first use in this function)
So I wanted to know that why both the errors are not listed at one time?
Compiler and interpreters are technically two different things, though the boundaries can be pretty fluid sometimes.
A compiler is basically nothing more than a language translator. It takes a source language as input and generates a destination language as output.
An interpreter takes a language (be it high-level or low-level) and executes the code described by the language.
The confusion is mostly because most modern script languages contains both a compiler and an interpreter, where the compiler takes the script language and creates a lower-level equivalent (similar to binary machine language) that the interpreter then reads and executes.
As for your problems with the compiler errors, it's most likely because the compiler can't continue parsing the scanf
call due to the first error, and simply skips it (including the undeclared variable).
You should also know that in C some errors can actually cause more errors in code that is otherwise correct for example
int j
printf("Enter something: ");
scanf("%d", &j);
You will get an error because of the missing semicolon after the declaration of the variable j
, but you will also get an error with the scanf
line as the compiler can't find the variable j
, even though the scanf
call is otherwise correct.
Another typical example of errors that will give follow-up errors in unrelated code, is to forget the terminating semicolon of a structure in a header file. If it's the last structure you might not even get any error in the header file, just unrelated errors in the source file you include the header file in.