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