Thread: How to get the current function name?

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    3

    Question How to get the current function name?

    Is there anyway that I can get the current function name?
    Like, if main() calls to function named "doit()".
    When I'm in "doit()", I want to print out what the function name is.
    It sounds silly because I can just printf("doit()\n");
    But... is there any other ways??

    Thank you.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well that depends on your compiler...

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    As Salem stated. If you have a C99 compiler, you can use __func__ as described here.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Nov 2004
    Posts
    3
    It works!! Thank you.

    I have one more question.
    What about passing this parameter?
    I want to pass this parameter to another function, which it will print who calls it.

    I try to pass __func__ as a parameter for (char *), but it doesn't work.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    It works fine for me:
    Code:
    itsme@itsme:~/C$ cat func.c
    #include <stdio.h>
    
    void doit(const char *name)
    {
      puts(name);
    }
    
    int main(void)
    {
      doit(__func__);
      return 0;
    }
    itsme@itsme:~/C$ ./func
    main
    itsme@itsme:~/C$
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Nov 2004
    Posts
    3
    Oh, it works now too.
    I put doit(char *name) instead of doit(const char* name)
    Now, I changed it and it works.

    Thank you everybody

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  2. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. illegal function overloading????
    By Mr_Jack in forum C++ Programming
    Replies: 3
    Last Post: 12-17-2003, 01:03 PM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM