Download Csprintf User`s Manual Software Industry
Transcript
COPYRIGHT © 2009 Software Industry & General Hardware — ALL RIGHTS RESERVED
The output should be:
ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 means we have written 36 characters.
Try this in gcc and you will get a Bus error.
If you investigate this option you will find that gcc does not implement %n. However...Csprintf does!
Try this code in Java (make certain you have included com.sigh.jar in the CLASSPATH).
public void testPercentN() {
String
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String
digits
= "1234567890";
int
aValue
= 0;
Csprintf csf = new Csprintf();
Object[] values = new Object [ 3 ];
values [ 0 ] = alphabet;
values [ 1 ] = digits;
values [ 2 ] = (Object) aValue;
try {
System.out.println ( csf.doSprintf ( "%s%s is %d characters long",
values ) );
System.out.prtinln ( csf.doSprintf ( "%s%s%n is %d characters long", values ) );
} catch ( CsprintfExceptions csfE ) {
csfE.printStackTrace();
}
}
Note two things. The first is that new Object has only 3 elements. The reason is that %n is defined as
not incrementing the option count. That is, the value returned by %n apples to the next option
processed. This means that a %n%n must be followed by another %-option. The value returned is defined
in KNR as an int* so a %n must be followed by a %-option that is not a %s nor a %c (actually %c might
make sense, but only for values less than 256 so the author has simply disallowed it; use a %d if you
are desperate). Also a %n may not be followed by a %n. Nor may a %n be the final %-option in a format
string. Here are the three rules:
1.
2.
3.
4.
%n may not exist as the last %-option in a format string.
%n may not be followed by %n (with or without intervening characters).
%n may not be followed by %c nor %s (with or without intervening characters).
%n must be followed by a numeric %-options of some kind (usually a %d, %i or %u).
Percent-n is tricky. The gcc compiler does not implement it. The author found very little supporting
documentation, no examples and several reasons NOT to implement percent-n.
Still, %n is included in Csprintf.
C99
Inclusions
Csprintf follows the KNR description of printf, sprintf and avoids the C99 extensions.
Especially avoided are the manufacturer specific extensions DWORD, etc.
Re Software Version X.1.0.0
Page 7
doc re: Csprintf20090423024030PDT