__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.

    --
    Regards.
    Mockey Chen
     
    Mockey Chen, Jul 27, 2006
    #1
    1. Advertising

  2. Mockey Chen

    mdler Guest

    Hello

    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

    error_logger(__FUNCTION__,"something");


    this You can put in a macro like this

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

    Now you can call it like

    ERROR_LOG("someThing");


    I hope that now your quetion is anserd

    Greetings Olaf


    Mockey Chen schreef:

    > 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.
    >
    > --
    > Regards.
    > Mockey Chen
     
    mdler, Jul 27, 2006
    #2
    1. Advertising

  3. Mockey Chen

    Eric Sosman Guest

    Mockey Chen wrote On 07/27/06 10:06,:
    > 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__"


    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.)

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


    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
    #3
  4. (Top-posting corrected.)

    mdler wrote:
    > Mockey Chen schreef:

    [...]
    > > 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.


    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 it then compiles, and show the function "<foo>", then that's
    the problem.

    > > Is there any way to do it?


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

    [...]
    > doing things your way it won't work, strings can not be added to
    > eachother in C


    String literals can be "added" just fine:

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

    or

    #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
    #4
  5. On 2006-07-27, mdler <> wrote:
    > Hello
    >
    > why so complicated


    One macro? How long have you been doing C?

    <manual printf() snipped>
    > 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!


    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.)

    >
    > 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);
    > }


    The original version was fine, style-wise.

    >
    >
    > and call it like this
    >
    > error_logger(__FUNCTION__,"something");
    >
    >
    > this You can put in a macro like this
    >
    > #define ERROR_LOG(mess) error_logger(__FUNCTION__,mess)
    >
    > Now you can call it like
    >
    > ERROR_LOG("someThing");
    >
    > I hope that now your quetion is anserd


    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!

    >
    > Mockey Chen schreef:
    >
    >> 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?
    >>



    --
    Andrew Poelstra <website down>
    To reach my email, use <email also down>
    New server ETA: 2 days
     
    Andrew Poelstra, Jul 27, 2006
    #5
  6. Mockey Chen wrote:
    > 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?


    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;
    }

    >
    > Thanks in advance.


    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
    response,
     
    Martin Ambuhl, Jul 27, 2006
    #6
  7. Mockey Chen wrote:
    > I using #define as following:
    > #include <stdio.h>
    > #define LOG_PREFIX "Current function: <" __FUNCTION__ ">: "


    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.

    > int main()
    > {
    > printf(LOG_PREFIX "some thing.\n");
     
    lovecreatesbeauty, Jul 27, 2006
    #7
  8. On Thu, 27 Jul 2006, Martin Ambuhl wrote:

    > Mockey Chen wrote:
    >> Thanks in advance.

    >
    > 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 response,


    Totally uncalled for, bad netiquette, and off-topic.

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

    Default User Guest

    Kenneth Brody wrote:

    > (Top-posting corrected.)
    >
    > mdler wrote:
    > > Mockey Chen schreef:


    > > > func_test.c:6: error: syntax error before "__FUNCTION__"


    > 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:



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




    Brian
     
    Default User, Jul 27, 2006
    #9
  10. Mockey Chen

    jacob navia Guest

    Martin Ambuhl wrote:
    > Mockey Chen wrote:

    [snip]
    >> Thanks in advance.

    >
    >
    > 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
    > response,
    >


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

    You were polite, and you are welcome!

    jacob
     
    jacob navia, Jul 27, 2006
    #10
  11. jacob navia <> writes:

    > Martin Ambuhl wrote:
    >> Mockey Chen wrote:

    > [snip]
    >>> Thanks in advance.

    >>
    >>
    >> 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 response,
    >>

    >
    > Please Mr Chen, do not think that all people in this group are like
    > this person.
    >
    > You were polite, and you are welcome!


    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
     
    John Devereux, Jul 27, 2006
    #11
  12. Martin Ambuhl <> writes:
    > Mockey Chen wrote:

    [...]
    >> Thanks in advance.

    >
    > Why? Are you too f***ing important to be bothered with us after you
    > get your answer? Or are you a bill collector trying to imply you are
    > owed a response,


    [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
    answer?"

    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 (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Jul 27, 2006
    #12
  13. On Thu, 27 Jul 2006 11:16:31 -0400, in comp.lang.c , Martin Ambuhl
    <> wrote:

    >Mockey Chen wrote:
    >> I using #define as following:
    >>
    >> Thanks in advance.

    >
    >Why? Are you too <expletive removed> important to be bothered with us after you get
    >your answer?


    This is uncalled for, and rude. Martin you need to think before you
    post this sort of stuff.

    >Or are you a bill collector trying to imply you are owed a
    >response,


    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
    #13
  14. On Thu, 27 Jul 2006 20:18:37 GMT, in comp.lang.c , Keith Thompson
    <> wrote:

    >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.


    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.

    >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.


    <aol>
    --
    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
  15. Mockey Chen

    jacob navia Guest

    Mark McIntyre wrote:
    > On Thu, 27 Jul 2006 20:18:37 GMT, in comp.lang.c , Keith Thompson
    > <> wrote:
    >
    >
    >>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.

    >
    >
    > I fail to see how this can be, by any stretch of the imagination.


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

    jacob
     
    jacob navia, Jul 27, 2006
    #15
  16. Quoting order corrected.

    mdler wrote:
    > Mockey Chen schreef:
    >
    > > 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?

    >
    > why so complicated


    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.

    > just do it like this
    >
    > #include <stdio.h>
    >
    > int main ()
    > {
    > printf("Current function <%s>\n",__FUNCTION__);
    > return 0;
    > }


    Why so complicated? Why do at run-time what can be simply and cleanly
    done at compile time?

    > doing things your way it won't work, strings can not be added to
    > eachother in C


    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";

    > There has to be always memory to hold it!


    Obviously, but what does that have to do with anything?

    > 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);
    > }


    Not at all better - that requires extra function calls and extra work
    in printf().
     
    J. J. Farrell, Jul 27, 2006
    #16
  17. Martin Ambuhl wrote:
    > Mockey Chen wrote:
    > >
    > > Thanks in advance.

    >
    > 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
    > response,


    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
    #17
  18. Mark McIntyre wrote:
    > Keith Thompson <> wrote:
    > >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.

    >
    > I fail to see how this can be, by any stretch of the imagination.


    Perhaps you have no imagination. ;-)

    > Its merely being polite and no different to saying "have a nice day" ...


    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
     
    Peter Nilsson, Jul 28, 2006
    #18
  19. On 27 Jul 2006 16:51:52 -0700, in comp.lang.c , "Peter Nilsson"
    <> wrote:

    >Mark McIntyre wrote:
    >> Its merely being polite and no different to saying "have a nice day" ...

    >
    >Ironic choice of phrase because it's one that I hate hearing from
    >Americans!


    Me too, but I don't generally respond by telling the other party to
    fsck off.

    >Martin has is manic moments. Perhaps he's just auditioning for the
    >US version of "Grumpy Old Men". <g>


    :)

    --
    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
    #19
  20. "Mockey Chen" <> writes:
    > "Martin Ambuhl" <>
    > wrote:<>...

    [...]
    >> 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:
    >>

    > when I use C, there is no C99, only C89 exist, C99 has lots of new feature,
    > but not every compiler support it. I thing C89 will be exist years.
    > I known most of compiler support __FUNCTION__, such as gcc, msvc, xlc...
    > I use gcc, and it also predefine such a marco.


    __FUNCTION__ is exactly as non-standard in C89 (which I prefer to call
    C90) as it is in C99.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Jul 29, 2006
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dead RAM
    Replies:
    20
    Views:
    1,119
    John Harrison
    Jul 14, 2004
  2. Replies:
    1
    Views:
    314
  3. Neo
    Replies:
    9
    Views:
    22,248
  4. somenath
    Replies:
    1
    Views:
    664
    Richard Bos
    Aug 2, 2007
  5. Ravi

    __FUNCTION__

    Ravi, Sep 27, 2007, in forum: C Programming
    Replies:
    7
    Views:
    635
    Richard Bos
    Sep 28, 2007
Loading...

Share This Page