Overview
In this session, we will be dealing with troubleshooting compiler errors. Compilation issues can be very difficult in the beginning.
So make sure that you have a reference program not written by you, until you are comfortable with the syntax in general.
Initial compiler errors should never let you down. You may not be able to resolve it in a day, it can be upsetting, but keep on trying.
Make sure you follow my demos when you deal with Visual Studio. Visual Studio is too big.
It can be challenging if you start trying your own things in the beginning.
Few tips to avoid compiler errors:
-
Never delete the default #include "stdafx.h" unless instructed.
-
Always start with building a 'hello world' application to make sure the environment is correct and always
'Build' the program created by right-clicking on the project and start from a successful hello world.
-
Check your code very carefully for the below items for any kind of compiler errors your face, as below items are very common for beginners.
Also below reasons will force the compiler to point some wrong line as errors.
Missing matching " { " and " } ", for curly braces.
Missing matching "(" and ")", for parenthesis.
Missing ";", semicolon. Each statement in C programming should end with a semicolon except keywords like 'while' 'if'.
-
The variables and functions we use should be declared before, either in a header file or in your program. Otherwise,
the compiler won't be knowing those things and will complain about them.
-
Always remember, C language is "Case Sensitive". So variable 'a' and 'A' are treated differently.
Let see a Demo now:
Create a new project in Visual Studio.
A
new project in Visual Studio.
Select 'Win32' under 'Visual C++',
and then select 'Win32console'. Name your project, say 'comperrors' in this case and click 'Ok'
Click 'Finish' in the wizard window.
The output of the wizard.
Now remove ; (semicolon) in the statement 'return 0' and build the program.
Removing semicolon after return 0 and building the program.
#include "stdafx.h"
#include <stdio.h>
int _tmain(int argc, _TCHAR* argv[])
{
return 0
}
Now, have a look at the output window.
The box at the bottom of the screen is the 'Output Window'.
In the output window, you will get all the compilation errors, if any.
In this case, the output window shows, 0 succeeded and 1 failed, which means our build failed.
The output window displaying build failed
When we build the project, Visual Studio starts compiling files one by one.
When Visual Studio starts compiling our project file 'comperrors.cpp', it shows error.
The error we got.
Now we will have a look at the error we got.
e:\myprojects\comperrors\comperrors.cpp(10) : error C2143: syntax error : missing ';' before '}'.
"e:\myprojects\" is the location of our project.
"comperrors\" is our project folder.
"comperrors.cpp" is our project file.
(10) represents the line in which error is present, which means the error is in the 10th line of the program.
"error C2143: syntax error:" shows the type of error present.
"missing ';' before '}' " explains the error.
This error is pretty straightforward. We will understand the error present just by reading this.
Now double click on the error.
Double clicking on the error.
By double-clicking on the error we will go straight to the source code.
If we look carefully we can see an arrow mark pointing the line number 10.
Shows an arrow pointing line 10.
How did we know it is line number 10? At the bottom of the output window, we can see the line number where the arrow appears.
The line number at the bottom of the output window.
Putting a semicolon in line number 10.
#include "stdafx.h"
#include <stdio.h>
int _tmain(int argc, _TCHAR* argv[])
{
return 0
;}
So put a semicolon at line 10 and build the project once again.
Building the project after putting a ';' in line 10.
In the output window of the above fig4-14, we can see that our project is built successfully.
In this case, the error was easy to understand and solve.
But it won't be the case always. We can have a look at what happens when we remove the closing curly braces "}" in the application.
Removing the closing curly braces from the application.
#include "stdafx.h"
#include <stdio.h>
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
Building the project by removing the closing "}".
In the output window of the above code, we can see the status of our application build as failed.
The error we got.
Now let us have a look at the error.
e: \myprojects\comperrors\comperrors\comperrors.cpp(12) : fatal error C1075 end of file found before the left brace ‘{‘
at ‘e:\my myprojects\comperrors\comperrors\comerrors.cpp(8) ‘ was matched.
This is more complicated than the previous one.
“e: \myprojects\comperrors” shows the location of our project.
“comperrors” is our project folder.
“comperrors.cpp” is our project file.
“fatal error C1075” shows the error we got.
“end of file found before the left brace ‘{‘ at ‘e:\my myprojects\comperrors\comperrors\comerrors.cpp(8)
‘ was matched”
is the explanation of the error.
The compiler is trying to tell us that there is an open brace at line 8 and the matching closing brace
is not found.
Now double click on the error.
Double clicking on the error and an arrow appearing at line 12.
Click on the open brace and in the bottom of the output window you can see line 8.
Clicking on the open brace and the bottom of the output window showing line 8.
Close the curly braces and build the program again.
#include "stdafx.h"
#include <stdio.h>
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
Closing the curly braces and building the program.
We can see in the output window that, our project is compiled successfully.
Now, say for example, add a statement “a=10” and build the project. We have not declared what is ‘a’.
Adding a new line ‘a=10’ and building the project.
#include "stdafx.h"
#include <stdio.h>
int _tmain(int argc, _TCHAR* argv[])
{
a = 10;
return 0;
}
The build failed. Now have a look at the error we got.
The error we got.
The error says “a: undeclared identifier” which means that we haven’t told the compiler what is ‘a’.
So, we can declare ‘a’ by adding a statement “int a;”. This tells the compiler that ‘a’ is an integer.
Declaring ‘a’ and building the program.
#include "stdafx.h"
#include <stdio.h>
int _tmain(int argc, _TCHAR* argv[])
{
int a;
a = 10;
return 0;
}
Now build the program. We can see that the compiler is happy and our project got compiled without any error.
Our build is successful.
What happens if we give ‘A’ instead of ‘a’?
Giving ‘A’ instead of ‘a’ and building the program.
The error we got.
Again, same thing happens. Compiler complains ‘a’ is undeclared.
Remember we told earlier that C program is case sensitive. Uppercase A is entirely different from ‘a’ and compiler shows error.
Removing an opening parenthesis from the ‘main’ function.
#include "stdafx.h"
#include <stdio.h>
int _tmainint argc, _TCHAR* argv[])
{
int a;
a = 10;
return 0;
}
Now build the project.
The errors we got.
The error we got now is pretty confusing. We have more than one error from the compiler. Although there is only one mistake in the program we got multiple errors.
This can be a little tricky especially if it occurs in a big program.
Now have a look at the error we got. It shows mostly line 7 and one line 8. Here the compiler tries to tell that the error is mainly in line 7. Most of these errors doesn’t tell you the reason. If you are able to point out something from the bundle of errors, that will be great.
The selected line gives a hint about the mistake we made.
If we do what the compiler points out, the error gets fixed.
Fixing the error by adding ‘(‘to the main function.
#include "stdafx.h"
#include <stdio.h>
int _tmain(int argc, _TCHAR* argv[])
{
int a;
a = 10;
return 0;
}
Compiler Error Code:
What is a compiler error code?
Compiler error code is the number we get from the compiler error message, which indicates the type of error present in the program we created, say “C2143”. This is a compiler error. For example:
Compiler error code ‘C2143’.
These error codes will help you to search for that particular error.
In certain programs the error compiler shows, will not be understandable. In such cases these error codes come in handy.
Demo:
Copying an error code.
Opening the error code we copied in a browser.
The search result.
Open any link. We can see the Microsoft documentation of that particular error code.
The details about the particular error code we searched.
In this window you can read details about the error code and figure out the error we got and resolve the issue.
At the left side of the window we can see all the compiler errors.
Above fig shows all the compiler errors possible at the left side of the window.
Very Important !
Don’t wait till your code becomes too big to see your compiler errors.
Build your code in between, and see whether the compiler is satisfied or not. If our code becomes too huge and finally after building the code, we get an error,
it will be so difficult to troubleshoot the error. So, make sure you compile the code after every 5/6 line or even less than that,
and make sure that it gets compiled.
This is the most important point among all we have discussed.
Conclusion:
-
Compiler errors can be cryptic to begin with.
-
Start with simple programs and have a reference program always.
-
Make sure you use all possible online help.
-
Make sure you compile every single line or 5/6 lines before program getting too big.
-
Compiler errors can be a bit tiring, but make sure you don’t loose sight and try hard.
Don’t hate programming due to initial compiler errors.
Materials
Presentation Slides
PDF
PowerPoint
PPT
Source Code
Browse
Code
Video