1Printf(3) OCamldoc 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).
83
84 - h or H : convert a floating-point argument to hexadecimal notation,
85 in the style 0xh.hhhh e+-dd (hexadecimal mantissa, exponent in decimal
86 and denotes a power of 2).
87
88 - B : convert a boolean argument to the string true or false
89
90
91 - b : convert a boolean argument (deprecated; do not use in new pro‐
92 grams).
93
94 - ld , li , lu , lx , lX , lo : convert an int32 argument to the format
95 specified by the second letter (decimal, hexadecimal, etc).
96
97 - nd , ni , nu , nx , nX , no : convert a nativeint argument to the
98 format specified by the second letter.
99
100 - Ld , Li , Lu , Lx , LX , Lo : convert an int64 argument to the format
101 specified by the second letter.
102
103 - a : user-defined printer. Take two arguments and apply the first one
104 to outchan (the current output channel) and to the second argument. The
105 first argument must therefore have type out_channel -> 'b -> unit and
106 the second 'b . The output produced by the function is inserted in the
107 output of fprintf at the current point.
108
109 - t : same as %a , but take only one argument (with type out_channel ->
110 unit ) and apply it to outchan .
111
112 - { fmt %} : convert a format string argument to its type digest. The
113 argument must have the same type as the internal format string fmt .
114
115 - ( fmt %) : format string substitution. Take a format string argument
116 and substitute it to the internal format string fmt to print following
117 arguments. The argument must have the same type as the internal format
118 string fmt .
119
120 - ! : take no argument and flush the output.
121
122 - % : take no argument and output one % character.
123
124 - @ : take no argument and output one @ character.
125
126 - , : take no argument and output nothing: a no-op delimiter for con‐
127 version specifications.
128
129 The optional flags are:
130
131 - - : left-justify the output (default is right justification).
132
133 - 0 : for numerical conversions, pad with zeroes instead of spaces.
134
135 - + : for signed numerical conversions, prefix number with a + sign if
136 positive.
137
138 -space: for signed numerical conversions, prefix number with a space if
139 positive.
140
141 - # : request an alternate formatting style for the hexadecimal and
142 octal integer types ( x , X , o , lx , lX , lo , Lx , LX , Lo ).
143
144 The optional width is an integer indicating the minimal width of the
145 result. For instance, %6d prints an integer, prefixing it with spaces
146 to fill at least 6 characters.
147
148 The optional precision is a dot . followed by an integer indicating
149 how many digits follow the decimal point in the %f , %e , and %E con‐
150 versions. For instance, %.4f prints a float with 4 fractional digits.
151
152 The integer in a width or precision can also be specified as * , in
153 which case an extra integer argument is taken to specify the corre‐
154 sponding width or precision . This integer argument precedes immedi‐
155 ately the argument to print. For instance, %.*f prints a float with as
156 many fractional digits as the value of the argument given before the
157 float.
158
159
160
161 val printf : ('a, Pervasives.out_channel, unit) Pervasives.format -> 'a
162
163 Same as Printf.fprintf , but output on stdout .
164
165
166
167 val eprintf : ('a, Pervasives.out_channel, unit) Pervasives.format ->
168 'a
169
170 Same as Printf.fprintf , but output on stderr .
171
172
173
174 val sprintf : ('a, unit, string) Pervasives.format -> 'a
175
176 Same as Printf.fprintf , but instead of printing on an output channel,
177 return a string containing the result of formatting the arguments.
178
179
180
181 val bprintf : Buffer.t -> ('a, Buffer.t, unit) Pervasives.format -> 'a
182
183 Same as Printf.fprintf , but instead of printing on an output channel,
184 append the formatted arguments to the given extensible buffer (see mod‐
185 ule Buffer ).
186
187
188
189 val ifprintf : 'b -> ('a, 'b, 'c, unit) Pervasives.format4 -> 'a
190
191 Same as Printf.fprintf , but does not print anything. Useful to ignore
192 some material when conditionally printing.
193
194
195 Since 3.10.0
196
197
198
199
200 === Formatted output functions with continuations. ===
201
202
203 val kfprintf : (Pervasives.out_channel -> 'd) -> Pervasives.out_channel
204 -> ('a, Pervasives.out_channel, unit, 'd) Pervasives.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) Pervasives.format4
215 -> 'a
216
217 Same as kfprintf above, but does not print anything. Useful to ignore
218 some material when conditionally printing.
219
220
221 Since 4.01.0
222
223
224
225 val ksprintf : (string -> 'd) -> ('a, unit, string, 'd) Pervasives.for‐
226 mat4 -> 'a
227
228 Same as sprintf above, but instead of returning the string, passes it
229 to the first argument.
230
231
232 Since 3.09.0
233
234
235
236 val kbprintf : (Buffer.t -> 'd) -> Buffer.t -> ('a, Buffer.t, unit, 'd)
237 Pervasives.format4 -> 'a
238
239 Same as bprintf , but instead of returning immediately, passes the buf‐
240 fer to its first argument at the end of printing.
241
242
243 Since 3.10.0
244
245
246
247
248 === Deprecated ===
249
250
251 val kprintf : (string -> 'b) -> ('a, unit, string, 'b) Pervasives.for‐
252 mat4 -> 'a
253
254 A deprecated synonym for ksprintf .
255
256
257
258
259
2602018-04-14 source: Printf(3)