__FUNCTION__ in another macro

Discussion in 'C Programming' started by Mockey Chen, Jul 27, 2006.

  1. Mockey Chen

    Mockey Chen Guest

    I using #define as following:
    #include <stdio.h>
    #define LOG_PREFIX "Current function: <" __FUNCTION__ ">: "

    int main()
    printf(LOG_PREFIX "some thing.\n");

    return 0;

    I complie and got error:
    % cc func.c
    func_test.c: In function `main':
    func_test.c:6: error: syntax error before "__FUNCTION__"

    I want to use LOG_PREFIX macro automatice expand.
    Is there any way to do it?

    Thanks in advance.

    Mockey Chen
    Mockey Chen, Jul 27, 2006
    1. Advertisements

  2. Mockey Chen

    mdler Guest


    why so complicated

    just do it like this

    #include <stdio.h>

    int main ()
    printf("Current function <%s>\n",__FUNCTION__);
    return 0;

    doing things your way it won't work, strings can not be added to
    eachother in C
    There has to be always memory to hold it!

    better is it to make a error function like this

    void error_logger(char *functionName,char *mess)
    printf("Current function <%s> %s\n",functionName,mess);

    and call it like this


    this You can put in a macro like this

    #define ERROR_LOG(mess) error_logger(__FUNCTION__,mess)

    Now you can call it like


    I hope that now your quetion is anserd

    Greetings Olaf

    Mockey Chen schreef:
    mdler, Jul 27, 2006
    1. Advertisements

  3. Mockey Chen

    Eric Sosman Guest

    Mockey Chen wrote On 07/27/06 10:06,:
    Probably because __FUNCTION__ has not been defined.
    From the usage, it appears you expect __FUNCTION__ to be
    a macro that expands to a string literal (which would then
    combine with the literals on each side to make one larger
    literal). C does not define any such macro, though, so
    you must either (1) define it yourself or (2) use a compiler
    that defines __FUNCTION__ automatically as an extra feature
    beyond what Standard C provides. (I believe that some gcc
    versions, if invoked in some modes, will do this.)
    If you can find a compiler that supports the C99 version
    of the Standard, you can use the predefined identifier __func__
    for a similar purpose. Note, though, that __func__ is not a
    macro and does not expand to a string literal; it refers to an
    automatically-generated array of const char containing the
    function name as a string. That means you can't concatenate
    __func__ with its neighbors the way you're trying to do with
    __FUNCTION__, but will need to do something more like

    printf ("Function: <%s>: some thing.\n", __func__);
    Eric Sosman, Jul 27, 2006
  4. (Top-posting corrected.)
    It is expanded. However, if your compiler doesn't support the
    __FUNCTION__ macro (which, as I understand it, is a C99 feature,
    which is supported by some C90 compilers), then this will cause
    an error, as the expansion becomes:

    printf("Current function: <" __FUNCTION__ ">: " "some thing.\n");

    As a quick test, add:

    #define __FUNCTION__ "foo"

    If your compiler doesn't support it, the only option is to do
    something else, or get a compiler that does support it.

    String literals can be "added" just fine:

    printf("Hello" " world" ".\n");


    #define LONG_STRING "This is a long string " \
    "that won't fit on a " \
    "single line."

    | Kenneth J. Brody | www.hvcomputer.com | #include |
    | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
    Don't e-mail me at: <mailto:>
    Kenneth Brody, Jul 27, 2006
  5. One macro? How long have you been doing C?

    Hmm. This compiles for me:
    #include <stdio.h>
    int main (void)
    puts ("This is a long" "string that I'm" "adding" "together.");
    return 0;

    Also, please type properly. You type once, but the message is read
    thousands of times. (So when it takes an extra 3.6 seconds for a
    thousand people to understand your message, it had better saved
    you an hour.)
    The original version was fine, style-wise.
    I saw some other posts below yours; it's probably that it has.

    Don't top post!
    Don't top post!
    Don't top post!
    Andrew Poelstra, Jul 27, 2006
  6. There are several ways to accomplish this. Start by removing the
    nonstandard identifier __FUNCTION__; the standard provided for this use
    is __func__. And remember that __func__ (and the non-standard
    __FUNCTION__ for many compilers that provide it) are used as if the were
    declared as character arrays, not a string literals. Here's one way:

    #include <stdio.h>
    #define LOG_PREFIX "Current function: <%s>: %s", __func__,

    int main()
    printf(LOG_PREFIX "some thing.\n");
    return 0;

    Obviously there are some advantages to changing this to

    #include <stdio.h>
    #define LOG(s) do {\
    printf("Current function: <%s>: %s", __func__, s);\
    } while 0;

    int main()
    LOG("some thing.\n");
    return 0;
    Why? Are you too fucking important to be bothered with us after you get
    your answer? Or are you a bill collector trying to imply you are owed a
    Martin Ambuhl, Jul 27, 2006
  7. What is __FUNCTION__ ? Is it mistyping of __func__ ? __func__ is not a
    preprocessor. After preprocessing, a macro likes this: #define STH
    "AAA" __func__ is still: "AAA" __func__ . The __func__ remains. For
    string literal, "AAA" "BBB" is correct, but __func__ is a string
    variable, not a string literal, so "AAA" __func__ is a syntax error.
    lovecreatesbeauty, Jul 27, 2006
  8. Totally uncalled for, bad netiquette, and off-topic.

    Tak-Shing Chan, Jul 27, 2006
  9. Mockey Chen

    Default User Guest

    The C99 macro is __func__, so the one above is some
    implementation-specific extension.

    Default User, Jul 27, 2006
  10. Mockey Chen

    jacob navia Guest

    Please Mr Chen, do not think that all people in this group are like
    this person.

    You were polite, and you are welcome!

    jacob navia, Jul 27, 2006
  11. Agreed - I have even used "Thanks in advance" myself on occasion. The
    intent was to convey gratitude without cluttering up the thread with
    otherwise content-free one line "thanks" posts.
    John Devereux, Jul 27, 2006
  12. [edited for content]

    Some people find the phrase "Thanks in advance" offensive. I can sort
    of see the point; taken literally, it could imply some sort of
    unwelcome obligation, or a rebuke to anyone who doesn't offer help.
    But it should be obvious that the *intent* of the phrase is simply to
    be polite. It's difficult to reach the same conclusion for "Are you
    too f***ing important to be bothered with us after you get your

    Martin, you seem to be insisting on your own standard of politeness
    while being extraordinarily rude yourself. Next time someone
    innocently writes "Thanks in advance" or something similar, I suggest
    you ignore it. If you must flame someone for something so trivial,
    please do so in private; I can't speak for everyone, but I strongly
    suspect that nobody else is very impressed by your ability to curse.

    Grow up.
    Keith Thompson, Jul 27, 2006
  13. This is uncalled for, and rude. Martin you need to think before you
    post this sort of stuff.
    I write dozens of emails every day which, if they were asking for
    something, I generally end in "thanks". This is by definition "in
    advance" of when I get the answer, so...

    Mark McIntyre

    "Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are,
    by definition, not smart enough to debug it."
    --Brian Kernighan
    Mark McIntyre, Jul 27, 2006
  14. I fail to see how this can be, by any stretch of the imagination. Its
    merely being polite and no different to saying "have a nice day" or
    "best regards" or "cheers" or "yours sincerely" which I also doubt
    anyone actually means as anything other than a pleasantry.
    Mark McIntyre

    "Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are,
    by definition, not smart enough to debug it."
    --Brian Kernighan
    Mark McIntyre, Jul 27, 2006
  15. Mockey Chen

    jacob navia Guest

    Agreed. If we are going to flame somebody because
    he/she is polite we are doomed.

    jacob navia, Jul 27, 2006
  16. Quoting order corrected.
    What's complicated about it? It's a simple and common way to do this
    sort of thing, assuming __FUNCTION__ expands to a string. Mockey's
    problem is most likely that __FUNCTION__ is not defined.
    Why so complicated? Why do at run-time what can be simply and cleanly
    done at compile time?
    Adjacent strings are automatically concatenated in C.

    const char *str = "This " "is" " "
    "a str" "ing";

    has exactly the same effect as

    const char *str = "This is a string";
    Obviously, but what does that have to do with anything?
    Not at all better - that requires extra function calls and extra work
    in printf().
    J. J. Farrell, Jul 27, 2006
  17. That's grossly out of order.

    Messages which just say "thanks" to a reply are irritating, in that
    readers go to them eager for new words of wisdom and find nothing.
    They're little more than "me toos", though much more acceptable since
    they at least have a valid social purpose. It's also very difficult to
    know when to send a note of thanks. I've seen threads where the OP has
    carefully posted a thanks reply to every response in the thread, making
    half the messages in the thread of no value to most people. Given the
    nature of Usenet, it can be very difficult to work out when to post a
    single thanks reply.

    On the other hand, a comment at the end of the original posting
    offering thanks in advance makes it clear that the poster will be
    grateful for any help he gets, and indicates to me that he doesn't
    think he is owed a response. It avoids all the issues with content-free
    messages cluttering up the thread. For me, it's the most polite way
    overall of handling it (perhaps followed up with individual emails if
    any posters are particularly helpful).
    J. J. Farrell, Jul 27, 2006
  18. Perhaps you have no imagination. ;-)
    Ironic choice of phrase because it's one that I hate hearing from
    Americans! Usually, because it's delivered in a forced manner
    by someone who doesn't mean it but is saying so because it's
    company policy to feign empathy in front of clients.

    Martin has is manic moments. Perhaps he's just auditioning for the
    US version of "Grumpy Old Men". <g>
    Peter Nilsson, Jul 28, 2006
  19. Me too, but I don't generally respond by telling the other party to
    fsck off.

    Mark McIntyre

    "Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are,
    by definition, not smart enough to debug it."
    --Brian Kernighan
    Mark McIntyre, Jul 28, 2006
  20. __FUNCTION__ is exactly as non-standard in C89 (which I prefer to call
    C90) as it is in C99.
    Keith Thompson, Jul 29, 2006
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.