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