[comp.lang.c] help with a macro

joubert@afc-tci.UUCP (Joubert Berger) (05/09/91)

I want to define a macro, lets call it UNDERLINE that would take a string
as a parameter.  This macro should add some characters in front and behind
the string.  Is this possible?

I want to be able to say:

	fprintf(f, "%s\n", UNDERLINE("test"));

and it replaces the the "test" with the newly created string.

Am I asking to much of the preprocessor?

			       Joubert
-- 
Joubert Berger                   | Email:
Teldata Computer Industries, Inc.| joubert@afc-tci.uucp    
Atlanta, Georgia  (404) 256-2166 | {rutgers,ogicse,gatech}!emory!afc-tci!joubert

gwyn@smoke.brl.mil (Doug Gwyn) (05/11/91)

In article <595@afc-tci.UUCP> joubert@afc-tci.UUCP (Joubert Berger) writes:
>I want to define a macro, lets call it UNDERLINE that would take a string
>as a parameter.  This macro should add some characters in front and behind
>the string.  Is this possible?
>I want to be able to say:
>	fprintf(f, "%s\n", UNDERLINE("test"));
>and it replaces the the "test" with the newly created string.
>Am I asking to much of the preprocessor?

Since you want to do this with the preprocessor, we may assume that
you want to do this only for STRING LITERAL arguments to the UNDERLINE()
macro.  In ANSI C, it is trivial:
	#define UNDERLINE(s) "\033[4m" s "\033[0m"
for example.  In older C implementations, it is unlikely that you will
be able to perform this operation via preprocessing.

jwm712@unhd.unh.edu (Jonathan W Miner) (05/12/91)

Someone asked:
> I want a macro which adds characters before and
> after a string.

This may or may not work (depending on your compiler)
I tested it on cc (Failed) and gcc (Passed).

#define UNDERLINE(x) "_"x"_"

So: printf(UNDERLINE("hello"));

displays: _hello_

Jon.
-- 
Jonathan Miner        | I won't speak for UNH, and UNH does not 
jwm712@unhd.unh.edu   | speak for me, but they'll charge me anyway! 
(603)868-3416         | Hacking to graduate this May!
Moving soon to miner@mervax.sanders.com  (I think?!)

darcy@druid.uucp (D'Arcy J.M. Cain) (05/12/91)

In article <595@afc-tci.UUCP> joubert@afc-tci.UUCP (Joubert Berger) writes:
>I want to define a macro, lets call it UNDERLINE that would take a string
>as a parameter.  This macro should add some characters in front and behind
>the string.  Is this possible?
>	fprintf(f, "%s\n", UNDERLINE("test"));
>Am I asking to much of the preprocessor?

Not at all.  Here is the macro

#define   UNDERLINE(x)  "pre string" x "post string"

Note that this only works with string constants but you knew that.

-- 
D'Arcy J.M. Cain (darcy@druid)     |
D'Arcy Cain Consulting             |   There's no government
Toronto, Ontario, Canada           |   like no government!
+1 416 424 2871                    |