473,406 Members | 2,352 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

C preprocessor conundrum

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.
Nov 13 '05 #1
4 1927

"Jim Ford" <jf*****@excite.com> schrieb im Newsbeitrag
news:pa****************************@excite.com...
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
Nov 13 '05 #2
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" <jf*****@excite.com> schrieb im Newsbeitrag
news:pa****************************@excite.com. ..
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"?
Nov 13 '05 #3
sh****@australis.net.STOP.SPAM (Peter "Shaggy" Haywood) wrote in message news:<3f**************@news.australis.net>...
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" <jf*****@excite.com> schrieb im Newsbeitrag
news:pa****************************@excite.com. ..
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.

Nov 13 '05 #4
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!
sh****@australis.net.STOP.SPAM (Peter "Shaggy" Haywood) wrote in message news:<3f**************@news.australis.net>...
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" <jf*****@excite.com> schrieb im Newsbeitrag
>news:pa****************************@excite.com. ..
>> 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"?
Nov 13 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

205
by: Jeremy Siek | last post by:
CALL FOR PAPERS/PARTICIPATION C++, Boost, and the Future of C++ Libraries Workshop at OOPSLA October 24-28, 2004 Vancouver, British Columbia, Canada http://tinyurl.com/4n5pf Submissions
24
by: Nudge | last post by:
I have an array, and an unrolled loop which looks like this: do_something(A); do_something(A); .... do_something(A); I thought: why should I type so much? I should write a macro. So I was...
16
by: Trying_Harder | last post by:
Is it possible to redefine a macro with global scope after undefining it in a function? If yes, could someone explain how? /If/ my question above isn't very clear you can refer to the...
13
by: Chris Croughton | last post by:
Is the following code standard-compliant, and if so what should it do? And where in the standard defines the behaviour? #include <stdio.h> #define DEF defined XXX int main(void) { int...
9
by: Walter Roberson | last post by:
I have run into a peculiarity with SGI's C compiler (7.3.1.2m). I have been reading carefully over the ANSI X3.159-1989 specification, but I cannot seem to find a justification for the behaviour....
4
by: Jim Owen | last post by:
I am storing all my application data in the application cache. Anytime I have a method as part of an asp.net form, I need to access the objects in the cache. The only way I can think of to do this...
2
by: subnunciation | last post by:
i know, this shouldnt be a conundrum right? one just shouldnt divide by zero. but this is suddenly happening *all over* my site. after chasing the error here and there, i simplified things down to:...
32
by: spibou | last post by:
Is the output of the C preprocessor deterministic ? What I mean by that is , given 2 compilers which conform to the same standard, will their preprocessors produce identical output given as input...
31
by: Sam of California | last post by:
Is it accurate to say that "the preprocessor is just a pass in the parsing of the source file"? I responded to that comment by saying that the preprocessor is not just a pass. It processes...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.