Debugging your C code

Debugging C can sometimes be a pain but thanks to some handy tools, there are some efficient ways to make your code run smoothly.

Note that this isn’t something you should only do after you finished writing your program. Debugging is very important and should be done everytime you add something meaningfull to your code.

Show all warnings when compiling

Most important thing to do, if you weren’t doing it before this should fix 90% of your problems. Nowadays compilers are no longer dumb beasts only capable of spewing compiled code and errors. They can assists us in debugging our codes through the use of warnings (and more).

Warnings are like errors but less important, in the sense that they wont prevent your program from running. But they should always be resolved (and I mean it). Some less important warnings are often hidden by default, we will have to add them manually.

Using gcc you can add the -Wall and -Wextra flags to enable all warnings to be shown. This should allow you to remove a lot of issues in your code by highlighting instructions which might seem ambiguous.

Use Valgrind to spot memory errors

Conditional jump and invalid read / write are very common mistakes which happens to pretty much everyone. Valgrind is a very usefull tool that we can use to fix these mistakes.

While valgrind is a very powerfull tool, we will only gloss over the most usefull and easy to use features here.

In order to make our debugging easier we will compile our code with the -g flag (using gcc). This will allow valgrind to show us the line at which the error occured. Don’t forget to remove it afterward (or just make a new rule in your Makefile).

We can now start debugging our program, use the following command to start your code:

valgrind ./your_program

If you have never used valgrind before, don’t be afraid if you see a bunch of error messages on your screen.

Let’s see how to debug this simple program:

#include <unistd.h>

int			main()
	char	c;

	write(1, &c, 1);
	return (0);

Here’s valgrind output:


As we can see there seems to be an unitialised value used in our code at line 7 in the file called main.c.

Let’s fix our code accordingly:

#include <unistd.h>

int			main()
	char	c;

	c = 'a';
	write(1, &c, 1);
	return (0);

If we try to run our code again, we can see that there is no error anymore.

You will sometimes see errors in valgrind which seems to make no sense, saying that x value is not initialized when you are sure of initializing it. When this happens, remember one simple rule:

Valgrind is always right.

Seriously, don’t dismiss any error thrown out by valgrind, they need to be fixed if you don’t want your program crashing.

Print all your values and have simple tests in your code

When debugging, try to print your variables everytime they are expected to change. This is a very simple thing to do but it is more effective than it seems. Don’t ever hesitate to add printfs every other lines to monitor the state of your code.

While manually printing variables might be very effective, it is not the cleaniest way to test your program. There are some framework which will allow to run unit tests on your program, I won’t go into it here but if you’re interested, a list can be found here.


Debugging C programs might seem like something very hard to do and that no one wants to do. However debugging is one of the most important aspect of programming and it has to be done regularly.

To assist us, there are plenty of available tools which we showcased here.

I hope this helped you understand the basics of debugging C code, and that it allow you to write your own bug-free awesome software!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s