Errors


If there are some problems with your C program the compilation process may fail. These issues can range from simple syntax errors, to other complicated problems that are harder to understand.

Sometimes the error message from the compiler will make sense, but if you are having trouble understanding it try searching online for it. You should see if you can find a concise explanation of what it means, and work out how to correct it. Remember this: there are many people before you who have struggled with exactly the same problems.

smash

Rage • A poor debugging technique

Sometimes there will be many compiler errors stemming from one source. Always go through compiler errors from first to last.

Sometimes the compiler will compile a program, but when you run it it will crash. Debugging C programs in this situation is hard. It can be an art far beyond the scope of this book.

If you are a beginner, the first port of call for debugging a crashing C program would be to print out lots of information as the program is running. Using this method you should try to isolate exactly what part of the code is incorrect and what, if anything, is going wrong. It is a debugging technique which is active. This is the important thing. As long as you are doing something, and not just staring at the code, the process is less painful and the temptation to give up is lessened.

For people feeling more confident a program called gdb can be used to debug your C programs. This can be difficult and complicated to use, but it is also very powerful and can give you extremely valuable information and what went wrong and where. Information on how to use gdb can be found online.

On Mac the most recent versions of OS X don’t come with gdb. Instead you can use lldb which does largely the same job.

On Linux or Mac valgrind can be used to aid the debugging of memory leaks and other more nasty errors. Valgrind is a tool that can save you hours, or even days, of debugging. It does not take much to get proficient at it, so investigating it is highly recommended. Information on how to use it can be found online.