|
Contents | Previous | Next | Subchapters |
| Syntax |
sprintf(format string, A, B, ...)
|
| See Also | fprintf , ntoa , format , print , write |
sprintf function
in the C compute language
(see page 154 of The C programming Language by B.W. Kernighan
and D.M. Ritchie).
It allows for multiple format specifications to be used
when creating a single ASCII string.
(The ntoa
function is simpler but it only uses one format
specification to create a character matrix.)
The argument format string specifies the manner in which
the values in the list
A, B, ...
are converted to
a sequence of ascii
characters.
Characters that are not part of a conversion specification
are copied directly to the output string with the following C language
style escaping of special characters:
| Escape Sequence | Resulting Output character |
%% | single percent sign |
\\ | single backslash |
\n | new line |
\ooo | octal ascii code is ooo |
\xhh | hexadecimal ascii code is hh |
%% appears in format string
they are copied to the output string as a single %
(see %% escape sequence above).
Otherwise the character % denotes the beginning of a format
conversion specification.
A format conversion specification has the following form:
percent sign width dot precision type
The following table describes each of the components in the form above:
| Symbol | Characters | Optional |
precent |
% | no |
sign |
- | yes |
width |
* or a number | yes |
dot |
. | yes |
precision |
* or a number | yes |
type |
dioxXucsfeEgG | no |
dot and precision field:
| Type | Precision | Description |
d | no | integer to decimal |
i | no | integer to decimal |
o | no | positive integer to octal |
u | no | positive integer to decimal |
x | no | integer to lower case hex |
X | no | integer to upper case hex |
c | no | from and to ASCII character |
s | yes | from and to vector of ASCII characters |
f | yes | floating point to fixed point |
e | yes |
floating point to e scientific notation |
E | yes |
floating point to E scientific notation |
g | yes |
combination of f and e formats |
G | yes |
combination of f and E formats |
* characters in it,
it corresponds to one element.
If it has one * character, the specification corresponds to two
elements with the first corresponding to the *.
If it has two * characters, the specification corresponds to three
elements with the first two corresponding to the * characters.
s = sprintf("%d", -10)
print type(s), s
O-Matrix will respond
char -10
If you enter
sprintf(":%5i:", -10)
O-Matrix will respond
: -10:
If you enter
sprintf(":%-5o:%-5u:", 10, 10)
O-Matrix will respond
:12 :10 :
If you enter
sprintf(":%5x:%5X:%05X:", 10, 10, 10)
O-Matrix will respond
: a: A:0000A:
If you enter
sprintf(":%-5o:", 10)
O-Matrix will respond
:12 :
sprintf(":%f:%.6f:", 1.0, 2.0)
O-Matrix will respond
:1.000000:2.000000:
If you enter
sprintf(":%10f:%-10.6f:", 1.0, 2.0)
O-Matrix will respond
: 1.000000:2.000000 :
If you enter
sprintf(":%e:%.6E:", 1.0, 2.0)
O-Matrix will respond
:1.000000e+000:2.000000E+000:
sprintf(":%15e:%-15.6E:", 1.0, 2.0)
O-Matrix will respond
: 1.000000e+000:2.000000E+000 :
The g and G formats only display the number of
digits necessary to get the requested precision.
If you enter
sprintf(":%g:%.6G:", pi, 2.0)
O-Matrix will respond
:3.14159:2:
If you enter
sprintf(":%g:%.6G:", 1e+10, 2e+20)
O-Matrix will respond
:1e+010:2e+020:
c format type can be used to convert a single value
using its corresponding ASCII code.
If you enter
sprintf("%c, %d, %e", "ABC")
O-Matrix will respond
A, 66, 6.700000e+001
The s format can be used to convert an entire matrix
using the corresponding ascii
codes.
If you enter
sprintf(":%s:%s:", "ABC", [65, 66, 67])
O-Matrix will respond
:ABC:ABC:
If you enter
sprintf(":%10c:%10s:", "A", "ABC")
O-Matrix will respond
: A: ABC:
If you enter
sprintf(":%-10c:%-10s:", "A", "ABC")
O-Matrix will respond
:A :ABC :
If you enter
sprintf(":%10.2s:%-10.2s:", "ABC", "ABC")
O-Matrix will respond
: AB:AB :
* character is used to specify
the value for width or precision when they depends
on some other value.
If you enter
sprintf(":%*d:", 5, 1)
O-Matrix will respond
: 1:
If you enter
sprintf(":%*.*f:", [10, 2, pi])
O-Matrix will respond
: 3.14:
fprintf acts the same as sprintf
with the exception that the result is printed to the command
window instead of returned as the function value.
If in Mlmode you enter
x = 1;
y = 2;
fprintf('one = %g\ntwo = %g\n', x, y);
O-Matrix will respond
one = 1
two = 2