Print the function name using __FUNCTION__ macro

Discussion in 'C Programming' started by somenath, Aug 2, 2007.

  1. somenath

    somenath Guest

    Hi ALL ,

    I need to print the name of the function as my debug message. I have
    tried the following way
    #include<stdio.h>
    #include<stdlib.h>

    #define __STR(x) _VAL(x)
    #define _VAL(x) #x
    int main (void)
    {
    puts( __FUNCTION__ __STR( __FILE__));
    return 0;

    }
    But while compiling the program I am getting the following warning .

    simple.c: In function `main':
    simple.c:8: warning: concatenation of string literals with
    __FUNCTION__ is deprecated

    Is there any correct way I can print the function name with file
    name ?

    Regards,
    Somenath
     
    somenath, Aug 2, 2007
    #1
    1. Advertisements

  2. somenath

    Richard Bos Guest

    You are not allowed to declare identifiers, not even macros, starting
    with __ or _ followed by a capital. (And it is a bad idea, because only
    legal in some places, to declare an identifier starting with _ and
    something else.) Only the implementation itself is allowed to do that.
    There's no such thing as __FUNCTION__ in ISO C. There is __func__,
    though, which is not a macro and not a string literal but an identifier
    which declares a static const char array containing the function name.
    Since __func__ is not a string literal, you can't concatenate it with
    another string literal, but what you can do is

    printf("%s %s\n", __func__, __FILE__);

    or anything equivalent.

    Richard
     
    Richard Bos, Aug 2, 2007
    #2
    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.