function name printing

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kumaravel

    #1

    function name printing

    Can I print the current function name in c? something like __LINE__?

    for e.g

    can I do

    printf( " the current file is %s : function is : %s and line # is
    :%d\n", __FILE__, __FUNC__, __LINE__);


    thanks
    kumaravel.
  • Harti Brandt

    #2
    Re: function name printing

    On Mon, 5 Jul 2004, Kumaravel wrote:

    K>Can I print the current function name in c? something like __LINE__?
    K>
    K>for e.g
    K>
    K>can I do
    K>
    K>printf( " the current file is %s : function is : %s and line # is
    K>:%d\n", __FILE__, __FUNC__, __LINE__);

    __func__ is a a const char * and points to the current function name (for
    C99). Gcc supported __FUNCTION__ and __PRETTY_FUNCTI ON__, but that's not
    standard and has been deprecated.

    harti

    Comment

    • jacob navia

      #3
      Re: function name printing


      "Harti Brandt" <brandt@dlr.d e> a écrit dans le message de
      news:2004070513 2112.M90892@bea gle.kn.op.dlr.d e...[color=blue]
      > On Mon, 5 Jul 2004, Kumaravel wrote:
      >
      > K>Can I print the current function name in c? something like __LINE__?
      > K>
      > K>for e.g
      > K>
      > K>can I do
      > K>
      > K>printf( " the current file is %s : function is : %s and line # is
      > K>:%d\n", __FILE__, __FUNC__, __LINE__);
      >
      > __func__ is a a const char * and points to the current function name (for
      > C99). Gcc supported __FUNCTION__ and __PRETTY_FUNCTI ON__, but that's not
      > standard and has been deprecated.
      >
      > harti[/color]

      What was __PRETTY_FUNCTI ON__ ???

      Weird name ... :-)


      Comment

      • Dominik Zanettin

        #4
        Re: function name printing

        On Mon, 05 Jul 2004 04:20:18 -0700, Kumaravel wrote:
        [color=blue]
        > Can I print the current function name in c? something like __LINE__?
        > for e.g[/color]

        yes, in C99 you can use the predefined identifier __func__

        regards
        zhan


        Comment

        • Harti Brandt

          #5
          Re: function name printing

          On Mon, 5 Jul 2004, jacob navia wrote:

          jn>
          jn>"Harti Brandt" <brandt@dlr.d e> a écrit dans le message de
          jn>news:2004070 5132112.M90892@ beagle.kn.op.dl r.de...
          jn>> On Mon, 5 Jul 2004, Kumaravel wrote:
          jn>>
          jn>> K>Can I print the current function name in c? something like __LINE__?
          jn>> K>
          jn>> K>for e.g
          jn>> K>
          jn>> K>can I do
          jn>> K>
          jn>> K>printf( " the current file is %s : function is : %s and line # is
          jn>> K>:%d\n", __FILE__, __FUNC__, __LINE__);
          jn>>
          jn>> __func__ is a a const char * and points to the current function name (for
          jn>> C99). Gcc supported __FUNCTION__ and __PRETTY_FUNCTI ON__, but that's not
          jn>> standard and has been deprecated.
          jn>>
          jn>> harti
          jn>
          jn>What was __PRETTY_FUNCTI ON__ ???

          That gives you the complete function signature (including name space and
          classes in C++).

          harti

          Comment

          • Martin Ambuhl

            #6
            Re: function name printing

            Kumaravel wrote:
            [color=blue]
            > Can I print the current function name in c? something like __LINE__?
            >
            > for e.g
            >
            > can I do
            >
            > printf( " the current file is %s : function is : %s and line # is
            > :%d\n", __FILE__, __FUNC__, __LINE__);[/color]

            #include <stdio.h>

            #define FTRACE printf( " the current file is %s, " \
            "function is %s, and line # is %d\n", \
            __FILE__, __func__, __LINE__)

            void foo1(void)
            {
            FTRACE;
            }

            void foo2(void)
            {
            FTRACE;
            }

            int main(void)
            {
            foo1();
            foo2();
            return 0;
            }

            [output]

            the current file is a.c, function is foo1, and line # is 9
            the current file is a.c, function is foo2, and line # is 14

            Comment

            • Martin Ambuhl

              #7
              Re: function name printing

              jacob navia wrote:
              [color=blue]
              > "Harti Brandt" <brandt@dlr.d e> a écrit dans le message de
              > news:2004070513 2112.M90892@bea gle.kn.op.dlr.d e...[/color]
              [color=blue][color=green]
              >>__func__ is a a const char * and points to the current function name (for
              >>C99). Gcc supported __FUNCTION__ and __PRETTY_FUNCTI ON__, but that's not
              >>standard and has been deprecated.[/color][/color]
              [color=blue]
              > What was __PRETTY_FUNCTI ON__ ???
              >
              > Weird name ... :-)[/color]

              Doubly off-topic: particular compiler (gcc) and wrong language (not C).
              Used to print the function name as it appears in the source
              (__FUNCTION__) or "in a language-specific way" (__PRETTY_FUNCT ION__).
              For C++, for example, __PRETTY_FUNCTI ON__ yields scoping information.

              Comment

              • Old Wolf

                #8
                Re: function name printing

                Harti Brandt <brandt@dlr.d e> wrote:[color=blue]
                > On Mon, 5 Jul 2004, Kumaravel wrote:
                >
                > K>Can I print the current function name in c? something like __LINE__?
                > K>
                > K>for e.g
                > K>
                > K>can I do
                > K>
                > K>printf( " the current file is %s : function is : %s and line # is
                > K>:%d\n", __FILE__, __FUNC__, __LINE__);
                >
                > __func__ is a a const char * and points to the current function name (for
                > C99).[/color]

                Actually it is a const char [] containing the current function name
                (so you can do "sizeof" on it, and take its address, etc.)

                Comment

                Working...