Jump to content

Turning the printf statements on and off

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
veda87

veda87

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
This is my first tutorial...

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...

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Out of curiosity, wouldn't it it be just as easy to wrap printf statements for debugging in a #ifdef __DEBUG__ rather than what you have here? It seems like that would do a better job of alerting the reader of what's happening while also giving you access to the full number of arguments in printf().
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,720 posts
I usually do it like this, to allow any number of arguments:

#ifdef __DEBUG__
    #define    DPRINTF(f,...)    fprintf(stderr,f,##__VA_ARGS__)
#else
    #define    DPRINTF(f,...)
#endif

Then I'd use it like this:
#define __DEBUG__

/*problematic code...*/
DPRINTF("Current pointer value is %p\n",mypointer);
somefunc(mypointer);

By the way, the ## in the definition allows you to completely omit the arguments if you want. For example:

DPRINTF("Entering message loop\n");

The above line will not work without the ##, at least on GCC.

Edited by dargueta, 24 September 2009 - 05:03 PM.
Added example

sudo rm -rf /

#4
debtboy

debtboy

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 916 posts

veda87 said:

This is my first tutorial...
Good first tutorial,
looking forward to seeing more from you :thumbup1:

WingedPanther & dargueta are very smart guys,
we can all learn from them.
Good Thread +rep

#5
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,720 posts
I'm not necessarily smart, just exceptionally good at googling.
sudo rm -rf /

#6
brownhead

brownhead

    Programmer

  • Members
  • PipPipPipPip
  • 173 posts
Wow, I never knew you could have a macro with a variable number of arguments. That's amazing.
+rep