Debug function like printf()

This is a discussion on Debug function like printf() within the C Programming forums, part of the General Programming Boards category; Hi, I need some help! I'd like to do a function for debug my code (at compile time) that follows ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    6

    Debug function like printf()

    Hi, I need some help!

    I'd like to do a function for debug my code (at compile time) that follows this scheme:

    #ifdef DEBUG
    my_printf( <list arguments> ) printf( <list arguments> )
    #else
    my_printf( <list arguments> ) NOTHING
    #end

    In other words if DEBUG is defined the code is replace with the printf() function else do nothing.

    How can I write this macro for my purpose??

    Thanks a lots.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    #ifdef DEBUG
    #define my_printf printf
    #else
    #define my_printf
    #end
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    6
    Thanks but with this define I get a lot of:
    Code:
    cripto.c: In function ‘print_error’:
    cripto.c:90: warning: left-hand operand of comma expression has no effect
    cripto.c:90: warning: left-hand operand of comma expression has no effect
    cripto.c:90: warning: statement with no effect
    where the function is present and DEBUG not defined.

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    If you have C99, you can use variadic macro.
    #define debug_print(...) fprintf(stderr,__VA_ARGS)
    You might also want to read:
    Question 10.26

  5. #5
    Registered User
    Join Date
    Jul 2010
    Posts
    6
    Thanks, I used this:
    Code:
    #ifdef DEBUG_CRIPTO
    	#define lib_debug(...) fprintf(stderr, __VA_ARGS__)
    #else
    	#define lib_debug(...)
    #endif
    and it works fine.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Hrm...
    How about:

    #ifdef DEBUG
    #define my_printf printf
    #else
    #define my_printf //
    #end
    (Assuming you have C99.)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    For your method, I think you'd have to do something like this:
    Quote Originally Posted by Elysia View Post
    #ifdef DEBUG
    #define my_printf( x ) printf( x )
    #else
    #define my_printf
    #end
    Then I think you need double parentheses when calling it: my_printf(( "%d %s", 5, "foo" ));

    Something like that anyways.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  8. #8
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    It is #endif not #end

    A small addition to Elysia's idea:
    Code:
    #ifdef DEBUG
    #define my_printf printf
    #else
    #define my_printf ;//
    #end
    Since you might have
    Code:
    if (something)
      my_printf(...);
    Of course what Mordor actually did would be the best for C99.

    testing cpjust idea
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    #define DEBUG
    #ifdef DEBUG
    #define my_printf(x) printf(x)
    #else
    #define my_printf
    #endif
    
    int main()
    {
      printf(("%s", "10"));
    }
    this works. This doesn't compile
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    #define DEBUG
    #ifdef DEBUG
    #define my_printf(x) printf(x)
    #else
    #define my_printf
    #endif
    
    int main()
    {
      printf(("%d", 10));
    }
    Both give a warning (in GCC). That the left hand operant on the comma has no effect. The error the later gets is that
    "passing arg 1 of 'printf' makes pointer from integer without a cast"

    What I think happens is that this
    Code:
    ("%s", "hey") --> ("%s" "hey") --> ("%shey")
    so the first one will run. The second I have no idea. Or something equally weird happens.

    Conclusion: be careful when being tricky with macros

    What maybe you could do is change it to
    Code:
    #define my_printf(x, y) printf(x, y)
    and always use printf with two inputs and combine multiple of them for more inputs

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help again!!
    By ewic0190 in forum C Programming
    Replies: 1
    Last Post: 03-19-2010, 06:56 PM
  2. Trouble with two two dimensional arrays
    By scmurphy64 in forum C Programming
    Replies: 5
    Last Post: 12-06-2009, 05:57 PM
  3. Stuck with function
    By scmurphy64 in forum C Programming
    Replies: 9
    Last Post: 11-10-2009, 10:41 AM
  4. segmentation fault upon reload
    By yabud in forum C Programming
    Replies: 8
    Last Post: 12-18-2006, 05:54 AM
  5. Replies: 28
    Last Post: 07-16-2006, 11:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21