Thread: Macro help

  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    46

    Macro help

    Hello,

    My macro looks like this:

    Code:
    #include <stdio.h>
    #include <stdbool.h>
    
    #define PRINT_ONCE(message) \
            do { \
                static bool fixme_written = 0;\
                if (!fixme_written) \
                {\
                   fixme_written = 1; \
                   fprintf(stderr, message); \
                } \
            } while (0)
    
    static void OnlyPrintOnce()
    {
        PRINT_ONCE("This patch is broken\n");
    }
    
    int main()
    {
        int i;
    
        for (int i = 0; i < 10; i++)
        {
            OnlyPrintOnce();
        }
    }
    Seeing as prinft takes variable number of arguments, it is not clear to me how to pass such arguments from the macro. Could anyone give me a heads up on how this could be done? It is probably simple but has eluded me so far so good.

    Thanks in advance.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,362
    I can't say I've ever had reason to use the feature myself, but variadic macros were introduced in C99.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2021
    Posts
    70
    You are passing everything through a call to a function. You can make that function into a varargs function by declaring parameters with '...', and then call vfprintf instead of fprintf, passing the va_list parameter.

  4. #4
    Registered User
    Join Date
    Oct 2019
    Posts
    46
    Quote Originally Posted by laserlight View Post

    I can't say I've ever had reason to use the feature myself, but variadic macros were introduced in C99.
    Thanks, the term variadic saved me there. It seems like they work with -std=gcc89? Is that ten years before C99? Well.


    You are passing everything through a call to a function. You can make that function into a varargs function by declaring parameters with '...', and then call vfprintf instead of fprintf, passing the va_list parameter.
    Thanks a lot. This time, for me, it is actually not necessary because 'fprintf' already accepts a variable number of arguments.

    My result was

    Code:
    #define PRINT_ONCE(...) \
    
            do { \
    
                static bool fixme_written = 0;\
    
                if (!fixme_written) \
    
                {\
    
                   fixme_written = 1; \
    
                   fprintf(stderr, __VA_ARGS__); \
    
                } \
            } while (0)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking a macro's sign in order to define other macro
    By Egalestrenge in forum C Programming
    Replies: 5
    Last Post: 05-23-2016, 09:39 PM
  2. what does this macro do?
    By MayaIlias in forum C Programming
    Replies: 0
    Last Post: 06-09-2015, 03:23 AM
  3. Help with macro
    By spanlength in forum C Programming
    Replies: 4
    Last Post: 07-03-2012, 03:37 PM
  4. need a macro that does nothing
    By Nyda in forum C Programming
    Replies: 5
    Last Post: 11-18-2004, 10:16 AM
  5. macro
    By sballew in forum C Programming
    Replies: 2
    Last Post: 10-10-2001, 07:51 PM

Tags for this Thread