Including debugging macros
Printf can help considerably in debugging and testing new code, on the other hand,
you should remove, or at least disable, such print statements. If you implement a new
feature in the program (or somebody finds a bug), and you
want to turn at least one of the messages back on. There are several ways to solve
both issues, to globally enable or disable your debug messages and to turn individual messages on or off.
Here we show one way to code printf calls so you can turn them on and off individually
or globally; the technique depends on defining a macro that resolves to a printf
call when you want it to:
• Each print statement can be enabled or disabled by removing or adding a single
letter to the macro’s name.
Including a debugging macro
#ifdef DEBUG
fprintf(stderr, "top of main loop k= %d\n",k);
#endif
• This code will only be run if the .c file has a
#define DEBUG before the #ifdef
• Easy to turn on and off
• Can also use DEBUG levels (e.g., #if DEBUG > 1)
disadvantages
• the program must be recompiled
• we can't use it on a running process
We
provide Including debugging macros-Free Online tutorials for the educational purposes only. We do not
responsiable
for the correctness of its contents. the risk of using it lies
entirelywith the user.