How can I always print the function name, in which printf()
is called, without supplying the information every time?
Asked
Active
Viewed 728 times
1

John Kugelman
- 349,597
- 67
- 533
- 578

earthling
- 620
- 6
- 20
-
1@JohnKugelmann I challenge your duplicate. The answer below seems to be on the point asked by OP. The solution can only be found among the answers of your duplicate if you know the solution already and then not among the first few most upvoted answers and in a code-only answer. So please explain how OPs question is answered there in your opinion. – Yunnosch Jan 08 '21 at 07:19
-
Defining a logging macro for debugging is exactly what this both this question and the linked one are aiming to do. The amazingly comprehensive [accepted answer](https://stackoverflow.com/a/1644898/68587) covers variadic macros in C99, the GCC `##__VA_ARGS__` extension, as well as adding "things like `__FILE__`, `__LINE__` and `__func__` into the output, which can improve the diagnostics." – John Kugelman Jan 08 '21 at 07:26
-
Johns' linked answer is indeed on another level and includes way more detail and makes this Q&A kind of obsolete. One thing though: I may not have the best googling skills but I don't think I have the worst either, but I could not find that answer when I searched for it. I think the title of the other answer leads into a _somewhat_ different direction and handles my answer on a side note. Any thoughts on that? Should I delete this question? – earthling Jan 08 '21 at 07:31
-
I would leave this question up. It's nice to have alternate signposts for people with different search terms. – John Kugelman Jan 08 '21 at 07:32
-
1Upvoted both question and answer, but yeah it is a duplicate and Jonathan's answer does provide way more details – Antti Haapala -- Слава Україні Jan 08 '21 at 07:38
-
@John Thank you for your input, I tried and you're right :) I personally use `printf()` for debugging only, maybe that's why I did not bother to add `debugging` as a search term. – earthling Jan 08 '21 at 07:38
-
1Search tip, try searching for your goal rather than your solution. Here the *solution* is "print the function name with a wrapper". If you take a step back, the *goal* is to create a logging macro for debugging. Searching for "c logging macro" or "c debugging macro" both turn up the linked question (which is how I found it). – John Kugelman Jan 08 '21 at 07:39
-
That is a very good hint. Thanks. – earthling Jan 08 '21 at 08:01
1 Answers
3
You can use a variadic macro:
#define printff(a, ...) printf("%s(): " a, __func__, ##__VA_ARGS__)
This macro uses the string a
passed to printf and prepends it with "%s(): "
, which will hold the function name. The function name __func__
is the first supplied variable, which will always be used for the first supplied %s
. ##
before __VA_ARGS__
handles calls for a empty ...
.
This macro is expandable to include the file name __FILE__
or the line number __LINE__
.
This should work for C99 and up.

John Kugelman
- 349,597
- 67
- 533
- 578

earthling
- 620
- 6
- 20