3

Simlar question with LINE

But when I replaced __LINE__ with __FUNCTION__. Macro concat string literal "__FUNCTION__" and not actual function name.

5
  • Please ask the actual question you have in this question. Linking to another, putatively similar question does not suffice. SO questions should stand on their own. Commented Jan 19, 2021 at 20:00
  • 1
    C11 __func__ is not a macro. The preprocessor does not care about it. The same happens with gcc's __FUNCTION__ extension ( see gcc.gnu.org/onlinedocs/gcc/Function-Names.html ).
    – pmg
    Commented Jan 19, 2021 at 20:00
  • @pmg any way to stringizing(##) functioname through macro. Commented Jan 19, 2021 at 20:03
  • 1
    No, not directly. The pre-processor does not have any way to get that information. Unless, of course, you give the information manually... int fx(void) { #define CURRFUNC "fx" ... } #undef CURRFUNC ... but, even with this, you can only use CURRFUNC inside the function. Note that this is a bad idea and I do not reccomend you try anyhting with it
    – pmg
    Commented Jan 19, 2021 at 20:05
  • 1
    __func__ is already a string, although it is a named array rather than a string literal. What do you want to do with it? It may already be suited for some uses as a string, so there would be no need to stringize it. Commented Jan 19, 2021 at 23:28

2 Answers 2

6

__FUNCTION__ is not a macro, it's an implicitly declared static array. The same is true for __func__, __PRETTY_FUNCTION__, etc.

Thus # can't work on it. If you want to concatenate something to it, you'll have to do that at runtime, or with constexpr.

3
  • I want to it at preprocessor time. Any way? Commented Jan 19, 2021 at 20:03
  • 4
    @gauravbharadwaj Nope, can't do that. Preprocessor is a simple thing, it doesn't know what functions are. Commented Jan 19, 2021 at 20:03
  • 1
    @gauravbharadwaj The preprocessor is (at least conceptually) decoupled from the parser and it operates just on directives and token streams. As such it lacks the parsing capability to recognize that after int (*func(int A, int B))(int C) {, __func__ should expand to "func". Commented Jan 19, 2021 at 20:15
-1

This will keep __FUNCTION__ as a string macro:

#pragma iso_9899_1999
2
  • Documentation? What compiler(s) does this apply to? Commented Mar 30, 2023 at 16:19
  • You are right, I just tested it on gcc and it has no impact. I usually use it with clang. As for documentation, I don't know where I found it long ago, sorry. Google does not seem much help.
    – dargaud
    Commented Mar 30, 2023 at 16:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.