some simple C debugging macros

My teacher always says if you need to use a debugger, you don’t understand your program. So, maybe I’m a shitty programmer, but these macros have helped me out.

Sometimes you need to check variables:

#define LOG(x, fmt)		fprintf(stderr, "LOG: %s : %" #fmt "\n", #x, x)

LOG(butt, f);

Sometimes you just want to print something, to see if a loop or branch is being executed:

#define CHECK(x)		fprintf(stderr, "CHECK: %s\n", #x)

CHECK(hi);

Then you pass into CHECK whatever string you want to be printed.

Note I didn’t put a semicolon at the end of the macros. If I did, then I would have to remember to not put one when I used the macros, otherwise I would end up with two semicolons when the macro expanded!

I’ve seen a lot more complicated debugging systems implemented in larger projects, but these will work fine for now.

Tags:

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Connecting to %s


Follow

Get every new post delivered to your Inbox.