1Printf(3) OCaml library Printf(3)
2
3
4
6 Printf - Formatted output functions.
7
9 Module Printf
10
12 Module Printf
13 : sig end
14
15
16 Formatted output functions.
17
18
19
20
21
22
23 val fprintf : out_channel -> ('a, out_channel, unit) format -> 'a
24
25
26 fprintf outchan format arg1 ... argN formats the arguments arg1 to argN
27 according to the format string format , and outputs the resulting
28 string on the channel outchan .
29
30 The format string is a character string which contains two types of
31 objects: plain characters, which are simply copied to the output chan‐
32 nel, and conversion specifications, each of which causes conversion and
33 printing of arguments.
34
35 Conversion specifications have the following form:
36
37
38 % [flags] [width] [.precision] type
39
40 In short, a conversion specification consists in the % character, fol‐
41 lowed by optional modifiers and a type which is made of one or two
42 characters.
43
44 The types and their meanings are:
45
46
47 - d , i : convert an integer argument to signed decimal. The flag #
48 adds underscores to large values for readability.
49
50 - u , n , l , L , or N : convert an integer argument to unsigned deci‐
51 mal. Warning: n , l , L , and N are used for scanf , and should not be
52 used for printf . The flag # adds underscores to large values for
53 readability.
54
55 - x : convert an integer argument to unsigned hexadecimal, using lower‐
56 case letters. The flag # adds a 0x prefix to non zero values.
57
58 - X : convert an integer argument to unsigned hexadecimal, using upper‐
59 case letters. The flag # adds a 0X prefix to non zero values.
60
61 - o : convert an integer argument to unsigned octal. The flag # adds a
62 0 prefix to non zero values.
63
64 - s : insert a string argument.
65
66 - S : convert a string argument to OCaml syntax (double quotes,
67 escapes).
68
69 - c : insert a character argument.
70
71 - C : convert a character argument to OCaml syntax (single quotes,
72 escapes).
73
74 - f : convert a floating-point argument to decimal notation, in the
75 style dddd.ddd .
76
77 - F : convert a floating-point argument to OCaml syntax ( dddd. or
78 dddd.ddd or d.ddd e+-dd ). Converts to hexadecimal with the # flag
79 (see h ).
80
81 - e or E : convert a floating-point argument to decimal notation, in
82 the style d.ddd e+-dd (mantissa and exponent).
83
84 - g or G : convert a floating-point argument to decimal notation, in
85 style f or e , E (whichever is more compact). Moreover, any trailing
86 zeros are removed from the fractional part of the result and the deci‐
87 mal-point character is removed if there is no fractional part remain‐
88 ing.
89
90 - h or H : convert a floating-point argument to hexadecimal notation,
91 in the style 0xh.hhhh p+-dd (hexadecimal mantissa, exponent in decimal
92 and denotes a power of 2).
93
94 - B : convert a boolean argument to the string true or false
95
96
97 - b : convert a boolean argument (deprecated; do not use in new pro‐
98 grams).
99
100 - ld , li , lu , lx , lX , lo : convert an int32 argument to the format
101 specified by the second letter (decimal, hexadecimal, etc).
102
103 - nd , ni , nu , nx , nX , no : convert a nativeint argument to the
104 format specified by the second letter.
105
106 - Ld , Li , Lu , Lx , LX , Lo : convert an int64 argument to the format
107 specified by the second letter.
108
109 - a : user-defined printer. Take two arguments and apply the first one
110 to outchan (the current output channel) and to the second argument. The
111 first argument must therefore have type out_channel -> 'b -> unit and
112 the second 'b . The output produced by the function is inserted in the
113 output of fprintf at the current point.
114
115 - t : same as %a , but take only one argument (with type out_channel ->
116 unit ) and apply it to outchan .
117
118 - { fmt %} : convert a format string argument to its type digest. The
119 argument must have the same type as the internal format string fmt .
120
121 - ( fmt %) : format string substitution. Take a format string argument
122 and substitute it to the internal format string fmt to print following
123 arguments. The argument must have the same type as the internal format
124 string fmt .
125
126 - ! : take no argument and flush the output.
127
128 - % : take no argument and output one % character.
129
130 - @ : take no argument and output one @ character.
131
132 - , : take no argument and output nothing: a no-op delimiter for con‐
133 version specifications.
134
135 The optional flags are:
136
137 - - : left-justify the output (default is right justification).
138
139 - 0 : for numerical conversions, pad with zeroes instead of spaces.
140
141 - + : for signed numerical conversions, prefix number with a + sign if
142 positive.
143
144 -space: for signed numerical conversions, prefix number with a space if
145 positive.
146
147 - # : request an alternate formatting style for the integer types and
148 the floating-point type F .
149
150 The optional width is an integer indicating the minimal width of the
151 result. For instance, %6d prints an integer, prefixing it with spaces
152 to fill at least 6 characters.
153
154 The optional precision is a dot . followed by an integer indicating
155 how many digits follow the decimal point in the %f , %e , %E , %h , and
156 %H conversions or the maximum number of significant digits to appear
157 for the %F , %g and %G conversions. For instance, %.4f prints a float
158 with 4 fractional digits.
159
160 The integer in a width or precision can also be specified as * , in
161 which case an extra integer argument is taken to specify the corre‐
162 sponding width or precision . This integer argument precedes immedi‐
163 ately the argument to print. For instance, %.*f prints a float with as
164 many fractional digits as the value of the argument given before the
165 float.
166
167
168
169 val printf : ('a, out_channel, unit) format -> 'a
170
171 Same as Printf.fprintf , but output on stdout .
172
173
174
175 val eprintf : ('a, out_channel, unit) format -> 'a
176
177 Same as Printf.fprintf , but output on stderr .
178
179
180
181 val sprintf : ('a, unit, string) format -> 'a
182
183 Same as Printf.fprintf , but instead of printing on an output channel,
184 return a string containing the result of formatting the arguments.
185
186
187
188 val bprintf : Buffer.t -> ('a, Buffer.t, unit) format -> 'a
189
190 Same as Printf.fprintf , but instead of printing on an output channel,
191 append the formatted arguments to the given extensible buffer (see mod‐
192 ule Buffer ).
193
194
195
196 val ifprintf : 'b -> ('a, 'b, 'c, unit) format4 -> 'a
197
198 Same as Printf.fprintf , but does not print anything. Useful to ignore
199 some material when conditionally printing.
200
201
202 Since 3.10.0
203
204
205
206
207 Formatted output functions with continuations.
208
209 val kfprintf : (out_channel -> 'd) -> out_channel -> ('a, out_channel,
210 unit, 'd) format4 -> 'a
211
212 Same as fprintf , but instead of returning immediately, passes the out
213 channel to its first argument at the end of printing.
214
215
216 Since 3.09.0
217
218
219
220 val ikfprintf : ('b -> 'd) -> 'b -> ('a, 'b, 'c, 'd) format4 -> 'a
221
222 Same as kfprintf above, but does not print anything. Useful to ignore
223 some material when conditionally printing.
224
225
226 Since 4.01.0
227
228
229
230 val ksprintf : (string -> 'd) -> ('a, unit, string, 'd) format4 -> 'a
231
232 Same as sprintf above, but instead of returning the string, passes it
233 to the first argument.
234
235
236 Since 3.09.0
237
238
239
240 val kbprintf : (Buffer.t -> 'd) -> Buffer.t -> ('a, Buffer.t, unit, 'd)
241 format4 -> 'a
242
243 Same as bprintf , but instead of returning immediately, passes the buf‐
244 fer to its first argument at the end of printing.
245
246
247 Since 3.10.0
248
249
250
251
252 Deprecated
253
254 val kprintf : (string -> 'b) -> ('a, unit, string, 'b) format4 -> 'a
255
256 A deprecated synonym for ksprintf .
257
258
259
260
261
262OCamldoc 2020-02-27 Printf(3)