Mombu the Programming Forum sponsored links

Go Back   Mombu the Programming Forum > Programming > printf() with trailing % in format string
User Name
Password
REGISTER NOW! Mark Forums Read

sponsored links


Reply
 
1 21st May 05:09
stefan kuesters
External User
 
Posts: 1
Default printf() with trailing % in format string



Hello all,
when outputting text with printf or a similar function, is there any
specification
how a trailing percent sign in the format string will be handled?

printf("An integer: %d and junk: %", 42);

My implementation (MS VC 6.0) silently swallows the trailing % sign (and
this
is what I need in this very special cir***stances), but the question remains
whether this is standard and/or if there is a standardized behaviour anyway.

Thanks for any answer!

Regards
Stefan

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
  Reply With Quote


  sponsored links


2 31st May 23:58
ron natalie
External User
 
Posts: 1
Default printf() with trailing % in format string



It's undefined behavior. The C standard says that the % must be
followed
by certain characters (of which the null terminator is not allowed).
It then
states that the invalid format is undefined behavior. 7.19.6 of C99 or
7.9.6.2
of C90.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
  Reply With Quote
3 31st May 23:59
ron natalie
External User
 
Posts: 1
Default printf() with trailing % in format string


It's undefined behavior. The C standard says that the % must be
followed
by certain characters (of which the null terminator is not allowed).
It then
states that the invalid format is undefined behavior. 7.19.6 of C99 or
7.9.6.2
of C90.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
  Reply With Quote
4 31st May 23:59
vinay doma
External User
 
Posts: 1
Default printf() with trailing % in format string


You can use %% to print a '%' character.
For example,
printf("An integer: %d and junk: %%", 42);

prints "An integer: 42 and junk: %".

Vinay

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
  Reply With Quote
5 1st June 00:00
vinay doma
External User
 
Posts: 1
Default printf() with trailing % in format string


You can use %% to print a '%' character.
For example,
printf("An integer: %d and junk: %%", 42);

prints "An integer: 42 and junk: %".

Vinay

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
  Reply With Quote
6 10th June 09:16
stefan kuesters
External User
 
Posts: 1
Default printf() with trailing % in format string


Thank you for all you answers. In fact, it was not my intention to
print a % sign (I am quite firm with C/C++ :-) but I had to figure
out a workaround to allow a specific behaviour in an existing
program (a product which can not easiliy changed):

A control character sequence has to been sent to a device. A 'G'
must be the last character of the sequence, but it *must not* be
seen by the function that does the first processing of the sequence.
A second function uses the control character sequence as input
format for printf, so 'S1m44k1G%' transmutes to 'S1m44k1G'
which is what the device needs. I hope I could express what I
mean........

Once again, thank you all.

Stefan

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
  Reply With Quote
Reply


Thread Tools
Display Modes




Copyright © 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666