Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: Comment(s) in format strings?

Reply
Thread Tools

Re: Comment(s) in format strings?

 
 
Barry Schwarz
Guest
Posts: n/a
 
      01-23-2004
On Thu, 22 Jan 2004 13:05:00 +0200, "nfg" <> wrote:

>Is there a way to comment out part of a format string used in i.e. printf,
>sprintf, etc? What I have in mind is a statement like printf("%s %s %s",
>string1, string2, string3), and the temporary need to eliminate one format
>specifier (%s) and its corresponding string: printf("%s %s /* %s */",
>string1, string2, /*string3*/). Now, while coomenting out string3 works
>fine, obviously the comment delimiters /* and */ around %s do not get
>accepted as such. Workarounds, anyone?
>

If the comment string is a literal as in your example, then the answer
is no because you are not allowed to modify a string literal at any
time. If you change it to
char format[] = "%s %s %s";
printf(format, string1, string2, string3);
you could then later say
memcpy(format, " ", 2);
printf(format, string1 string2);
or even
memcpy(format, "%d", 2);
printf(format, int1, string1 string2);

Let the off topic record reflect that this will go a long way to
making your code extremely difficult to understand or maintain. To
put it another way, it really sucks.


<<Remove the del for email>>
--
comp.lang.c.moderated - moderation address:
 
Reply With Quote
 
 
 
 
Jonathan Leffler
Guest
Posts: n/a
 
      01-24-2004
Barry Schwarz wrote:

> On Thu, 22 Jan 2004 13:05:00 +0200, "nfg" <> wrote:
>
>
>>Is there a way to comment out part of a format string used in i.e. printf,
>>sprintf, etc? What I have in mind is a statement like printf("%s %s %s",
>>string1, string2, string3), and the temporary need to eliminate one format
>>specifier (%s) and its corresponding string: printf("%s %s /* %s */",
>>string1, string2, /*string3*/). Now, while coomenting out string3 works
>>fine, obviously the comment delimiters /* and */ around %s do not get
>>accepted as such. Workarounds, anyone?
>>

>
> If the comment string is a literal as in your example, then the answer
> is no because you are not allowed to modify a string literal at any
> time. If you change it to
> char format[] = "%s %s %s";
> printf(format, string1, string2, string3);
> you could then later say
> memcpy(format, " ", 2);
> printf(format, string1 string2);
> or even
> memcpy(format, "%d", 2);
> printf(format, int1, string1 string2);
>
> Let the off topic record reflect that this will go a long way to
> making your code extremely difficult to understand or maintain. To
> put it another way, it really sucks.


An alternative relies on string concatenation:

printf("%s" " %s" " %s", string1, string2, string3);
printf("%s"/*" %s"*/" %s", string1,/*string2,*/string3);

I'd think I'd prefer:

printf("%s", string1);
printf(" %s", string2);
printf(" %s", string3);

You can then simply comment out the middle call.

(Oh - the original question omitted string3; mea culpa, mutatis
mutandis, or thereabouts).

--
Jonathan Leffler #include <disclaimer.h>
Email: ,
Guardian of DBD::Informix v2003.04 -- http://dbi.perl.org/
--
comp.lang.c.moderated - moderation address:
 
Reply With Quote
 
 
 
 
Jonathan Turkanis
Guest
Posts: n/a
 
      01-24-2004
"Barry Schwarz" <> wrote in message
news:clcm-20040122-...
> On Thu, 22 Jan 2004 13:05:00 +0200, "nfg" <>

wrote:
>
> >Is there a way to comment out part of a format string used in i.e.

printf,
> >sprintf, etc? What I have in mind is a statement like printf("%s %s

%s",
> >string1, string2, string3), and the temporary need to eliminate one

format
> >specifier (%s) and its corresponding string: printf("%s %s /* %s

*/",
> >string1, string2, /*string3*/). Now, while coomenting out string3

works
> >fine, obviously the comment delimiters /* and */ around %s do not

get
> >accepted as such. Workarounds, anyone?


One way to comment out parts of string literals is to rely on implicit
string literal concatenation:

const char* s = "In the country of Westphalia, "
"in the castle of the most noble "
"Baron of Thunder-ten-tronckh, " // Who's he?
// "lived a youth whom Nature had "
"endowed with a most sweet "
"disposition.";

If you arrange your string literal appropriately, you can conviently
comment parts out, and add comments to parts if they require
explanation. I'd only recommend this in rare cases, however, such as
with very complex regular expressions.

Jonathan
--
comp.lang.c.moderated - moderation address:
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      01-24-2004
"nfg" <> wrote:

> Is there a way to comment out part of a format string used in i.e.
> printf, sprintf, etc? What I have in mind is a statement like
> printf("%s %s %s", string1, string2, string3), and the temporary
> need to eliminate one format specifier (%s) and its corresponding
> string: printf("%s %s /* %s */", string1, string2, /*string3*/).
> Now, while coomenting out string3 works fine, obviously the
> comment delimiters /* and */ around %s do not get accepted as
> such. Workarounds, anyone?


Maybe you could write:

printf("%s" " %s" "%s", s1, s2, s3);

and remove s2 by:

printf("%s" /*" %s"*/ "%s", s1, /*s2,*/ s3);

taking advantage of string concatenation.

--
--
comp.lang.c.moderated - moderation address:
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      01-24-2004
In <clcm-20040122-> Barry Schwarz <> writes:

>On Thu, 22 Jan 2004 13:05:00 +0200, "nfg" <> wrote:
>
>>Is there a way to comment out part of a format string used in i.e. printf,
>>sprintf, etc? What I have in mind is a statement like printf("%s %s %s",
>>string1, string2, string3), and the temporary need to eliminate one format
>>specifier (%s) and its corresponding string: printf("%s %s /* %s */",
>>string1, string2, /*string3*/). Now, while coomenting out string3 works
>>fine, obviously the comment delimiters /* and */ around %s do not get
>>accepted as such. Workarounds, anyone?
>>

>If the comment string is a literal as in your example, then the answer
>is no because you are not allowed to modify a string literal at any
>time.


Note that he doesn't want to do it at run time and there is nothing wrong
in modifying a string literal with a text editor

The solution is ugly, but it is possible:

printf("%s %s " /* "%s" */, string1, string2 /*, string3*/);

It works because comments get replaced by white space in TP3, while
adjacent string literal splicing occurs in TP6. To uncomment it, it is
enough to remove the /* and */.

I would comment out the whole printf call, though:

/* printf("%s %s %s", string1, string2, string3); */
printf("%s %s ", string1, string2);

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email:
--
comp.lang.c.moderated - moderation address:
 
Reply With Quote
 
Dave Hansen
Guest
Posts: n/a
 
      01-24-2004
On 23 Jan 2004 05:19:15 GMT, Barry Schwarz <> wrote:

>On Thu, 22 Jan 2004 13:05:00 +0200, "nfg" <> wrote:
>
>>Is there a way to comment out part of a format string used in i.e. printf,
>>sprintf, etc? What I have in mind is a statement like printf("%s %s %s",
>>string1, string2, string3), and the temporary need to eliminate one format
>>specifier (%s) and its corresponding string: printf("%s %s /* %s */",
>>string1, string2, /*string3*/). Now, while coomenting out string3 works
>>fine, obviously the comment delimiters /* and */ around %s do not get
>>accepted as such. Workarounds, anyone?
>>

>If the comment string is a literal as in your example, then the answer
>is no because you are not allowed to modify a string literal at any
>time. If you change it to


You could do something like this:

printf("%s " "%s " "%s", string1, string2, string3);
printf("%s " "%s " /*"%s"*/, string1, string2 /*, string3*/);

If you want to avoid getting lost in the quotes, you might try
something like

#define STR "%s "

printf(STR STR STR, string1, string2, string3);
printf(STR STR /*STR*/, string1, string2 /*, string3*/);

though that may be overkill. Regards,

-=Dave
--
Change is inevitable, progress is not.
--
comp.lang.c.moderated - moderation address:
 
Reply With Quote
 
Artie Gold
Guest
Posts: n/a
 
      01-24-2004
Barry Schwarz wrote:
> On Thu, 22 Jan 2004 13:05:00 +0200, "nfg" <> wrote:
>
>
>>Is there a way to comment out part of a format string used in i.e. printf,
>>sprintf, etc? What I have in mind is a statement like printf("%s %s %s",
>>string1, string2, string3), and the temporary need to eliminate one format
>>specifier (%s) and its corresponding string: printf("%s %s /* %s */",
>>string1, string2, /*string3*/). Now, while coomenting out string3 works
>>fine, obviously the comment delimiters /* and */ around %s do not get
>>accepted as such. Workarounds, anyone?
>>

>
> If the comment string is a literal as in your example, then the answer
> is no because you are not allowed to modify a string literal at any
> time. If you change it to
> char format[] = "%s %s %s";
> printf(format, string1, string2, string3);
> you could then later say
> memcpy(format, " ", 2);
> printf(format, string1 string2);
> or even
> memcpy(format, "%d", 2);
> printf(format, int1, string1 string2);
>
> Let the off topic record reflect that this will go a long way to
> making your code extremely difficult to understand or maintain. To
> put it another way, it really sucks.
>
>
> <<Remove the del for email>>

The other approach is to break up the format string (using implicit
cnocatenation). For example:

printf("%s "
"%s "
"%s "
,string1, string2 ,string3);

This way you can comment out whichever piece you want.

HTH,
--ag

--
Artie Gold -- Austin, Texas
--
comp.lang.c.moderated - moderation address:
 
Reply With Quote
 
Robert Wessel
Guest
Posts: n/a
 
      01-24-2004
Barry Schwarz <> wrote in message news:<clcm-20040122->...
> On Thu, 22 Jan 2004 13:05:00 +0200, "nfg" <> wrote:
>
> >Is there a way to comment out part of a format string used in i.e. printf,
> >sprintf, etc? What I have in mind is a statement like printf("%s %s %s",
> >string1, string2, string3), and the temporary need to eliminate one format
> >specifier (%s) and its corresponding string: printf("%s %s /* %s */",
> >string1, string2, /*string3*/). Now, while coomenting out string3 works
> >fine, obviously the comment delimiters /* and */ around %s do not get
> >accepted as such. Workarounds, anyone?
> >

> If the comment string is a literal as in your example, then the answer
> is no because you are not allowed to modify a string literal at any
> time. If you change it to
> char format[] = "%s %s %s";
> printf(format, string1, string2, string3);
> you could then later say
> memcpy(format, " ", 2);
> printf(format, string1 string2);
> or even
> memcpy(format, "%d", 2);
> printf(format, int1, string1 string2);
>
> Let the off topic record reflect that this will go a long way to
> making your code extremely difficult to understand or maintain. To
> put it another way, it really sucks.



It's ugly, but since C pastes together adjacent string in the source
code, this will work:

printf("%s %s" /* "%s "*/, string1, string2, /*string3*/)
--
comp.lang.c.moderated - moderation address:
 
Reply With Quote
 
Gary
Guest
Posts: n/a
 
      01-24-2004
"Jonathan Leffler" <> wrote in message
news:clcm-20040123-...
> Barry Schwarz wrote:
>
> > On Thu, 22 Jan 2004 13:05:00 +0200, "nfg" <> wrote:
> >
> >
> >>Is there a way to comment out part of a format string used in i.e.

printf,
> >>sprintf, etc? What I have in mind is a statement like printf("%s %s %s",
> >>string1, string2, string3), and the temporary need to eliminate one

format
> >>specifier (%s) and its corresponding string: printf("%s %s /* %s */",
> >>string1, string2, /*string3*/). Now, while coomenting out string3 works
> >>fine, obviously the comment delimiters /* and */ around %s do not get
> >>accepted as such. Workarounds, anyone?


I have a sorta OT question about this thread. Since I am teaching C++,
should I ever get into the use of sprintf, printf, etc. or are these
included in C++ because they occur in C? (I suspect I know the answer.)
Is this post then not OT in comp.lang.c++ and should be deleted from that
ng?
Or am I being provincial?
--
Gary


 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      01-25-2004
Barry Schwarz wrote:

> On Thu, 22 Jan 2004 13:05:00 +0200, "nfg" <> wrote:
>
>>Is there a way to comment out part of a format string used in i.e.
>>printf, sprintf, etc? What I have in mind is a statement like
>>printf("%s %s %s", string1, string2, string3), and the temporary need
>>to eliminate one format specifier (%s) and its corresponding string:
>>printf("%s %s /* %s */", string1, string2, /*string3*/). Now, while
>>coomenting out string3 works fine, obviously the comment delimiters /*
>>and */ around %s do not get accepted as such. Workarounds, anyone?
>>

> If the comment string is a literal as in your example, then the answer
> is no because you are not allowed to modify a string literal at any
> time. If you change it to
> char format[] = "%s %s %s";
> printf(format, string1, string2, string3);
> you could then later say
> memcpy(format, " ", 2);
> printf(format, string1 string2);
> or even
> memcpy(format, "%d", 2);
> printf(format, int1, string1 string2);
>
> Let the off topic record reflect that this will go a long way to
> making your code extremely difficult to understand or maintain. To
> put it another way, it really sucks.


Since this was posted to comp.lang.c++, I assume a C++ solution will
also do, so why not just use cout instead of printf?

cout << string1 << ' ' << string2 << ' ' << string3;

You can easily somment out one of the strings without the need to change
any format specifier.

[mod note: the crosspost was probably a bad idea. -mod]
--
comp.lang.c.moderated - moderation address:
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Money Format + Decimal Place Format shannon Java 1 02-02-2006 03:47 PM
Money Format + Decimal Place Format shannon Java 0 02-01-2006 10:02 PM
need code to convert float format to internal java float format which is kept in 4 bytes integer Andy Java 7 05-10-2004 09:26 PM
NTFS quick format and normal format Guan Foo Wah MCSE 2 05-08-2004 11:35 PM
Date Format - best way of converting a string into a date format Brian Candy ASP .Net 2 02-18-2004 02:13 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57