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.
48
49 - u , n , l , L , or N : convert an integer argument to unsigned deci‐
50 mal. Warning: n , l , L , and N are used for scanf , and should not be
51 used for printf .
52
53 - x : convert an integer argument to unsigned hexadecimal, using lower‐
54 case letters.
55
56 - X : convert an integer argument to unsigned hexadecimal, using upper‐
57 case letters.
58
59 - o : convert an integer argument to unsigned octal.
60
61 - s : insert a string argument.
62
63 - S : convert a string argument to OCaml syntax (double quotes,
64 escapes).
65
66 - c : insert a character argument.
67
68 - C : convert a character argument to OCaml syntax (single quotes,
69 escapes).
70
71 - f : convert a floating-point argument to decimal notation, in the
72 style dddd.ddd .
73
74 - F : convert a floating-point argument to OCaml syntax ( dddd. or
75 dddd.ddd or d.ddd e+-dd ).
76
77 - e or E : convert a floating-point argument to decimal notation, in
78 the style d.ddd e+-dd (mantissa and exponent).
79
80 - g or G : convert a floating-point argument to decimal notation, in
81 style f or e , E (whichever is more compact). Moreover, any trailing
82 zeros are removed from the fractional part of the result and the deci‐
83 mal-point character is removed if there is no fractional part remain‐
84 ing.
85
86 - h or H : convert a floating-point argument to hexadecimal notation,
87 in the style 0xh.hhhh e+-dd (hexadecimal mantissa, exponent in decimal
88 and denotes a power of 2).
89
90 - B : convert a boolean argument to the string true or false
91
92
93 - b : convert a boolean argument (deprecated; do not use in new pro‐
94 grams).
95
96 - ld , li , lu , lx , lX , lo : convert an int32 argument to the format
97 specified by the second letter (decimal, hexadecimal, etc).
98
99 - nd , ni , nu , nx , nX , no : convert a nativeint argument to the
100 format specified by the second letter.
101
102 - Ld , Li , Lu , Lx , LX , Lo : convert an int64 argument to the format
103 specified by the second letter.
104
105 - a : user-defined printer. Take two arguments and apply the first one
106 to outchan (the current output channel) and to the second argument. The
107 first argument must therefore have type out_channel -> 'b -> unit and
108 the second 'b . The output produced by the function is inserted in the
109 output of fprintf at the current point.
110
111 - t : same as %a , but take only one argument (with type out_channel ->
112 unit ) and apply it to outchan .
113
114 - { fmt %} : convert a format string argument to its type digest. The
115 argument must have the same type as the internal format string fmt .
116
117 - ( fmt %) : format string substitution. Take a format string argument
118 and substitute it to the internal format string fmt to print following
119 arguments. The argument must have the same type as the internal format
120 string fmt .
121
122 - ! : take no argument and flush the output.
123
124 - % : take no argument and output one % character.
125
126 - @ : take no argument and output one @ character.
127
128 - , : take no argument and output nothing: a no-op delimiter for con‐
129 version specifications.
130
131 The optional flags are:
132
133 - - : left-justify the output (default is right justification).
134
135 - 0 : for numerical conversions, pad with zeroes instead of spaces.
136
137 - + : for signed numerical conversions, prefix number with a + sign if
138 positive.
139
140 -space: for signed numerical conversions, prefix number with a space if
141 positive.
142
143 - # : request an alternate formatting style for the integer types ( x ,
144 X , o , lx , lX , lo , Lx , LX , Lo , d , i , u , ld , li , lu , Ld ,
145 Li , Lu , nd , ni , nu ).
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, out_channel, unit) format -> 'a
165
166 Same as Printf.fprintf , but output on stdout .
167
168
169
170 val eprintf : ('a, out_channel, unit) format -> 'a
171
172 Same as Printf.fprintf , but output on stderr .
173
174
175
176 val sprintf : ('a, unit, string) format -> 'a
177
178 Same as Printf.fprintf , but instead of printing on an output channel,
179 return a string containing the result of formatting the arguments.
180
181
182
183 val bprintf : Buffer.t -> ('a, Buffer.t, unit) format -> 'a
184
185 Same as Printf.fprintf , but instead of printing on an output channel,
186 append the formatted arguments to the given extensible buffer (see mod‐
187 ule Buffer ).
188
189
190
191 val ifprintf : 'b -> ('a, 'b, 'c, unit) format4 -> 'a
192
193 Same as Printf.fprintf , but does not print anything. Useful to ignore
194 some material when conditionally printing.
195
196
197 Since 3.10.0
198
199
200
201
202 Formatted output functions with continuations.
203
204 val kfprintf : (out_channel -> 'd) -> out_channel -> ('a, out_channel,
205 unit, 'd) format4 -> 'a
206
207 Same as fprintf , but instead of returning immediately, passes the out
208 channel to its first argument at the end of printing.
209
210
211 Since 3.09.0
212
213
214
215 val ikfprintf : ('b -> 'd) -> 'b -> ('a, 'b, 'c, 'd) format4 -> '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) format4 -> 'a
226
227 Same as sprintf above, but instead of returning the string, passes it
228 to the first argument.
229
230
231 Since 3.09.0
232
233
234
235 val kbprintf : (Buffer.t -> 'd) -> Buffer.t -> ('a, Buffer.t, unit, 'd)
236 format4 -> 'a
237
238 Same as bprintf , but instead of returning immediately, passes the buf‐
239 fer to its first argument at the end of printing.
240
241
242 Since 3.10.0
243
244
245
246
247 Deprecated
248
249 val kprintf : (string -> 'b) -> ('a, unit, string, 'b) format4 -> 'a
250
251 A deprecated synonym for ksprintf .
252
253
254
255
256
257OCamldoc 2019-07-30 Printf(3)