moonpool/dev/ocaml/Stdlib/Printf/index.html
2023-08-29 18:39:53 +00:00

14 lines
No EOL
18 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Printf (ocaml.Stdlib.Printf)</title><link rel="stylesheet" href="../../../_odoc-theme/odoc.css"/><meta charset="utf-8"/><meta name="generator" content="odoc 2.2.1"/><meta name="viewport" content="width=device-width,initial-scale=1.0"/><script src="../../../highlight.pack.js"></script><script>hljs.initHighlightingOnLoad();</script></head><body class="odoc"><nav class="odoc-nav"><a href="../index.html">Up</a> <a href="../../index.html">ocaml</a> &#x00BB; <a href="../index.html">Stdlib</a> &#x00BB; Printf</nav><header class="odoc-preamble"><h1>Module <code><span>Stdlib.Printf</span></code></h1><p>Formatted output functions.</p></header><div class="odoc-content"><div class="odoc-spec"><div class="spec value anchored" id="val-fprintf"><a href="#val-fprintf" class="anchor"></a><code><span><span class="keyword">val</span> fprintf : <span><a href="../index.html#type-out_channel">out_channel</a> <span class="arrow">&#45;&gt;</span></span> <span><span><span>(<span class="type-var">'a</span>, <a href="../index.html#type-out_channel">out_channel</a>, unit)</span> <a href="../index.html#type-format">format</a></span> <span class="arrow">&#45;&gt;</span></span> <span class="type-var">'a</span></span></code></div><div class="spec-doc"><p><code>fprintf outchan format arg1 ... argN</code> formats the arguments <code>arg1</code> to <code>argN</code> according to the format string <code>format</code>, and outputs the resulting string on the channel <code>outchan</code>.</p><p>The format string is a character string which contains two types of objects: plain characters, which are simply copied to the output channel, and conversion specifications, each of which causes conversion and printing of arguments.</p><p>Conversion specifications have the following form:</p><p><code>% [flags] [width] [.precision] type</code></p><p>In short, a conversion specification consists in the <code>%</code> character, followed by optional modifiers and a type which is made of one or two characters.</p><p>The types and their meanings are:</p><ul><li><code>d</code>, <code>i</code>: convert an integer argument to signed decimal. The flag <code>#</code> adds underscores to large values for readability.</li><li><code>u</code>, <code>n</code>, <code>l</code>, <code>L</code>, or <code>N</code>: convert an integer argument to unsigned decimal. Warning: <code>n</code>, <code>l</code>, <code>L</code>, and <code>N</code> are used for <code>scanf</code>, and should not be used for <code>printf</code>. The flag <code>#</code> adds underscores to large values for readability.</li><li><code>x</code>: convert an integer argument to unsigned hexadecimal, using lowercase letters. The flag <code>#</code> adds a <code>0x</code> prefix to non zero values.</li><li><code>X</code>: convert an integer argument to unsigned hexadecimal, using uppercase letters. The flag <code>#</code> adds a <code>0X</code> prefix to non zero values.</li><li><code>o</code>: convert an integer argument to unsigned octal. The flag <code>#</code> adds a <code>0</code> prefix to non zero values.</li><li><code>s</code>: insert a string argument.</li><li><code>S</code>: convert a string argument to OCaml syntax (double quotes, escapes).</li><li><code>c</code>: insert a character argument.</li><li><code>C</code>: convert a character argument to OCaml syntax (single quotes, escapes).</li><li><code>f</code>: convert a floating-point argument to decimal notation, in the style <code>dddd.ddd</code>.</li><li><code>F</code>: convert a floating-point argument to OCaml syntax (<code>dddd.</code> or <code>dddd.ddd</code> or <code>d.ddd e+-dd</code>). Converts to hexadecimal with the <code>#</code> flag (see <code>h</code>).</li><li><code>e</code> or <code>E</code>: convert a floating-point argument to decimal notation, in the style <code>d.ddd e+-dd</code> (mantissa and exponent).</li><li><code>g</code> or <code>G</code>: convert a floating-point argument to decimal notation, in style <code>f</code> or <code>e</code>, <code>E</code> (whichever is more compact). Moreover, any trailing zeros are removed from the fractional part of the result and the decimal-point character is removed if there is no fractional part remaining.</li><li><code>h</code> or <code>H</code>: convert a floating-point argument to hexadecimal notation, in the style <code>0xh.hhhh p+-dd</code> (hexadecimal mantissa, exponent in decimal and denotes a power of 2).</li><li><code>B</code>: convert a boolean argument to the string <code>true</code> or <code>false</code></li><li><code>b</code>: convert a boolean argument (deprecated; do not use in new programs).</li><li><code>ld</code>, <code>li</code>, <code>lu</code>, <code>lx</code>, <code>lX</code>, <code>lo</code>: convert an <code>int32</code> argument to the format specified by the second letter (decimal, hexadecimal, etc).</li><li><code>nd</code>, <code>ni</code>, <code>nu</code>, <code>nx</code>, <code>nX</code>, <code>no</code>: convert a <code>nativeint</code> argument to the format specified by the second letter.</li><li><code>Ld</code>, <code>Li</code>, <code>Lu</code>, <code>Lx</code>, <code>LX</code>, <code>Lo</code>: convert an <code>int64</code> argument to the format specified by the second letter.</li><li><code>a</code>: user-defined printer. Take two arguments and apply the first one to <code>outchan</code> (the current output channel) and to the second argument. The first argument must therefore have type <code>out_channel -&gt; 'b -&gt; unit</code> and the second <code>'b</code>. The output produced by the function is inserted in the output of <code>fprintf</code> at the current point.</li><li><code>t</code>: same as <code>%a</code>, but take only one argument (with type <code>out_channel -&gt; unit</code>) and apply it to <code>outchan</code>.</li><li><code>\{ fmt %\}</code>: convert a format string argument to its type digest. The argument must have the same type as the internal format string <code>fmt</code>.</li><li><code>( fmt %)</code>: format string substitution. Take a format string argument and substitute it to the internal format string <code>fmt</code> to print following arguments. The argument must have the same type as the internal format string <code>fmt</code>.</li><li><code>!</code>: take no argument and flush the output.</li><li><code>%</code>: take no argument and output one <code>%</code> character.</li><li><code>\@</code>: take no argument and output one <code>\@</code> character.</li><li><code>,</code>: take no argument and output nothing: a no-op delimiter for conversion specifications.</li></ul><p>The optional <code>flags</code> are:</p><ul><li><code>-</code>: left-justify the output (default is right justification).</li><li><code>0</code>: for numerical conversions, pad with zeroes instead of spaces.</li><li><code>+</code>: for signed numerical conversions, prefix number with a <code>+</code> sign if positive.</li><li>space: for signed numerical conversions, prefix number with a space if positive.</li><li><code>#</code>: request an alternate formatting style for the integer types and the floating-point type <code>F</code>.</li></ul><p>The optional <code>width</code> is an integer indicating the minimal width of the result. For instance, <code>%6d</code> prints an integer, prefixing it with spaces to fill at least 6 characters.</p><p>The optional <code>precision</code> is a dot <code>.</code> followed by an integer indicating how many digits follow the decimal point in the <code>%f</code>, <code>%e</code>, <code>%E</code>, <code>%h</code>, and <code>%H</code> conversions or the maximum number of significant digits to appear for the <code>%F</code>, <code>%g</code> and <code>%G</code> conversions. For instance, <code>%.4f</code> prints a <code>float</code> with 4 fractional digits.</p><p>The integer in a <code>width</code> or <code>precision</code> can also be specified as <code>*</code>, in which case an extra integer argument is taken to specify the corresponding <code>width</code> or <code>precision</code>. This integer argument precedes immediately the argument to print. For instance, <code>%.*f</code> prints a <code>float</code> with as many fractional digits as the value of the argument given before the float.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-printf"><a href="#val-printf" class="anchor"></a><code><span><span class="keyword">val</span> printf : <span><span><span>(<span class="type-var">'a</span>, <a href="../index.html#type-out_channel">out_channel</a>, unit)</span> <a href="../index.html#type-format">format</a></span> <span class="arrow">&#45;&gt;</span></span> <span class="type-var">'a</span></span></code></div><div class="spec-doc"><p>Same as <a href="#val-fprintf"><code>Printf.fprintf</code></a>, but output on <code>stdout</code>.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-eprintf"><a href="#val-eprintf" class="anchor"></a><code><span><span class="keyword">val</span> eprintf : <span><span><span>(<span class="type-var">'a</span>, <a href="../index.html#type-out_channel">out_channel</a>, unit)</span> <a href="../index.html#type-format">format</a></span> <span class="arrow">&#45;&gt;</span></span> <span class="type-var">'a</span></span></code></div><div class="spec-doc"><p>Same as <a href="#val-fprintf"><code>Printf.fprintf</code></a>, but output on <code>stderr</code>.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-sprintf"><a href="#val-sprintf" class="anchor"></a><code><span><span class="keyword">val</span> sprintf : <span><span><span>(<span class="type-var">'a</span>, unit, string)</span> <a href="../index.html#type-format">format</a></span> <span class="arrow">&#45;&gt;</span></span> <span class="type-var">'a</span></span></code></div><div class="spec-doc"><p>Same as <a href="#val-fprintf"><code>Printf.fprintf</code></a>, but instead of printing on an output channel, return a string containing the result of formatting the arguments.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-bprintf"><a href="#val-bprintf" class="anchor"></a><code><span><span class="keyword">val</span> bprintf : <span><a href="../Buffer/index.html#type-t">Buffer.t</a> <span class="arrow">&#45;&gt;</span></span> <span><span><span>(<span class="type-var">'a</span>, <a href="../Buffer/index.html#type-t">Buffer.t</a>, unit)</span> <a href="../index.html#type-format">format</a></span> <span class="arrow">&#45;&gt;</span></span> <span class="type-var">'a</span></span></code></div><div class="spec-doc"><p>Same as <a href="#val-fprintf"><code>Printf.fprintf</code></a>, but instead of printing on an output channel, append the formatted arguments to the given extensible buffer (see module <a href="../Buffer/index.html"><code>Buffer</code></a>).</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-ifprintf"><a href="#val-ifprintf" class="anchor"></a><code><span><span class="keyword">val</span> ifprintf : <span><span class="type-var">'b</span> <span class="arrow">&#45;&gt;</span></span> <span><span><span>(<span class="type-var">'a</span>, <span class="type-var">'b</span>, <span class="type-var">'c</span>, unit)</span> <a href="../index.html#type-format4">format4</a></span> <span class="arrow">&#45;&gt;</span></span> <span class="type-var">'a</span></span></code></div><div class="spec-doc"><p>Same as <a href="#val-fprintf"><code>Printf.fprintf</code></a>, but does not print anything. Useful to ignore some material when conditionally printing.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 3.10.0</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-ibprintf"><a href="#val-ibprintf" class="anchor"></a><code><span><span class="keyword">val</span> ibprintf : <span><a href="../Buffer/index.html#type-t">Buffer.t</a> <span class="arrow">&#45;&gt;</span></span> <span><span><span>(<span class="type-var">'a</span>, <a href="../Buffer/index.html#type-t">Buffer.t</a>, unit)</span> <a href="../index.html#type-format">format</a></span> <span class="arrow">&#45;&gt;</span></span> <span class="type-var">'a</span></span></code></div><div class="spec-doc"><p>Same as <a href="#val-bprintf"><code>Printf.bprintf</code></a>, but does not print anything. Useful to ignore some material when conditionally printing.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 4.11.0</li></ul></div></div><p>Formatted output functions with continuations.</p><div class="odoc-spec"><div class="spec value anchored" id="val-kfprintf"><a href="#val-kfprintf" class="anchor"></a><code><span><span class="keyword">val</span> kfprintf :
<span><span>(<span><a href="../index.html#type-out_channel">out_channel</a> <span class="arrow">&#45;&gt;</span></span> <span class="type-var">'d</span>)</span> <span class="arrow">&#45;&gt;</span></span>
<span><a href="../index.html#type-out_channel">out_channel</a> <span class="arrow">&#45;&gt;</span></span>
<span><span><span>(<span class="type-var">'a</span>, <a href="../index.html#type-out_channel">out_channel</a>, unit, <span class="type-var">'d</span>)</span> <a href="../index.html#type-format4">format4</a></span> <span class="arrow">&#45;&gt;</span></span>
<span class="type-var">'a</span></span></code></div><div class="spec-doc"><p>Same as <code>fprintf</code>, but instead of returning immediately, passes the out channel to its first argument at the end of printing.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 3.09.0</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-ikfprintf"><a href="#val-ikfprintf" class="anchor"></a><code><span><span class="keyword">val</span> ikfprintf : <span><span>(<span><span class="type-var">'b</span> <span class="arrow">&#45;&gt;</span></span> <span class="type-var">'d</span>)</span> <span class="arrow">&#45;&gt;</span></span> <span><span class="type-var">'b</span> <span class="arrow">&#45;&gt;</span></span> <span><span><span>(<span class="type-var">'a</span>, <span class="type-var">'b</span>, <span class="type-var">'c</span>, <span class="type-var">'d</span>)</span> <a href="../index.html#type-format4">format4</a></span> <span class="arrow">&#45;&gt;</span></span> <span class="type-var">'a</span></span></code></div><div class="spec-doc"><p>Same as <code>kfprintf</code> above, but does not print anything. Useful to ignore some material when conditionally printing.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 4.01.0</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-ksprintf"><a href="#val-ksprintf" class="anchor"></a><code><span><span class="keyword">val</span> ksprintf : <span><span>(<span>string <span class="arrow">&#45;&gt;</span></span> <span class="type-var">'d</span>)</span> <span class="arrow">&#45;&gt;</span></span> <span><span><span>(<span class="type-var">'a</span>, unit, string, <span class="type-var">'d</span>)</span> <a href="../index.html#type-format4">format4</a></span> <span class="arrow">&#45;&gt;</span></span> <span class="type-var">'a</span></span></code></div><div class="spec-doc"><p>Same as <code>sprintf</code> above, but instead of returning the string, passes it to the first argument.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 3.09.0</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-kbprintf"><a href="#val-kbprintf" class="anchor"></a><code><span><span class="keyword">val</span> kbprintf :
<span><span>(<span><a href="../Buffer/index.html#type-t">Buffer.t</a> <span class="arrow">&#45;&gt;</span></span> <span class="type-var">'d</span>)</span> <span class="arrow">&#45;&gt;</span></span>
<span><a href="../Buffer/index.html#type-t">Buffer.t</a> <span class="arrow">&#45;&gt;</span></span>
<span><span><span>(<span class="type-var">'a</span>, <a href="../Buffer/index.html#type-t">Buffer.t</a>, unit, <span class="type-var">'d</span>)</span> <a href="../index.html#type-format4">format4</a></span> <span class="arrow">&#45;&gt;</span></span>
<span class="type-var">'a</span></span></code></div><div class="spec-doc"><p>Same as <code>bprintf</code>, but instead of returning immediately, passes the buffer to its first argument at the end of printing.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 3.10.0</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-ikbprintf"><a href="#val-ikbprintf" class="anchor"></a><code><span><span class="keyword">val</span> ikbprintf :
<span><span>(<span><a href="../Buffer/index.html#type-t">Buffer.t</a> <span class="arrow">&#45;&gt;</span></span> <span class="type-var">'d</span>)</span> <span class="arrow">&#45;&gt;</span></span>
<span><a href="../Buffer/index.html#type-t">Buffer.t</a> <span class="arrow">&#45;&gt;</span></span>
<span><span><span>(<span class="type-var">'a</span>, <a href="../Buffer/index.html#type-t">Buffer.t</a>, unit, <span class="type-var">'d</span>)</span> <a href="../index.html#type-format4">format4</a></span> <span class="arrow">&#45;&gt;</span></span>
<span class="type-var">'a</span></span></code></div><div class="spec-doc"><p>Same as <code>kbprintf</code> above, but does not print anything. Useful to ignore some material when conditionally printing.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 4.11.0</li></ul></div></div><p>Deprecated</p><div class="odoc-spec"><div class="spec value anchored" id="val-kprintf"><a href="#val-kprintf" class="anchor"></a><code><span><span class="keyword">val</span> kprintf : <span><span>(<span>string <span class="arrow">&#45;&gt;</span></span> <span class="type-var">'b</span>)</span> <span class="arrow">&#45;&gt;</span></span> <span><span><span>(<span class="type-var">'a</span>, unit, string, <span class="type-var">'b</span>)</span> <a href="../index.html#type-format4">format4</a></span> <span class="arrow">&#45;&gt;</span></span> <span class="type-var">'a</span></span></code></div><div class="spec-doc"><p>A deprecated synonym for <code>ksprintf</code>.</p><ul class="at-tags"><li class="deprecated"><span class="at-tag">deprecated</span> Use Printf.ksprintf instead.</li></ul></div></div></div></body></html>