Trouble Shooting Compiler Errors

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.

Above fig4-1 shows creating a new project in Visual Studio.
A new project in Visual Studio.
As in the above  fig4-2 select 'Win32' under 'Visual C++',
                                              and then select 'Win32console'. Name your project, say 'comperrors' in this case and click 'Ok'
Select 'Win32' under 'Visual C++', and then select 'Win32console'. Name your project, say 'comperrors' in this case and click 'Ok'
Above fig4-3 shows clicking 'Finish' in the wizard window.
Click 'Finish' in the wizard window.
Above fig4-4 shows the output of the wizard.
The output of the wizard.

Now remove ; (semicolon) in the statement 'return 0' and build the program.

Above fig4-5 shows removing semicolon after return 0 and building 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.

In the above fig4-6, the box at the bottom of the screen is 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.

Above fig4-7 shows, the output window displaying 'build is not a success and our project 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.

Above fig4-8 shows the error we got.
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.

Above fig4-9 shows double clicking 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.

Above fig4-10 shows an arrow pointing line 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.

Above fig, 4-11 shows the line number at the bottom of the output window.
The line number at the bottom of the output window.
Above fig4-12 shows putting a semicolon in line number 10.
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.

Above fig4-13 shows building the project after putting a ';' in line 10.
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 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.

Above fig4-15 shows removing the closing curly braces from the application.
Removing the closing curly braces from the application.

      
#include "stdafx.h"
#include <stdio.h>

int _tmain(int argc, _TCHAR* argv[])
{
  return 0;
                                                                        
Above fig4-16 shows building the project by removing the closing '}'.
Building the project by removing the closing "}".
In the output window of the above fig 4-17, we can see the status of our application as failed.
In the output window of the above code, we can see the status of our application build as failed.
Above fig4-18 shows the error we got.
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.

Above fig4-19 shows double clicking on the error and an arrow appearing at line 12.
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.

Above fig4-20 shows clicking on the open brace and the bottom of the output window showing 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;
}
                                                                      
Above fig4-21 shows closing the curly braces and building the program.
Closing the curly braces and building the program.
In the above fig4-22 we can see in the output window that, our project is compiled successfully.
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’.

Above fig4-23 shows adding a new line ‘a=10’ and building the project.
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.

Above fig4-24 shows 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.

Above fig4-25 shows declaring ‘a’ and building the program.
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.

Above fig4-26 shows our build is successful.
Our build is successful.

What happens if we give ‘A’ instead of ‘a’?

Above fig4-27 shows giving ‘A’ instead of ‘a’ and building the program.
Giving ‘A’ instead of ‘a’ and building the program.
Above fig4-28 shows the error we got.
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.

Above fig4-29 shows removing an opening parenthesis from the ‘main’ function.
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.

Above fig4-30 show the errors we got.
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.

In the above fig4-31 the selected line gives a hint about the mistake we made.
The selected line gives a hint about the mistake we made.

If we do what the compiler points out, the error gets fixed.

Above fig4-32 shows fixing the error by adding ‘(‘to the main function.
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:

Above fig4-33 shows compiler error code ‘C2143’.
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:

Above fig4-34 shows copying an error code.
Copying an error code.
Above fig4-35 shows opening the error code we copied in a browser.
Opening the error code we copied in a browser.
Above fig4-36 show the search result.
The search result.

Open any link. We can see the Microsoft documentation of that particular error code.

Above fig4-37 shows the details about the particular error code we searched.
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 fig4-38 shows all the compiler errors possible at the left side of the window.
Above fig shows all the compiler errors possible at the left side of the window.

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.

Presentation Slides

PDF
PowerPoint PPT

Source Code

Browse Code

Video

Author: Meera
Subject Name: C Programming Basics
Subject Category Name: System Programming

#TypeTitle
1
2
3
4
5
6
7
8
9
10
11
12
13