Is (ignore) valid code?

This is a discussion on Is (ignore) valid code? within the C Programming forums, part of the General Programming Boards category; Hi. Im using Visual Studio.NET 2003 and Im clearing warnings for some source files. Im getting the following when I ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    17

    Question Is (ignore) valid code?

    Hi.

    Im using Visual Studio.NET 2003 and Im clearing warnings for some source files. Im getting the following when I dont do a debug compile:

    warning C4002: too many actual parameters for macro 'PrintDebuggingText'
    The code in question relates to this that is in the header file:

    Code:
    #if DEBUGGING_TEXT_ON
    extern int PrintDebuggingText(const char* t, ...);
    #else
    #define PrintDebuggingText(ignore)
    #endif
    and an example of one of the lines in the actual C file is:

    Code:
    PrintDebuggingText("Gun Position x:%d,y:%d,z:%d\n",twPtr->RestPosition.vx,twPtr->RestPosition.vy,twPtr->RestPosition.vz);
    Apparently when I dont debug, it should ignore the function, but Im getting the warning above.

    I think its to do with the (ignore) part. Is it a deprecated code now? Can I safetly delete the (ignore) including the brackets? Warning goes away if I do that but me being new to this, Im unsure if it was supposed to do something.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    A macro is a #define that takes parameters. For example, this macro
    Code:
    #define debug_str(string, line, file) \
        fprintf(stderr, "%s:%i: %s\n", file, line, string)
    takes three parameters.

    Those parameters can be used in the macro, but they don't have to be. In your case, the macro
    Code:
    #define PrintDebuggingText(ignore)
    is defined when DEBUGGING_TEXT_ON is false. What's it defined as? It's defined as nothing. In this case, the PrintDebuggingText macro is replaced with nothing (presumably, to eliminate the debugging messages).

    [edit] In case I wasn't clear: that "ignore" is like an unused parameter. It could be called anything, because it isn't used. I guess "ignore" is as good a name as any. [/edit]

    The problem here is that this
    Code:
    extern int PrintDebuggingText(const char* t, ...);
    takes one parameter followed by zero or more parameters, probably printf-style, whereas this
    Code:
    #define PrintDebuggingText(ignore)
    only takes one parameter.

    The easiest way to fix this is to make the second maco also take the same parameters, i.e.
    Code:
    #define PrintDebuggingText(ignore, ...)
    Unfortunately, that code is C99-only. If you're okay with that, fine.

    Otherwise, you might consider creating a dummy function like this:
    Code:
    #if DEBUGGING_TEXT_ON
    extern int PrintDebuggingText(const char* t, ...);
    #else
    int PrintDebuggingText(const char* t, ...) {
        return 0;
    }
    #endif
    Also: I wouldn't use that PrintDebuggingText() function anyway. fprintf(stderr, ...) works just fine.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    287
    Visual Studio doesn't understand variadic macros? I'm pretty sure it does...

    #define PrintDebuggingText(ignore, ...)

    [edit] Was beaten to it. [/edit]

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,424
    Quote Originally Posted by arpsmack View Post
    Visual Studio doesn't understand variadic macros? I'm pretty sure it does...
    Not that I know of.
    But you could just add an #ifdef inside the function and strip out all its code in Release. The compiler will happily optimize away the function call altogether.
    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.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    Not that I know of.
    But you could just add an #ifdef inside the function and strip out all its code in Release. The compiler will happily optimize away the function call altogether.
    But only if that code is visible to the compiler at the time of compilation. If it's in a different source file, or even worse a .LIB or .DLL compiled separately, it won't.


    So, as suggested:
    Code:
    static inlineint PrintDebuggingText(const char* t, ...) {
        return 0;
    }
    in the "not defined" segment would be the right thing to do. Then the compiler will just remove the entire piece of code when it sees that it's not doing anything.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Jun 2008
    Posts
    17
    Thanks for the replies.

    Code:
    #if DEBUGGING_TEXT_ON
    extern int PrintDebuggingText(const char* t, ...);
    #else
    int PrintDebuggingText(const char* t, ...) {
        return 0;}
    #endif
    Gave lnk2005 errors. Something about the function was already defined in the linker file. Ive done it like this now:

    Code:
    #if 0
    #if DEBUGGING_TEXT_ON
    extern int PrintDebuggingText(const char* t, ...);
    #else
    #define PrintDebuggingText(ignore)
    #endif
    #endif
    extern int PrintDebuggingText(const char*, ...)
    The #define PrintDebuggingText(ignore, ...) wouldnt work either.

    Seems to be working now, thanks for the help. Im sure I have more to ask for again soon.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,424
    Code:
    #if DEBUGGING_TEXT_ON
    extern int PrintDebuggingText(const char* t, ...);
    #else
    static inline int PrintDebuggingText(const char* t, ...) { return 0; }
    #endif
    As mats already suggested, use static inline.
    If you don't use inline, you will get multiple symbols error.
    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.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The alternative solution (but not quite so good for performance) would be to move the #if DEBUGGING_TEXT_ON into the actual implementation of PrintDebuggingText.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Explain this C code in english
    By soadlink in forum C Programming
    Replies: 16
    Last Post: 08-31-2006, 12:48 AM
  2. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 03:28 AM
  3. Seems like correct code, but results are not right...
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 02-13-2003, 12:33 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Azbia - a simple RPG game code
    By Unregistered in forum Game Programming
    Replies: 11
    Last Post: 05-03-2002, 06:59 PM

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