Skip to content

Latest commit

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Debugging like a sir (in C)

#include"debug.h"intanswer(void) {
return42;
}
intmain(void) {
intnum=1;
char*str="hello";
debug(num);
// => num = 1debug(num, str, answer());
// => num = 1// str = hello// answer() = 42debug_raw("counting:", 1, -2, 3+0.4);
// => counting: 1 -2 3.4debug_raw(str, "world!");
// => hello world!idebug(num);
// => example.c:26: num = 1idebug(num, str);
// => example.c:29:// num = 1// str = helloidebug_raw("The answer is", answer());
// => example.c:34: The answer is 42return0;
}

Usage

Just include debug.h in your file.

Gotchas

  • Requires C11 support (uses _Generic). Consider using the flag -std=c11 when compiling.
  • Literal chars have type int, so you have to cast them to char to print them as actual chars:
debug_raw("Find the", (char)'X'); // => Find the X

Tips

For quick debugging, include the header right where you need it:

voidfunction(void) {
...
#include"debug.h"debug(var); // => var = XXX
...
}

To remove quickly all debugging output, define NDEBUG before the header inclusion:

#defineNDEBUG#include"debug.h"
...
debug(var); // Doesn't print anything
...

About

Debugging like a sir (in C)

Resources

Stars

286 stars

Watchers

7 watching

Forks

Releases

Packages

Contributors

Languages