C preprocessor conundrum

Discussion in 'C Programming' started by Jim Ford, Nov 1, 2003.

  1. Jim Ford

    Jim Ford Guest

    I have a single C file with the following code:

    int f2()
    {
    /* Blah-blah */
    }

    int f1()
    {
    /* Blah-blah */

    f2() ;

    /* Reblah-blah */
    }

    Is it possible, by means of the C processor, to arrange things in such a
    way that, after preprocessing, the int f1() and int f2() lines will be
    replaced by int F1(), int F2(), respectively, whereas the invocation to
    f2() from f1() (F1(), after the replacement) will remain unchanged? That
    is, after preprocessing we would have

    int F2()
    {
    /* Preprocessed blah-blah */
    }

    int F1()
    {
    /* Preprocessed blah-blah */

    f2() ;

    /* Preprocessed reblah-blah */
    }

    All the necessary preprocessor directives would have to be in a file to
    be included at the top of this one here.
    Jim Ford, Nov 1, 2003
    #1
    1. Advertising

  2. Jim Ford

    Frank Roland Guest

    "Jim Ford" <> schrieb im Newsbeitrag
    news:p...
    > I have a single C file with the following code:
    >
    > int f2()
    > {
    > /* Blah-blah */
    > }
    >
    > int f1()
    > {
    > /* Blah-blah */
    >
    > f2() ;
    >
    > /* Reblah-blah */
    > }
    >
    > Is it possible, by means of the C processor, to arrange things in such a
    > way that, after preprocessing, the int f1() and int f2() lines will be
    > replaced by int F1(), int F2(), respectively, whereas the invocation to
    > f2() from f1() (F1(), after the replacement) will remain unchanged? That
    > is, after preprocessing we would have
    >
    > int F2()
    > {
    > /* Preprocessed blah-blah */
    > }
    >
    > int F1()
    > {
    > /* Preprocessed blah-blah */
    >
    > f2() ;
    >
    > /* Preprocessed reblah-blah */
    > }
    >
    > All the necessary preprocessor directives would have to be in a file to
    > be included at the top of this one here.


    You can't do that. The reason is that
    a) you can not define macros with spaces in it, which would be
    necessary to have a int_f1 macro, where _ is space
    b) the preprocessor does not know about a context, i.e. he can not tell
    wheter a makro f1 is a function declaration or a call to a function in a
    definition

    To achive this you might be better off with external tools like awk and sed,
    if you are under a unix environment (otherwise you might install cygwin), or
    you might use a perl script.

    Hope it helped.

    Kind regards,
    Frank Roland
    Frank Roland, Nov 1, 2003
    #2
    1. Advertising

  3. Groovy hepcat Frank Roland was jivin' on Sat, 1 Nov 2003 20:33:37
    +0100 in comp.lang.c.
    Re: C preprocessor conundrum's a cool scene! Dig it!

    >"Jim Ford" <> schrieb im Newsbeitrag
    >news:p...
    >> I have a single C file with the following code:


    #define f1 F1
    #define f2 F2

    >> int f2()
    >> {
    >> /* Blah-blah */
    >> }
    >>
    >> int f1()
    >> {
    >> /* Blah-blah */


    #undef f2

    >> f2() ;
    >>
    >> /* Reblah-blah */
    >> }
    >>
    >> Is it possible, by means of the C processor, to arrange things in such a
    >> way that, after preprocessing, the int f1() and int f2() lines will be
    >> replaced by int F1(), int F2(), respectively, whereas the invocation to
    >> f2() from f1() (F1(), after the replacement) will remain unchanged? That
    >> is, after preprocessing we would have
    >>
    >> int F2()
    >> {
    >> /* Preprocessed blah-blah */
    >> }
    >>
    >> int F1()
    >> {
    >> /* Preprocessed blah-blah */
    >>
    >> f2() ;
    >>
    >> /* Preprocessed reblah-blah */
    >> }
    >>
    >> All the necessary preprocessor directives would have to be in a file to
    >> be included at the top of this one here.

    >
    >You can't do that. The reason is that


    Piffle! You can do it, given the right macros, as demonstrated
    above.

    > a) you can not define macros with spaces in it, which would be
    >necessary to have a int_f1 macro, where _ is space


    Nonsense! Macro names may not contain spaces (just like all
    identifiers), but macro replacement text may contain spaces. But this
    is irrelevant.

    > b) the preprocessor does not know about a context, i.e. he can not tell
    >wheter a makro f1 is a function declaration or a call to a function in a
    >definition


    True, but, once again, irrelevant.

    >To achive this you might be better off with external tools like awk and sed,


    Possibly. But he asked for a C answer using the preprocessor.

    >if you are under a unix environment (otherwise you might install cygwin), or
    >you might use a perl script.


    Those tools exist not only on Unix.

    >Hope it helped.


    I fail to see how telling him what he wanted to do can't be done the
    way he wanted to do it would help.

    --

    Dig the even newer still, yet more improved, sig!

    http://alphalink.com.au/~phaywood/
    "Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
    I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
    Peter Shaggy Haywood, Nov 6, 2003
    #3
  4. Jim Ford

    Anupam Guest

    (Peter "Shaggy" Haywood) wrote in message news:<>...
    > Groovy hepcat Frank Roland was jivin' on Sat, 1 Nov 2003 20:33:37
    > +0100 in comp.lang.c.
    > Re: C preprocessor conundrum's a cool scene! Dig it!
    >
    > >"Jim Ford" <> schrieb im Newsbeitrag
    > >news:p...
    > >> I have a single C file with the following code:

    >
    > #define f1 F1
    > #define f2 F2
    >
    > >> int f2()
    > >> {
    > >> /* Blah-blah */
    > >> }
    > >>
    > >> int f1()
    > >> {
    > >> /* Blah-blah */

    >
    > #undef f2
    >
    > >> f2() ;
    > >>
    > >> /* Reblah-blah */
    > >> }
    > >>
    > >> Is it possible, by means of the C processor, to arrange things in such a
    > >> way that, after preprocessing, the int f1() and int f2() lines will be
    > >> replaced by int F1(), int F2(), respectively, whereas the invocation to
    > >> f2() from f1() (F1(), after the replacement) will remain unchanged? That
    > >> is, after preprocessing we would have
    > >>
    > >> int F2()
    > >> {
    > >> /* Preprocessed blah-blah */
    > >> }
    > >>
    > >> int F1()
    > >> {
    > >> /* Preprocessed blah-blah */
    > >>
    > >> f2() ;
    > >>
    > >> /* Preprocessed reblah-blah */
    > >> }
    > >>
    > >> All the necessary preprocessor directives would have to be in a file to
    > >> be included at the top of this one here.


    I am afraid that your solution doesn't quite fulfil the
    requirements of the OP. It does achieve the required result but he had
    also said that "all the necessary preprocessor directives would have
    to be in a file to be included at the top of this one here". Which
    would mean that the #undef would have to be right below the #define
    which would defeat the purpose of the macros. I do believe Jim got it
    spot on. From the original post, it would seem fairly evident that the
    OP was talking about replacing the names only at the function calls.
    This would need context sensitivity... thats the context of this
    question :) (Pun intended).

    > >
    > >You can't do that. The reason is that

    >
    > Piffle! You can do it, given the right macros, as demonstrated
    > above.
    >
    > > a) you can not define macros with spaces in it, which would be
    > >necessary to have a int_f1 macro, where _ is space

    >
    > Nonsense! Macro names may not contain spaces (just like all
    > identifiers), but macro replacement text may contain spaces. But this
    > is irrelevant.
    >
    > > b) the preprocessor does not know about a context, i.e. he can not tell
    > >wheter a makro f1 is a function declaration or a call to a function in a
    > >definition

    >
    > True, but, once again, irrelevant.
    >
    > >To achive this you might be better off with external tools like awk and sed,

    >
    > Possibly. But he asked for a C answer using the preprocessor.
    >
    > >if you are under a unix environment (otherwise you might install cygwin), or
    > >you might use a perl script.

    >
    > Those tools exist not only on Unix.
    >
    > >Hope it helped.

    >
    > I fail to see how telling him what he wanted to do can't be done the
    > way he wanted to do it would help.
    Anupam, Nov 6, 2003
    #4
  5. Groovy hepcat Anupam was jivin' on 6 Nov 2003 08:42:36 -0800 in
    comp.lang.c.
    Re: C preprocessor conundrum's a cool scene! Dig it!

    > (Peter "Shaggy" Haywood) wrote in message news:<>...
    >> Groovy hepcat Frank Roland was jivin' on Sat, 1 Nov 2003 20:33:37
    >> +0100 in comp.lang.c.
    >> Re: C preprocessor conundrum's a cool scene! Dig it!
    >>
    >> >"Jim Ford" <> schrieb im Newsbeitrag
    >> >news:p...
    >> >> I have a single C file with the following code:

    >>
    >> #define f1 F1
    >> #define f2 F2
    >>
    >> >> int f2()
    >> >> {
    >> >> /* Blah-blah */
    >> >> }
    >> >>
    >> >> int f1()
    >> >> {
    >> >> /* Blah-blah */

    >>
    >> #undef f2
    >>
    >> >> f2() ;
    >> >>
    >> >> /* Reblah-blah */
    >> >> }
    >> >>
    >> >> Is it possible, by means of the C processor, to arrange things in such a
    >> >> way that, after preprocessing, the int f1() and int f2() lines will be
    >> >> replaced by int F1(), int F2(), respectively, whereas the invocation to
    >> >> f2() from f1() (F1(), after the replacement) will remain unchanged? That
    >> >> is, after preprocessing we would have
    >> >>
    >> >> int F2()
    >> >> {
    >> >> /* Preprocessed blah-blah */
    >> >> }
    >> >>
    >> >> int F1()
    >> >> {
    >> >> /* Preprocessed blah-blah */
    >> >>
    >> >> f2() ;
    >> >>
    >> >> /* Preprocessed reblah-blah */
    >> >> }
    >> >>
    >> >> All the necessary preprocessor directives would have to be in a file to
    >> >> be included at the top of this one here.

    >
    > I am afraid that your solution doesn't quite fulfil the
    >requirements of the OP. It does achieve the required result but he had
    >also said that "all the necessary preprocessor directives would have
    >to be in a file to be included at the top of this one here". Which


    Ah! I missed that bit. My appologies to Frank for, in effect,
    poopooing his post (with no offence intended); and to Jim for not
    paying more attention to his needs.

    --

    Dig the even newer still, yet more improved, sig!

    http://alphalink.com.au/~phaywood/
    "Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
    I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
    Peter Shaggy Haywood, Nov 11, 2003
    #5
    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. Jim Owen

    Clunky Cache Code Conundrum?

    Jim Owen, Jul 3, 2003, in forum: ASP .Net
    Replies:
    4
    Views:
    358
    dave wanta
    Jul 3, 2003
  2. Iain
    Replies:
    0
    Views:
    351
  3. Nobody
    Replies:
    6
    Views:
    459
    Hemal Pandya
    Jul 14, 2005
  4. VisionSet

    RMI conundrum

    VisionSet, Dec 23, 2005, in forum: Java
    Replies:
    2
    Views:
    367
    VisionSet
    Jan 2, 2006
  5. Cronus
    Replies:
    1
    Views:
    645
    Paul Mensonides
    Jul 15, 2004
Loading...

Share This Page