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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
<head>
<title>GNU Octave: Formatted Output</title>
<meta name="description" content="GNU Octave: Formatted Output">
<meta name="keywords" content="GNU Octave: Formatted Output">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="index.html#Top" rel="start" title="Top">
<link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="C_002dStyle-I_002fO-Functions.html#C_002dStyle-I_002fO-Functions" rel="up" title="C-Style I/O Functions">
<link href="Output-Conversion-for-Matrices.html#Output-Conversion-for-Matrices" rel="next" title="Output Conversion for Matrices">
<link href="Line_002dOriented-Input.html#Line_002dOriented-Input" rel="prev" title="Line-Oriented Input">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.indentedblock {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
div.smalllisp {margin-left: 3.2em}
kbd {font-style:oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nocodebreak {white-space:nowrap}
span.nolinebreak {white-space:nowrap}
span.roman {font-family:serif; font-weight:normal}
span.sansserif {font-family:sans-serif; font-weight:normal}
ul.no-bullet {list-style: none}
-->
</style>
</head>
<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
<a name="Formatted-Output"></a>
<div class="header">
<p>
Next: <a href="Output-Conversion-for-Matrices.html#Output-Conversion-for-Matrices" accesskey="n" rel="next">Output Conversion for Matrices</a>, Previous: <a href="Line_002dOriented-Input.html#Line_002dOriented-Input" accesskey="p" rel="prev">Line-Oriented Input</a>, Up: <a href="C_002dStyle-I_002fO-Functions.html#C_002dStyle-I_002fO-Functions" accesskey="u" rel="up">C-Style I/O Functions</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<a name="Formatted-Output-1"></a>
<h4 class="subsection">14.2.4 Formatted Output</h4>
<p>This section describes how to call <code>printf</code> and related functions.
</p>
<p>The following functions are available for formatted output. They are
modeled after the C language functions of the same name, but they
interpret the format template differently in order to improve the
performance of printing vector and matrix values.
</p>
<a name="XREFprintf"></a><dl>
<dt><a name="index-printf"></a>Built-in Function: <em></em> <strong>printf</strong> <em>(<var>template</var>, …)</em></dt>
<dd><p>Print optional arguments under the control of the template string
<var>template</var> to the stream <code>stdout</code> and return the number of
characters printed.
</p>
<p>See the Formatted Output section of the GNU Octave manual for a
complete description of the syntax of the template string.
</p>
<p><strong>See also:</strong> <a href="#XREFfprintf">fprintf</a>, <a href="#XREFsprintf">sprintf</a>, <a href="Formatted-Input.html#XREFscanf">scanf</a>.
</p></dd></dl>
<a name="XREFfprintf"></a><dl>
<dt><a name="index-fprintf"></a>Built-in Function: <em></em> <strong>fprintf</strong> <em>(<var>fid</var>, <var>template</var>, …)</em></dt>
<dd><p>This function is just like <code>printf</code>, except that the output is
written to the stream <var>fid</var> instead of <code>stdout</code>.
If <var>fid</var> is omitted, the output is written to <code>stdout</code>.
</p>
<p><strong>See also:</strong> <a href="Simple-Output.html#XREFfputs">fputs</a>, <a href="Simple-File-I_002fO.html#XREFfdisp">fdisp</a>, <a href="Binary-I_002fO.html#XREFfwrite">fwrite</a>, <a href="Formatted-Input.html#XREFfscanf">fscanf</a>, <a href="#XREFprintf">printf</a>, <a href="#XREFsprintf">sprintf</a>, <a href="Opening-and-Closing-Files.html#XREFfopen">fopen</a>.
</p></dd></dl>
<a name="XREFsprintf"></a><dl>
<dt><a name="index-sprintf"></a>Built-in Function: <em></em> <strong>sprintf</strong> <em>(<var>template</var>, …)</em></dt>
<dd><p>This is like <code>printf</code>, except that the output is returned as a
string. Unlike the C library function, which requires you to provide a
suitably sized string as an argument, Octave’s <code>sprintf</code> function
returns the string, automatically sized to hold all of the items
converted.
</p>
<p><strong>See also:</strong> <a href="#XREFprintf">printf</a>, <a href="#XREFfprintf">fprintf</a>, <a href="Formatted-Input.html#XREFsscanf">sscanf</a>.
</p></dd></dl>
<p>The <code>printf</code> function can be used to print any number of arguments.
The template string argument you supply in a call provides
information not only about the number of additional arguments, but also
about their types and what style should be used for printing them.
</p>
<p>Ordinary characters in the template string are simply written to the
output stream as-is, while <em>conversion specifications</em> introduced by
a ‘<samp>%</samp>’ character in the template cause subsequent arguments to be
formatted and written to the output stream. For example,
<a name="index-conversion-specifications-_0028printf_0029"></a>
</p>
<div class="example">
<pre class="example">pct = 37;
filename = "foo.txt";
printf ("Processed %d%% of '%s'.\nPlease be patient.\n",
pct, filename);
</pre></div>
<p>produces output like
</p>
<div class="example">
<pre class="example">Processed 37% of 'foo.txt'.
Please be patient.
</pre></div>
<p>This example shows the use of the ‘<samp>%d</samp>’ conversion to specify that a
scalar argument should be printed in decimal notation, the ‘<samp>%s</samp>’
conversion to specify printing of a string argument, and the ‘<samp>%%</samp>’
conversion to print a literal ‘<samp>%</samp>’ character.
</p>
<p>There are also conversions for printing an integer argument as an
unsigned value in octal, decimal, or hexadecimal radix (‘<samp>%o</samp>’,
‘<samp>%u</samp>’, or ‘<samp>%x</samp>’, respectively); or as a character value
(‘<samp>%c</samp>’).
</p>
<p>Floating-point numbers can be printed in normal, fixed-point notation
using the ‘<samp>%f</samp>’ conversion or in exponential notation using the
‘<samp>%e</samp>’ conversion. The ‘<samp>%g</samp>’ conversion uses either ‘<samp>%e</samp>’
or ‘<samp>%f</samp>’ format, depending on what is more appropriate for the
magnitude of the particular number.
</p>
<p>You can control formatting more precisely by writing <em>modifiers</em>
between the ‘<samp>%</samp>’ and the character that indicates which conversion
to apply. These slightly alter the ordinary behavior of the conversion.
For example, most conversion specifications permit you to specify a
minimum field width and a flag indicating whether you want the result
left- or right-justified within the field.
</p>
<p>The specific flags and modifiers that are permitted and their
interpretation vary depending on the particular conversion. They’re all
described in more detail in the following sections.
</p>
<hr>
<div class="header">
<p>
Next: <a href="Output-Conversion-for-Matrices.html#Output-Conversion-for-Matrices" accesskey="n" rel="next">Output Conversion for Matrices</a>, Previous: <a href="Line_002dOriented-Input.html#Line_002dOriented-Input" accesskey="p" rel="prev">Line-Oriented Input</a>, Up: <a href="C_002dStyle-I_002fO-Functions.html#C_002dStyle-I_002fO-Functions" accesskey="u" rel="up">C-Style I/O Functions</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
</body>
</html>
|