I have a macro called PRINT(...)
that I use in my code, which gets a variable number of arguments and acts like printf
(gets a format and arguments). It's defined like this:
#define PRINT(...) PRINT(__VA_ARGS__)
Now I want to modify it so it will have an optional argument, say that its name is number
and it will add a numeric prefix to the printing. For example:
PRINT("%s", "hi")
-> will print hi
PRINT(1, "%s", "hi")
-> will print 1: hi
How can I change the PRINT
macro to support this?
Important to say, that I don't want to change any existing call to this macro from my code (in the example, if I have a call to PRINT("%s", "hi")
- it needs to remain the same after the change).
Also, I can't create new macro for this purpose- must use the existing PRINT
macro for this purpose (but off course I can change it's arguemnts definition).
Any idea how can I do this?
Edit: I saw this post about variadic macro- but It's different from what I'm asking here since the argument number
needs to be a recognized variable, which will be treated in the implementation of PRINT
as -1
if the call to PRINT
doesn't contain the number
argument (-1
will be an indicator for printing no number) and otherwise it will print the number prefix.