File: io_printf.html

package info (click to toggle)
freemat 4.2%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 141,800 kB
  • ctags: 14,082
  • sloc: ansic: 126,788; cpp: 62,046; python: 2,080; perl: 1,255; sh: 1,146; yacc: 1,019; lex: 239; makefile: 100
file content (140 lines) | stat: -rw-r--r-- 12,387 bytes parent folder | download | duplicates (2)
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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<title>FreeMat: PRINTF Formated Output Function (C-Style)</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td style="padding-left: 0.5em;">
   <div id="projectname">FreeMat
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.1.1 -->
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li class="current"><a href="pages.html"><span>Related&#160;Pages</span></a></li>
    </ul>
  </div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('io_printf.html','');});
</script>
<div id="doc-content">
<div class="header">
  <div class="headertitle">
<div class="title">PRINTF Formated Output Function (C-Style) </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p>Section: <a class="el" href="sec_io.html">Input/Ouput Functions</a> </p>
<h1><a class="anchor" id="Usage"></a>
Usage</h1>
<p>Prints values to the output. The general syntax for its use is </p>
<pre class="fragment">  printf(format,a1,a2,...)
</pre><p> Here <code>format</code> is the format string, which is a string that controls the format of the output. The values of the variables <code>a_i</code> are substituted into the output as required. It is an error if there are not enough variables to satisfy the format string. Note that this <code>printf</code> command is not vectorized! Each variable must be a scalar.</p>
<p>It is important to point out that the <code>printf</code> function does not add a newline (or carriage return) to the output by default. That can lead to some confusing behavior if you do not know what to expect. For example, the command <code>printf('Hello')</code> does not appear to produce any output. In fact, it does produce the text, but it then gets overwritten by the prompt. To see the text, you need <code>printf('Hello<br/>
')</code>. This seems odd, but allows you to assemble a line using multiple <code>printf</code> commands, including the <code>'<br/>
'</code> when you are done with the line. You can also use the <code>'\r'</code> character as an explicit carriage return (with no line feed). This allows you to write to the same line many times (to show a progress string, for example).</p>
<h1><a class="anchor" id="Format"></a>
of the format string</h1>
<p>The format string is a character string, beginning and ending in its initial shift state, if any. The format string is composed of zero or more directives: ordinary characters (not %), which are copied unchanged to the output stream; and conversion specifications, each of which results in fetching zero or more subsequent arguments. Each conversion specification is introduced by the character %, and ends with a conversion specifier. In between there may be (in this order) zero or more flags, an optional minimum field width, and an optional precision.</p>
<p>The arguments must correspond properly (after type promotion) with the conversion specifier, and are used in the order given.</p>
<h1><a class="anchor" id="The"></a>
conversion specifier</h1>
<p>The character <code>%</code> is followed by zero or more of the following flags: </p>
<ul>
<li>
<code>#</code> The value should be converted to an ``alternate form''. For <code>o</code> conversions, the first character of the output string is made zero (by prefixing a <code>0</code> if it was not zero already). For <code>x</code> and <code>X</code> conversions, a nonzero result has the string <code>'0x'</code> (or <code>'0X'</code> for <code>X</code> conversions) prepended to it. For <code>a, A, e, E, f, F, g,</code> and <code>G</code> conversions, the result will always contain a decimal point, even if no digits follow it (normally, a decimal point appears in the results of those conversions only if a digit follows). For <code>g</code> and <code>G</code> conversions, trailing zeros are not removed from the result as they would otherwise be. For other conversions, the result is undefined.  </li>
<li>
<code>0</code> The value should be zero padded. For <code>d, i, o, u, x, X, a, A, e, E, f, F, g,</code> and <code>G</code> conversions, the converted value is padded on the left with zeros rather than blanks. If the <code>0</code> and <code>-</code> flags both appear, the <code>0</code> flag is ignored. If a precision is given with a numeric conversion <code>(d, i, o, u, x, and X)</code>, the <code>0</code> flag is ignored. For other conversions, the behavior is undefined.  </li>
<li>
<code>-</code> The converted value is to be left adjusted on the field boundary. (The default is right justification.) Except for <code>n</code> conversions, the converted value is padded on the right with blanks, rather than on the left with blanks or zeros. A <code>-</code> overrides a <code>0</code> if both are given.  </li>
<li>
<code>' '</code> (a space) A blank should be left before a positive number (or empty string) produced by a signed conversion.  </li>
<li>
<code>+</code> A sign (<code>+</code> or <code>-</code>) always be placed before a number produced by a signed conversion. By default a sign is used only for negative numbers. A <code>+</code> overrides a space if both are used.  </li>
</ul>
<h1><a class="anchor" id="The"></a>
conversion specifier</h1>
<p>An optional decimal digit string (with nonzero first digit) specifying a minimum field width. If the converted value has fewer characters than the field width, it will be padded with spaces on the left (or right, if the left-adjustment flag has been given). A negative field width is taken as a <code>'-'</code> flag followed by a positive field width. In no case does a non-existent or small field width cause truncation of a field; if the result of a conversion is wider than the field width, the field is expanded to contain the conversion result.</p>
<h1><a class="anchor" id="The"></a>
conversion specifier</h1>
<p>An optional precision, in the form of a period (<code>'.'</code>) followed by an optional decimal digit string. If the precision is given as just <code>'.'</code>, or the precision is negative, the precision is taken to be zero. This gives the minimum number of digits to appear for <code>d, i, o, u, x</code>, and <code>X</code> conversions, the number of digits to appear after the radix character for <code>a, A, e, E, f</code>, and <code>F</code> conversions, the maximum number of significant digits for <code>g</code> and <code>G</code> conversions, or the maximum number of characters to be printed from a string for s conversions.</p>
<h1><a class="anchor" id="The"></a>
conversion specifier</h1>
<p>A character that specifies the type of conversion to be applied. The conversion specifiers and their meanings are: </p>
<ul>
<li>
<code>d,i</code> The int argument is converted to signed decimal notation. The precision, if any, gives the minimum number of digits that must appear; if the converted value requires fewer digits, it is padded on the left with zeros. The default precision is <code>1</code>. When <code>0</code> is printed with an explicit precision <code>0</code>, the output is empty.  </li>
<li>
<code>o,u,x,X</code> The unsigned int argument is converted to unsigned octal (<code>o</code>), unsigned decimal (<code>u</code>), or unsigned hexadecimal (<code>x</code> and <code>X</code>) notation. The letters <code>abcdef</code> are used for <code>x</code> conversions; the letters <code>ABCDEF</code> are used for <code>X</code> conversions. The precision, if any, gives the minimum number of digits that must appear; if the converted value requires fewer digits, it is padded on the left with zeros. The default precision is <code>1</code>. When <code>0</code> is printed with an explicit precision <code>0</code>, the output is empty.  </li>
<li>
<code>e,E</code> The double argument is rounded and converted in the style <code>[-]d.ddde dd</code> where there is one digit before the decimal-point character and the number of digits after it is equal to the precision; if the precision is missing, it is taken as <code>6</code>; if the precision is zero, no decimal-point character appears. An <code>E</code> conversion uses the letter <code>E</code> (rather than <code>e</code>) to introduce the exponent. The exponent always contains at least two digits; if the value is zero, the exponent is <code>00</code>.  </li>
<li>
<code>f,F</code> The double argument is rounded and converted to decimal notation in the style <code>[-]ddd.ddd</code>, where the number of digits after the decimal-point character is equal to the precision specification. If the precision is missing, it is taken as <code>6</code>; if the precision is explicitly zero, no decimal-point character appears. If a decimal point appears, at least one digit appears before it.  </li>
<li>
<code>g,G</code> The double argument is converted in style <code>f</code> or <code>e</code> (or <code>F</code> or <code>E</code> for <code>G</code> conversions). The precision specifies the number of significant digits. If the precision is missing, <code>6</code> digits are given; if the precision is zero, it is treated as <code>1</code>. Style e is used if the exponent from its conversion is less than <code>-4</code> or greater than or equal to the precision. Trailing zeros are removed from the fractional part of the result; a decimal point appears only if it is followed by at least one digit.  </li>
<li>
<code>c</code> The int argument is converted to an unsigned char, and the resulting character is written.  </li>
<li>
<code>s</code> The string argument is printed.  </li>
<li>
<code>%</code> A <code>''</code> is written. No argument is converted. The complete conversion specification is <code>'%'</code>.  </li>
</ul>
<h1><a class="anchor" id="Example"></a>
Example</h1>
<p>Here are some examples of the use of <code>printf</code> with various arguments. First we print out an integer and double value.</p>
<pre class="fragment">--&gt; printf('intvalue is %d, floatvalue is %f\n',3,1.53);
intvalue is 3, floatvalue is 1.530000
</pre><p>Next, we print out a string value.</p>
<pre class="fragment">--&gt; printf('string value is %s\n','hello');
string value is hello
</pre><p>Now, we print out an integer using 12 digits, zeros up front.</p>
<pre class="fragment">--&gt; printf('integer padded is %012d\n',32);
integer padded is 000000000032
</pre><p>Print out a double precision value with a sign, a total of 18 characters (zero prepended if necessary), a decimal point, and 12 digit precision.</p>
<pre class="fragment">--&gt; printf('float value is %+018.12f\n',pi);
float value is +0003.141592653590
</pre> </div></div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="navelem"><a class="el" href="index.html">FreeMat Documentation</a></li><li class="navelem"><a class="el" href="sec_io.html">Input/Ouput Functions</a></li>
    <li class="footer">Generated on Thu Jul 25 2013 17:17:39 for FreeMat by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.1.1 </li>
  </ul>
</div>
</body>
</html>