|
Format string as a parameter to printf()?
Author |
Message |
Timur Ta #1 / 6
|
 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 |
|
 |
Ben Pfaf #2 / 6
|
 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 |
|
 |
Joona I Palast #3 / 6
|
 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 |
|
 |
Martin Ambuh #4 / 6
|
 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 |
|
 |
Ben Pfaf #5 / 6
|
 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 |
|
 |
Mark Gordo #6 / 6
|
 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 |
|
|
|