Prepend name to function names?

Discussion in 'C Programming' started by dspfun, Jan 29, 2008.

  1. dspfun

    dspfun Guest

    I was asked whether it is possible to prepend a name (using macro
    substitution) to all function names in a file/program.
    For example, the functions f1(), f2(), f3() should get aa_ prepended
    to their names?

    I.e., f1(), f2(), f3() should be changed to aa_f1(), aa_f2(), aa_f3()
    using some macro substitution.

    I came up with the following:

    ****************************************************
    #include <stdio.h>

    #define PREPEND aa_
    #define MAKE_FUNC_NAME(prepend, func_name) prepend ## func_name

    void MAKE_FUNC_NAME(PREPEND, f1)(void)
    {
    printf("here..\n");
    }
    ****************************************************

    However, this does not define the function aa_f1, instead the function
    PREPENDf1 is defined. Why? Is there some way to get around this to
    achieve what is set out to be done?
    dspfun, Jan 29, 2008
    #1
    1. Advertising

  2. dspfun

    Ben Pfaff Guest

    dspfun <> writes:

    > #define PREPEND aa_
    > #define MAKE_FUNC_NAME(prepend, func_name) prepend ## func_name
    >
    > void MAKE_FUNC_NAME(PREPEND, f1)(void)
    > {
    > printf("here..\n");
    > }
    > ****************************************************
    >
    > However, this does not define the function aa_f1, instead the function
    > PREPENDf1 is defined. Why? Is there some way to get around this to
    > achieve what is set out to be done?


    This is in the C FAQ:

    11.17: I'm trying to use the ANSI "stringizing" preprocessing operator
    `#' to insert the value of a symbolic constant into a message,
    but it keeps stringizing the macro's name rather than its value.

    A: You can use something like the following two-step procedure to
    force a macro to be expanded as well as stringized:

    #define Str(x) #x
    #define Xstr(x) Str(x)
    #define OP plus
    char *opname = Xstr(OP);

    This code sets opname to "plus" rather than "OP".

    An equivalent circumlocution is necessary with the token-pasting
    operator ## when the values (rather than the names) of two
    macros are to be concatenated.

    References: ISO Sec. 6.8.3.2, Sec. 6.8.3.5.

    --
    char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
    ={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa67f6aaa,0xaa9aa9f6,0x11f6},*p
    =b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
    2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}
    Ben Pfaff, Jan 29, 2008
    #2
    1. Advertising

  3. dspfun

    dspfun Guest

    On 29 Jan, 21:07, Ben Pfaff <> wrote:
    > dspfun <> writes:
    > > #define PREPEND aa_
    > > #define MAKE_FUNC_NAME(prepend, func_name) prepend ## func_name

    >
    > > void MAKE_FUNC_NAME(PREPEND, f1)(void)
    > > {
    > >  printf("here..\n");
    > > }
    > > ****************************************************

    >
    > > However, this does not define the function aa_f1, instead the function
    > > PREPENDf1 is defined. Why? Is there some way to get around this to
    > > achieve what is set out to be done?

    >
    > This is in the C FAQ:
    >
    > 11.17:  I'm trying to use the ANSI "stringizing" preprocessing operator
    >         `#' to insert the value of a symbolic constant into a message,
    >         but it keeps stringizing the macro's name rather than its value.
    >


    It's almost the same, but not quite. What I am trying to do is to
    create the function name using macro substitution, however, the
    problem is that the PREPEND is never substituted for its corresponding
    #define inside the MAKE_FUNC_NAME macro.

    I.e.
    MAKE_FUNC_NAME(PREPEND, f1)(void);

    is by the preprocessor substituted to:
    PREPENDf1(void);

    What I am trying to achieve is to get MAKE_FUNC_NAME(PREPEND, f1)
    (void) substituted to:
    aa_f1(void);

    Then if for example one have 100 functions, f1()...f100, it is easy to
    just change the #define to get all functions named with aa_ , bb_ etc.
    prepended to them.
    dspfun, Jan 29, 2008
    #3
  4. On Tue, 29 Jan 2008 12:44:29 -0800, dspfun wrote:
    > On 29 Jan, 21:07, Ben Pfaff <> wrote:
    >> dspfun <> writes:
    >> > #define PREPEND aa_
    >> > #define MAKE_FUNC_NAME(prepend, func_name) prepend ## func_name

    >>
    >> > void MAKE_FUNC_NAME(PREPEND, f1)(void) {
    >> >  printf("here..\n");
    >> > }
    >> > ****************************************************

    >>
    >> > However, this does not define the function aa_f1, instead the
    >> > function PREPENDf1 is defined. Why? Is there some way to get around
    >> > this to achieve what is set out to be done?

    >>
    >> This is in the C FAQ:
    >>
    >> 11.17:  I'm trying to use the ANSI "stringizing" preprocessing operator
    >>         `#' to insert the value of a symbolic constant into a
    >>         message, but it keeps stringizing the macro's name rather
    >>         than its value.
    >>
    >>

    > It's almost the same, but not quite. [...]


    Correct. However, the fix for the ## operator is exactly the same as the
    fix for the # operator: add an extra macro. PREPEND gets expanded as long
    as you don't perform the concatenation in MAKE_FUNC_NAME itself, like so:

    #define PREPEND aa_
    #define MAKE_FUNC_NAME_HELPER(x, y) x ## y
    #define MAKE_FUNC_NAME(prepend, func_name) MAKE_FUNC_NAME_HELPER(prepend,
    func_name)

    void MAKE_FUNC_NAME(PREPEND, f1)(void) {
    printf("here..\n");
    }
    Harald van Dijk, Jan 29, 2008
    #4
  5. dspfun

    dspfun Guest

    On 29 Jan, 21:55, Harald van Dijk <> wrote:
    > On Tue, 29 Jan 2008 12:44:29 -0800, dspfun wrote:
    > > On 29 Jan, 21:07, Ben Pfaff <> wrote:
    > >> dspfun <> writes:
    > >> > #define PREPEND aa_
    > >> > #define MAKE_FUNC_NAME(prepend, func_name) prepend ## func_name

    >
    > >> > void MAKE_FUNC_NAME(PREPEND, f1)(void) {
    > >> >  printf("here..\n");
    > >> > }
    > >> > ****************************************************

    >
    > >> > However, this does not define the function aa_f1, instead the
    > >> > function PREPENDf1 is defined. Why? Is there some way to get around
    > >> > this to achieve what is set out to be done?

    >
    > >> This is in the C FAQ:

    >
    > >> 11.17:  I'm trying to use the ANSI "stringizing" preprocessing operator
    > >>         `#' to insert the value of a symbolic constant into a
    > >>         message, but it keeps stringizing the macro's name rather
    > >>         than its value.

    >
    > > It's almost the same, but not quite. [...]

    >
    > Correct. However, the fix for the ## operator is exactly the same as the
    > fix for the # operator: add an extra macro. PREPEND gets expanded as long
    > as you don't perform the concatenation in MAKE_FUNC_NAME itself, like so:
    >
    > #define PREPEND aa_
    > #define MAKE_FUNC_NAME_HELPER(x, y) x ## y
    > #define MAKE_FUNC_NAME(prepend, func_name) MAKE_FUNC_NAME_HELPER(prepend,
    > func_name)
    >
    > void MAKE_FUNC_NAME(PREPEND, f1)(void) {
    >   printf("here..\n");
    >
    >
    >
    > }- Dölj citerad text -
    >
    > - Visa citerad text -- Dölj citerad text -
    >
    > - Visa citerad text -


    Ok, thanks! What is the reason it's not possible to perform the
    conatenation in MAKE_FUNC_NAME itself? Which sentence(s) in the
    standard (n1256.pdf) specifies this?
    dspfun, Jan 29, 2008
    #5
  6. On Tue, 29 Jan 2008 13:12:49 -0800, dspfun wrote:
    > On 29 Jan, 21:55, Harald van Dijk <> wrote:
    >> >> dspfun <> writes:
    >> >> > #define PREPEND aa_
    >> >> > #define MAKE_FUNC_NAME(prepend, func_name) prepend ## func_name

    [quoted text below edited for readability]
    >> #define PREPEND aa_
    >> #define MAKE_FUNC_NAME_HELPER(x, y) x ## y
    >> #define MAKE_FUNC_NAME(prepend, func_name) \
    >> MAKE_FUNC_NAME_HELPER(prepend, func_name)

    >
    > Ok, thanks! What is the reason it's not possible to perform the
    > conatenation in MAKE_FUNC_NAME itself?


    The rationale states:

    "The specification of this pasting operator is based on these principles:
    [...]
    * A formal parameter as an operand for ## is not expanded before pasting.
    The actual parameter is substituted for the formal parameter; but the
    actual parameter is not expanded. Given, for example
    #define a(n) aaa ## n
    #define b 2
    the expansion of a(b) is aaab, not aaa2 or aaan.
    [...]
    These principles codify the essential features of prior art and are
    consistent with the specification of the stringizing operator."

    In other words, there was no specific reason for or against expansion of
    macro arguments, and existing implementations didn't expand macro
    arguments, so that's what got into the standard.

    > Which sentence(s) in the standard
    > (n1256.pdf)


    I expect you'll get comments focusing solely on this.

    > specifies this?


    The standard specifies this in 6.10.3.1p1:
    "After the arguments for the invocation of a function-like macro have
    been identified, argument substitution takes place. A parameter in the
    replacement list, unless preceded by a # or ## preprocessing token or
    followed by a ## preprocessing token (see below), is replaced by the
    corresponding argument after all macros contained therein have been
    expanded."
    Harald van Dijk, Jan 29, 2008
    #6
  7. dspfun

    dspfun Guest

    On 29 Jan, 22:39, Harald van D©¦k <> wrote:
    > On Tue, 29 Jan 2008 13:12:49 -0800, dspfun wrote:
    > > On 29 Jan, 21:55, Harald van D©¦k <> wrote:
    > >> >> dspfun <> writes:
    > >> >> > #define PREPEND aa_
    > >> >> > #define MAKE_FUNC_NAME(prepend, func_name) prepend ## func_name

    >
    > [quoted text below edited for readability]
    >
    > >> #define PREPEND aa_
    > >> #define MAKE_FUNC_NAME_HELPER(x, y) x ## y
    > >> #define MAKE_FUNC_NAME(prepend, func_name) \
    > >> MAKE_FUNC_NAME_HELPER(prepend, func_name)

    >
    > > Ok, thanks! What is the reason it's not possible to perform the
    > > conatenation in MAKE_FUNC_NAME itself?

    >
    > The rationale states:
    >
    > "The specification of this pasting operator is based on these principles:
    > [...]
    > * A formal parameter as an operand for ## is not expanded before pasting.
    > The actual parameter is substituted for the formal parameter; but the
    > actual parameter is not expanded. Given, for example
    > #define a(n) aaa ## n
    > #define b 2
    > the expansion of a(b) is aaab, not aaa2 or aaan.
    > [...]
    > These principles codify the essential features of prior art and are
    > consistent with the specification of the stringizing operator."
    >
    > In other words, there was no specific reason for or against expansion of
    > macro arguments, and existing implementations didn't expand macro
    > arguments, so that's what got into the standard.
    >
    > > Which sentence(s) in the standard
    > > (n1256.pdf)

    >
    > I expect you'll get comments focusing solely on this.
    >
    > > specifies this?

    >
    > The standard specifies this in 6.10.3.1p1:
    > "After the arguments for the invocation of a function-like macro have
    > been identified, argument substitution takes place. A parameter in the
    > replacement list, unless preceded by a # or ## preprocessing token or
    > followed by a ## preprocessing token (see below), is replaced by the
    > corresponding argument after all macros contained therein have been
    > expanded."


    Thank you very much!
    dspfun, Jan 29, 2008
    #7
  8. dspfun

    Ben Pfaff Guest

    dspfun <> writes:

    > On 29 Jan, 21:07, Ben Pfaff <> wrote:
    >> dspfun <> writes:
    >> > #define PREPEND aa_
    >> > #define MAKE_FUNC_NAME(prepend, func_name) prepend ## func_name

    >>
    >> > void MAKE_FUNC_NAME(PREPEND, f1)(void)
    >> > {
    >> >  printf("here..\n");
    >> > }
    >> > ****************************************************

    >>
    >> > However, this does not define the function aa_f1, instead the function
    >> > PREPENDf1 is defined. Why? Is there some way to get around this to
    >> > achieve what is set out to be done?

    >>
    >> This is in the C FAQ:
    >>
    >> 11.17:  I'm trying to use the ANSI "stringizing" preprocessing operator
    >>         `#' to insert the value of a symbolic constant into a message,
    >>         but it keeps stringizing the macro's name rather than its value.
    >>

    >
    > It's almost the same, but not quite. What I am trying to do is to
    > create the function name using macro substitution, however, the
    > problem is that the PREPEND is never substituted for its corresponding
    > #define inside the MAKE_FUNC_NAME macro.


    It's pretty clear that you didn't read the whole answer in the
    FAQ:

    An equivalent circumlocution is necessary with the token-pasting
    operator ## when the values (rather than the names) of two
    macros are to be concatenated.

    --
    char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
    ={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa67f6aaa,0xaa9aa9f6,0x11f6},*p
    =b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
    2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}
    Ben Pfaff, Jan 29, 2008
    #8
  9. dspfun

    dspfun Guest

    On 29 Jan, 23:15, Ben Pfaff <> wrote:
    > dspfun <> writes:
    > > On 29 Jan, 21:07, Ben Pfaff <> wrote:
    > >> dspfun <> writes:
    > >> > #define PREPEND aa_
    > >> > #define MAKE_FUNC_NAME(prepend, func_name) prepend ## func_name

    >
    > >> > void MAKE_FUNC_NAME(PREPEND, f1)(void)
    > >> > {
    > >> >  printf("here..\n");
    > >> > }
    > >> > ****************************************************

    >
    > >> > However, this does not define the function aa_f1, instead the function
    > >> > PREPENDf1 is defined. Why? Is there some way to get around this to
    > >> > achieve what is set out to be done?

    >
    > >> This is in the C FAQ:

    >
    > >> 11.17:  I'm trying to use the ANSI "stringizing" preprocessing operator
    > >>         `#' to insert the value of a symbolic constant into a message,
    > >>         but it keeps stringizing the macro's name rather than its value.

    >
    > > It's almost the same, but not quite. What I am trying to do is to
    > > create the function name using macro substitution, however, the
    > > problem is that the PREPEND is never substituted for its corresponding
    > > #define inside the MAKE_FUNC_NAME macro.

    >
    > It's pretty clear that you didn't read the whole answer in the
    > FAQ:
    >
    >         An equivalent circumlocution is necessary with the token-pasting
    >         operator ## when the values (rather than the names) of two
    >         macros are to be concatenated.
    >
    > --
    > char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
    > ={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa67f6aaa,0xaa9aa9f6,0x11f6}­,*p
    > =b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
    > 2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}- Dölj citerad text -
    >
    > - Visa citerad text -


    I saw that, my bad.. Thanks for your help!
    dspfun, Jan 29, 2008
    #9
    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. Replies:
    0
    Views:
    2,197
  2. flamesrock

    how to prepend string to a string?

    flamesrock, May 31, 2005, in forum: Python
    Replies:
    6
    Views:
    16,109
    newcoder
    May 31, 2005
  3. Replies:
    37
    Views:
    2,056
    Flash Gordon
    Jan 9, 2006
  4. bdb112
    Replies:
    6
    Views:
    4,248
    Robert Kern
    Jul 25, 2009
  5. Joan Miller

    Prepend to logging message

    Joan Miller, Jan 10, 2010, in forum: Python
    Replies:
    7
    Views:
    1,066
    Joan Miller
    Jan 10, 2010
Loading...

Share This Page