<html lang="en"> <head> <title>Formatted Output - Untitled</title> <meta http-equiv="Content-Type" content="text/html"> <meta name="description" content="Untitled"> <meta name="generator" content="makeinfo 4.13"> <link title="Top" rel="start" href="index.html#Top"> <link rel="up" href="C_002dStyle-I_002fO-Functions.html#C_002dStyle-I_002fO-Functions" title="C-Style I/O Functions"> <link rel="prev" href="Line_002dOriented-Input.html#Line_002dOriented-Input" title="Line-Oriented Input"> <link rel="next" href="Output-Conversion-for-Matrices.html#Output-Conversion-for-Matrices" title="Output Conversion for Matrices"> <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> <meta http-equiv="Content-Style-Type" content="text/css"> <style type="text/css"><!-- pre.display { font-family:inherit } pre.format { font-family:inherit } pre.smalldisplay { font-family:inherit; font-size:smaller } pre.smallformat { font-family:inherit; font-size:smaller } pre.smallexample { font-size:smaller } pre.smalllisp { font-size:smaller } span.sc { font-variant:small-caps } span.roman { font-family:serif; font-weight:normal; } span.sansserif { font-family:sans-serif; font-weight:normal; } --></style> </head> <body> <div class="node"> <a name="Formatted-Output"></a> <p> Next: <a rel="next" accesskey="n" href="Output-Conversion-for-Matrices.html#Output-Conversion-for-Matrices">Output Conversion for Matrices</a>, Previous: <a rel="previous" accesskey="p" href="Line_002dOriented-Input.html#Line_002dOriented-Input">Line-Oriented Input</a>, Up: <a rel="up" accesskey="u" href="C_002dStyle-I_002fO-Functions.html#C_002dStyle-I_002fO-Functions">C-Style I/O Functions</a> <hr> </div> <h4 class="subsection">14.2.4 Formatted Output</h4> <p>This section describes how to call <code>printf</code> and related functions. <p>The following functions are available for formatted output. They are modelled 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. <!-- file-io.cc --> <p><a name="doc_002dprintf"></a> <div class="defun"> — Built-in Function: <b>printf</b> (<var>template, <small class="dots">...</small></var>)<var><a name="index-printf-781"></a></var><br> <blockquote><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. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dfprintf.html#doc_002dfprintf">fprintf</a>, <a href="doc_002dsprintf.html#doc_002dsprintf">sprintf</a>, <a href="doc_002dscanf.html#doc_002dscanf">scanf</a>. </p></blockquote></div> <!-- file-io.cc --> <p><a name="doc_002dfprintf"></a> <div class="defun"> — Built-in Function: <b>fprintf</b> (<var>fid, template, <small class="dots">...</small></var>)<var><a name="index-fprintf-782"></a></var><br> <blockquote><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>. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dprintf.html#doc_002dprintf">printf</a>, <a href="doc_002dsprintf.html#doc_002dsprintf">sprintf</a>, <a href="doc_002dfread.html#doc_002dfread">fread</a>, <a href="doc_002dfscanf.html#doc_002dfscanf">fscanf</a>, <a href="doc_002dfopen.html#doc_002dfopen">fopen</a>, <a href="doc_002dfclose.html#doc_002dfclose">fclose</a>. </p></blockquote></div> <!-- file-io.cc --> <p><a name="doc_002dsprintf"></a> <div class="defun"> — Built-in Function: <b>sprintf</b> (<var>template, <small class="dots">...</small></var>)<var><a name="index-sprintf-783"></a></var><br> <blockquote><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. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dprintf.html#doc_002dprintf">printf</a>, <a href="doc_002dfprintf.html#doc_002dfprintf">fprintf</a>, <a href="doc_002dsscanf.html#doc_002dsscanf">sscanf</a>. </p></blockquote></div> <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>Ordinary characters in the template string are simply written to the output stream as-is, while <dfn>conversion specifications</dfn> introduced by a ‘<samp><span class="samp">%</span></samp>’ character in the template cause subsequent arguments to be formatted and written to the output stream. For example, <a name="index-conversion-specifications-_0028_0040code_007bprintf_007d_0029-784"></a> <pre class="example"> pct = 37; filename = "foo.txt"; printf ("Processed %d%% of `%s'.\nPlease be patient.\n", pct, filename); </pre> <p class="noindent">produces output like <pre class="example"> Processed 37% of `foo.txt'. Please be patient. </pre> <p>This example shows the use of the ‘<samp><span class="samp">%d</span></samp>’ conversion to specify that a scalar argument should be printed in decimal notation, the ‘<samp><span class="samp">%s</span></samp>’ conversion to specify printing of a string argument, and the ‘<samp><span class="samp">%%</span></samp>’ conversion to print a literal ‘<samp><span class="samp">%</span></samp>’ character. <p>There are also conversions for printing an integer argument as an unsigned value in octal, decimal, or hexadecimal radix (‘<samp><span class="samp">%o</span></samp>’, ‘<samp><span class="samp">%u</span></samp>’, or ‘<samp><span class="samp">%x</span></samp>’, respectively); or as a character value (‘<samp><span class="samp">%c</span></samp>’). <p>Floating-point numbers can be printed in normal, fixed-point notation using the ‘<samp><span class="samp">%f</span></samp>’ conversion or in exponential notation using the ‘<samp><span class="samp">%e</span></samp>’ conversion. The ‘<samp><span class="samp">%g</span></samp>’ conversion uses either ‘<samp><span class="samp">%e</span></samp>’ or ‘<samp><span class="samp">%f</span></samp>’ format, depending on what is more appropriate for the magnitude of the particular number. <p>You can control formatting more precisely by writing <dfn>modifiers</dfn> between the ‘<samp><span class="samp">%</span></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>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. </body></html>