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
24 val fprintf : Pervasives.out_channel -> ('a, Pervasives.out_channel,
25 unit) Pervasives.format -> 'a
26
27
28 fprintf outchan format arg1 ... argN formats the arguments arg1 to argN
29 according to the format string format , and outputs the resulting
30 string on the channel outchan .
31
32 The format is a character string which contains two types of objects:
33 plain characters, which are simply copied to the output channel, and
34 conversion specifications, each of which causes conversion and printing
35 of arguments.
36
37 Conversion specifications have the following form:
38
39
40 % [positional specifier] [flags] [width] [.precision] type
41
42 In short, a conversion specification consists in the % character, fol‐
43 lowed by optional modifiers and a type which is made of one or two
44 characters. The types and their meanings are:
45
46
47 - d , i , n , l , L , or N : convert an integer argument to signed dec‐
48 imal.
49
50 - u : convert an integer argument to unsigned decimal.
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 : insert a string argument in Caml syntax (double quotes, escapes).
63
64 - c : insert a character argument.
65
66 - C : insert a character argument in Caml syntax (single quotes,
67 escapes).
68
69 - f : convert a floating-point argument to decimal notation, in the
70 style dddd.ddd .
71
72 - F : convert a floating-point argument to Caml syntax ( dddd. or
73 dddd.ddd or d.ddd e+-dd ).
74
75 - e or E : convert a floating-point argument to decimal notation, in
76 the style d.ddd e+-dd (mantissa and exponent).
77
78 - g or G : convert a floating-point argument to decimal notation, in
79 style f or e , E (whichever is more compact).
80
81 - B : convert a boolean argument to the string true or false
82
83
84 - b : convert a boolean argument (for backward compatibility; do not
85 use in new programs).
86
87 - ld , li , lu , lx , lX , lo : convert an int32 argument to the format
88 specified by the second letter (decimal, hexadecimal, etc).
89
90 - nd , ni , nu , nx , nX , no : convert a nativeint argument to the
91 format specified by the second letter.
92
93 - Ld , Li , Lu , Lx , LX , Lo : convert an int64 argument to the format
94 specified by the second letter.
95
96 - a : user-defined printer. Takes two arguments and apply the first one
97 to outchan (the current output channel) and to the second argument. The
98 first argument must therefore have type out_channel -> 'b -> unit and
99 the second 'b . The output produced by the function is inserted in the
100 output of fprintf at the current point.
101
102 - t : same as %a , but takes only one argument (with type out_channel
103 -> unit ) and apply it to outchan .
104
105 - { fmt %} : convert a format string argument. The argument must have
106 the same type as the internal format string fmt .
107
108 - ( fmt %) : format string substitution. Takes a format string argument
109 and substitutes it to the internal format string fmt to print following
110 arguments. The argument must have the same type as fmt .
111
112 - ! : take no argument and flush the output.
113
114 - % : take no argument and output one % character.
115
116 The optional positional specifier consists of an integer followed by a
117 $ ; the integer indicates which argument to use, the first argument
118 being denoted by 1.
119
120 The optional flags are:
121
122 - - : left-justify the output (default is right justification).
123
124 - 0 : for numerical conversions, pad with zeroes instead of spaces.
125
126 - + : for numerical conversions, prefix number with a + sign if posi‐
127 tive.
128
129 -space: for numerical conversions, prefix number with a space if posi‐
130 tive.
131
132 - # : request an alternate formatting style for numbers.
133
134 The optional width is an integer indicating the minimal width of the
135 result. For instance, %6d prints an integer, prefixing it with spaces
136 to fill at least 6 characters.
137
138 The optional precision is a dot . followed by an integer indicating
139 how many digits follow the decimal point in the %f , %e , and %E con‐
140 versions. For instance, %.4f prints a float with 4 fractional digits.
141
142 The integer in a width or precision can also be specified as * , in
143 which case an extra integer argument is taken to specify the corre‐
144 sponding width or precision . This integer argument precedes immedi‐
145 ately the argument to print, unless an optional positional specifier is
146 given to indicates which argument to use. For instance, %.*3$f prints a
147 float with as many fractional digits as the value of the third argu‐
148 ment.
149
150
151
152
153 val printf : ('a, Pervasives.out_channel, unit) Pervasives.format -> 'a
154
155 Same as Printf.fprintf , but output on stdout .
156
157
158
159
160 val eprintf : ('a, Pervasives.out_channel, unit) Pervasives.format ->
161 'a
162
163 Same as Printf.fprintf , but output on stderr .
164
165
166
167
168 val sprintf : ('a, unit, string) Pervasives.format -> 'a
169
170 Same as Printf.fprintf , but instead of printing on an output channel,
171 return a string containing the result of formatting the arguments.
172
173
174
175
176 val bprintf : Buffer.t -> ('a, Buffer.t, unit) Pervasives.format -> 'a
177
178 Same as Printf.fprintf , but instead of printing on an output channel,
179 append the formatted arguments to the given extensible buffer (see mod‐
180 ule Buffer ).
181
182
183
184
185 val kfprintf : (Pervasives.out_channel -> 'a) -> Pervasives.out_channel
186 -> ('b, Pervasives.out_channel, unit, 'a) format4 -> 'b
187
188 Same as fprintf , but instead of returning immediately, passes the out
189 channel to its first argument at the end of printing.
190
191
192
193
194 val ksprintf : (string -> 'a) -> ('b, unit, string, 'a) format4 -> 'b
195
196 Same as sprintf above, but instead of returning the string, passes it
197 to the first argument.
198
199
200
201
202 val kprintf : (string -> 'a) -> ('b, unit, string, 'a) format4 -> 'b
203
204 A deprecated synonym for ksprintf .
205
206
207
208
209
210
211OCamldoc 2007-05-24 Printf(3)