Debugging-Syntax Errors

In computer terms, debugging means to get rid of errors. The term comes from the early days of computers when a moth (a literal bug) got caught in a computer causing it to not work properly.

Through out this book, we will look at ways to debug a program, starting with some of the simplest. We will then work our way into using tools.

The simplest type of errors to find and fix are typically syntax errors. Syntax errors occur when you don't follow the rules of the language. You forgot some key punctuation. You spelled something wrong. etc. etc. These are the type of errors that the compiler is able to help you find. While they are very annoying at times, they are also relatively easy to find.

Lets start by looking at this piece of code:

#include <stdio.h>
int main(void){

    printf("this program has a bug\n")
    return 0;
}

Now, it may be quite obvious to some of you where the problem is in the code, but lets take a look at what happens when we try to compile it anyways.

Using gcc I get the following:

tmp.c: In function ‘main’:
tmp.c:5:5: error: expected ‘;’ before ‘return’

Using cl (visual studio commmand line compiler)

tmp.c
tmp.c(5) : error C2143: syntax error : missing ; before return

Using clang I get the following:

tmp.c:4:39: error: expected ';' after expression
    printf("this program has a bug\n")
                                      ^
                                      ;
1 error generated.

So, same code, 3 different compilers, 3 different messages.

What this means is that you will have to learn to read what it is that the compiler is trying to say. Now, all of these messages do have some things in common. Lets take a look at the first line of each of these

gcc: tmp.c:5:5
cl: tmp.c(5)
clang: tmp.c:4:39

Now, two of these have the number 5, one has the number 4. These are all referring to the line numbers on which the error was detected. This is the first thing you should look for when debugging syntax errors. Find the line where things went wrong.

Now, lets look at the program (with line numbers)

1. #include <stdio.h>
2. int main(void){
3.
4.     printf("this program has a bug\n")
5.     return 0;
6. }

Ok, sooo looking at the above, line 4 is the line with printf, line 5 is the return statement. Now, lets look closer at the 3 error messages in full:

gcc: tmp.c:5:5: error: expected ‘;’ before ‘return’

cl: tmp.c(5) : error C2143: syntax error : missing ; before return

clang: tmp.c:4:39: error: expected ';' after expression
    printf("this program has a bug\n")
                                      ^
                                      ;

Notice that the two compilers that marked that the error on line 5 both say that it expected a ; before return. Now, return is at start of line 5... so before return would mean that the error must have been on the previous line. The message from clang though is different. It tells you that the ; is missing after the printf() statement and even points to where its missing. So, line 4 is appropriate in this case.

The error is on line 4. The ; is missing at the end of the line. However, both gcc and cl detected that it was a problem when they got to line 5 as it saw that the previous statement had not been terminated yet. Thus, it reported it as a line 5 error. clang on the other hand caught it as the expression on line 4 not having been terminated with a semi-colon.

Either way, the fix to this problem is just a simple insertion of a semi-colon at the end of line 4.

#include <stdio.h>
int main(void){

    printf("this program has a bug\n");
    return 0;
}

In the end, what this all means is that different compilers will complain about syntax in different ways. You will need to learn about how your compiler likes to complain about syntax errors. Nothing short of practice will make that happen.

Last updated