How to use variable arguments with printf in a macro?

I did not find a way to merge the first printf into the second:

unsigned get_time_now(void) {return 1;}
#define DEBUG_PRINT 1
#define debug_tcprintf(fmt, ...) do { \
            if (DEBUG_PRINT) { \
                unsigned p_time_now = get_time_now(); \
                printf ("%u ms ", p_time_now); \
                printf(fmt, __VA_ARGS__); \
            } \
        } while (0)

I need to do this to get atomic debug_tcprintf. The above macro was taken from this stack overflow question .

I am making code in XC that runs on a multi-core XMOS processor. It compiles XC, C, and C ++, but the sample code is from part of the C code. This is similar to XC, except that it has a timer defined in the language.

If it is not possible to combine the two in one printf, perhaps this could be an option to create a string and use sprintf instead? I would prefer, since such an array can easily overflow.

+4
1

. , - .

#define debug_tcprintf(fmt, ...) do { \
    if (DEBUG_PRINT_HTTPD) { \
        unsigned p_time_now = get_time_now (); \
        printf ("%u ms " fmt, p_time_now, ##__VA_ARGS__); \
    } \
} while (0)

"%u ms " . Token ( ##) ( ).

, .


: - gcc C. gcc, ##. , ; , debug_tcprintf ("hello world") . debug_tcprintf ("%s", "hello world") .

+1

Source: https://habr.com/ru/post/1684683/


All Articles