When it comes coding a program, we normally use printf statements to test or debug the code. When the coding gets completed, we find these printf statements are no longer needed, so we remove them.
Later on, if we add a new feature to this existing code (or somebody finds a bug in the code), we may want to use these printf statements again to test or debug the code. At that time, we go for including these printf statements and later on, we delete them when the coding gets completed...
This process is a bit arduous one and it consumes lot of time...
In order to do this, we can better enabled and disabled these printf statements by commenting or uncommenting the macro definition.
Here is the sample code to do that:
#include <stdio.h>
#define debug(fmt, args1, args2) printf(fmt, args1, args2) /* use when debugging is needed */
#define debug(fmt, args1, args2) /* no debugging: nothing */
int main()
{
debug("The value is %d and the string is %s\n", 10, "ten");
..../* rest of the code */....
return 0;
}
when you need to debug use the first macro definition
#define debug(fmt, args1, args2) printf(fmt, args1, args2) /* use when debugging is needed */when you don't need to debug, use the second macro definition
#define debug(fmt, args1, args2) /* no debugging: nothing */
Any number of arguments can be used. When there is no need for an argument, use can simply initialize it to zero...
For example, In the above code, if you want to just print the value alone.. you could use
/* print value and the string */
debug("The value is %d and the string is %s\n", 10, "ten");
/* just print the value*/
/* there is no use for args2, so args2 = 0 */
debug("The value is %d\n", 10, 0);
By using this one can save a lot of time...
Hope this might be useful for all...


Sign In
Create Account


Back to top









