Preprocessor string pasting fun

This is a discussion on Preprocessor string pasting fun within the C Programming forums, part of the General Programming Boards category; Hi guys, my first post here. I'm having trouble doing string concatenation at the preprocessor level. This works fine : ...

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

    Preprocessor string pasting fun

    Hi guys, my first post here.

    I'm having trouble doing string concatenation at the preprocessor level.

    This works fine :
    Code:
    #define A "TOKEN1" "TOKEN2"
    and this also works fine:
    Code:
    #define A "TOKEN1"
    #define B "TOKEN2" A
    but this refuses to compile :
    Code:
    #define A "TOKEN1" __FUNCTION__
    The only difference seems to be that __FUNCTION__ is a builtin.

    Is there any other way to do this kind of concatenation? It's important that this happens on the preprocessor. BTW, this is with g++ 3.2.

    Thanks in advance!
    Last edited by ggambett; 11-08-2004 at 03:29 PM.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Are you putting the definition inside of a function?

    Code:
    #include <stdio.h>
    
    void foo(void)
    {
    #define A "TOKEN1" __FUNCTION__
       puts(A);
    }
    
    int main(void)
    {
       foo();
       return 0;
    }
    
    /* my output
    TOKEN1foo
    */
    Last edited by Dave_Sinkula; 11-08-2004 at 03:39 PM. Reason: Added sample code.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    4
    Yes. I investigated a little further, and came to this : the following code
    Code:
    #define FOOBAR const char* __fnname = __FUNCTION__ "A";
    #define QUUX const char* __fnname = __FILE__ "A";
    
    int main (void)
    {
    	FOOBAR
    	QUUX
    	
    	return 1;
    }
    is preprocessed as
    Code:
    # 1 "test.cpp"
    # 1 "<built-in>"
    # 1 "<command line>"
    # 1 "test.cpp"
    
    int main (void)
    {
            const char* __fnname = __FUNCTION__ "A";
            const char* __fnname = "test.cpp" "A";
    
            return 1;
    }
    As you can see, __FILE__ is expanded by the preprocessor but __FUNCTION__ isn't (maybe it makes sense...). If I put __FUNCTION__ inside main() instead of through a macro the result is the same

  4. #4
    Registered User
    Join Date
    Nov 2004
    Posts
    4
    Sorry for the confusion. "If I put __FUNCTION__ inside main() instead of through a macro the result is the same" meant yes.

    Anyway, I now believe there's no solution - concatenating __FUNCTION__ with a string literal seems to be deprecated, as mentioned vaguely in some mailing lists.

    Thanks anyway...

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,586
    There's good reason that is no longer supported nor...hopefully...used.

  6. #6
    Registered User
    Join Date
    Nov 2004
    Posts
    4
    Quote Originally Posted by Bubba
    There's good reason that is no longer supported nor...hopefully...used.
    And what is that reason?

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why are ypu compiling your C code as C++? Or, why ask a C++ question on a C board? Different compilers do things differently.

    If you are in fact using C and not C++, compile it that way. Also, if indeed that is true, C99 introduces a predefined identifier called __func__.
    C: A Reference Manual, 5th Edition
    Pages 23 and 24.

    2.6.1 Predefined Identifiers

    Although not a keyword, C99 introduces the concept of a predefined identicier and defines
    one such: __func__. Unlike a predefined macro, a predefined identifier can follow nor-
    mal block scoping rules. Like keywords, predefined identifiers must not be defined by
    programmers.

    The identifier __func__ is implicitly declared by C99 implementations as if the
    following declaration appeared after the opening brace of each function definition:
    Code:
    static const char __func__[] = "function-name";
    This identifier could be used by debugging tools to peint out the name of the enclosing
    function, as in:
    Code:
    if (failed) printf("Function %s failed\n", __func__ );
    When translating C programs for targets with tight memory constraints, C implementa-
    tions will have to be careful about getting rid of these strings if they are not needed at run-
    time.
    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Auto Calling Derived Fun
    By Arlenik in forum C++ Programming
    Replies: 27
    Last Post: 06-02-2008, 11:17 AM
  2. Programming for fun and profit
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 41
    Last Post: 12-16-2004, 03:52 PM
  3. Fun Learning a New Language
    By UnregdRegd in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 09-30-2003, 10:03 PM
  4. Token pasting and a little more
    By Mario in forum C++ Programming
    Replies: 9
    Last Post: 05-23-2002, 05:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21