Format string as a parameter to printf()? 
Author Message
 Format string as a parameter to printf()?

I have a string constant like this:

static char SZ_CUR_BOOT[] = "The Firmware is currently booting from
the %s side.";

Normally, if I wanted to output this string as HTML, I'd do something
like this:

printf("<p>");
printf(SZ_CUR_BOOT, "left");
printf("</p>");

Which would output this text:

"<p>The Firmware is currently booting from the left side.</p>"

Is there anyway to combined the above three printf() calls into a
SINGLE printf() call, without modifying the contents of SZ_CUR_BOOT?
Something like this:



interpret the next parameter as a format string.



Sat, 13 Aug 2005 06:26:01 GMT  
 Format string as a parameter to printf()?

Quote:

> I have a string constant like this:

> static char SZ_CUR_BOOT[] = "The Firmware is currently booting from
> the %s side.";

> Normally, if I wanted to output this string as HTML, I'd do something
> like this:

> printf("<p>");
> printf(SZ_CUR_BOOT, "left");
> printf("</p>");

> Which would output this text:

> "<p>The Firmware is currently booting from the left side.</p>"

> Is there anyway to combined the above three printf() calls into a
> SINGLE printf() call, without modifying the contents of SZ_CUR_BOOT?

No.  However, if you change the string constant to a macro, like
this:
        #define SZ_CUR_BOOT "The Firmware is currently booting from the %s side."
then you can use the string concatenation feature of C, like so:
        printf("<p>" SZ_CUR_BOOT "</p>", "left");
--
"It wouldn't be a new C standard if it didn't give a
 new meaning to the word `static'."
--Peter Seebach on C99


Sat, 13 Aug 2005 06:34:31 GMT  
 Format string as a parameter to printf()?

Quote:
> I have a string constant like this:
> static char SZ_CUR_BOOT[] = "The Firmware is currently booting from
> the %s side.";
> Normally, if I wanted to output this string as HTML, I'd do something
> like this:
> printf("<p>");
> printf(SZ_CUR_BOOT, "left");
> printf("</p>");
> Which would output this text:
> "<p>The Firmware is currently booting from the left side.</p>"
> Is there anyway to combined the above three printf() calls into a
> SINGLE printf() call, without modifying the contents of SZ_CUR_BOOT?
> Something like this:


> interpret the next parameter as a format string.

That is not possible. In fact it's not possible at all with a single
printf call. What you need is sprintf.
char buffer[BIG_ENOUGH];
sprintf(buffer, "<p>%s</p>", SZ_CUR_BOOT);
printf(buffer, "left");

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/
"Ice cream sales somehow cause drownings: both happen in summer."
   - Antti Voipio & Arto Wikla



Sat, 13 Aug 2005 06:40:32 GMT  
 Format string as a parameter to printf()?

Quote:

> I have a string constant like this:

> static char SZ_CUR_BOOT[] = "The Firmware is currently booting from
> the %s side.";

> Normally, if I wanted to output this string as HTML, I'd do something
> like this:

> printf("<p>");
> printf(SZ_CUR_BOOT, "left");
> printf("</p>");

> Which would output this text:

> "<p>The Firmware is currently booting from the left side.</p>"

> Is there anyway to combined the above three printf() calls into a
> SINGLE printf() call, without modifying the contents of SZ_CUR_BOOT?

printf("<p>" SZ_CUR_BOOT "</p>", "left");


Sat, 13 Aug 2005 14:23:45 GMT  
 Format string as a parameter to printf()?

Quote:


> > I have a string constant like this:
> > static char SZ_CUR_BOOT[] = "The Firmware is currently booting from
> > the %s side.";

> printf("<p>" SZ_CUR_BOOT "</p>", "left");

No, that syntax only works with string literals.
--
"IMO, Perl is an excellent language to break your teeth on"
--Micah Cowan


Sat, 13 Aug 2005 17:08:10 GMT  
 Format string as a parameter to printf()?
On Tue, 25 Feb 2003 06:23:45 GMT

Quote:


> > I have a string constant like this:

> > static char SZ_CUR_BOOT[] = "The Firmware is currently booting from
> > the %s side.";

> > Normally, if I wanted to output this string as HTML, I'd do
> > something like this:

> > printf("<p>");
> > printf(SZ_CUR_BOOT, "left");
> > printf("</p>");

> > Which would output this text:

> > "<p>The Firmware is currently booting from the left side.</p>"

> > Is there anyway to combined the above three printf() calls into a
> > SINGLE printf() call, without modifying the contents of SZ_CUR_BOOT?

> printf("<p>" SZ_CUR_BOOT "</p>", "left");

That won't work. SZ_CUR_BOOT is an array of char, not a macro that
expands to a literal string.
--
Mark Gordon
Paid to be a Geek & a Senior Software Developer
Currently looking for a new job commutable from Slough, Berks, U.K.
Although my email address says spamtrap, it is real and I read it.


Sat, 13 Aug 2005 17:53:54 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. printf format string: how bad style is this?

2. printf -*s format string

3. Examples of format strings for scanf/printf ?

4. printf and formatting numeric strings

5. placing output of printf in memory: formatting strings

6. printf and variable length string format (asterisk)

7. Printf format strings

8. printf() format-string parser wanted!

9. printf Format string

10. Source to format strings like printf()

11. STL strings & printf-type formatting

12. System::String::Format and Variable parameters

 

 
Powered by phpBB® Forum Software