1printf(3UCB)       SunOS/BSD Compatibility Library Functions      printf(3UCB)
2
3
4

NAME

6       printf,  fprintf, sprintf, vprintf, vfprintf, vsprintf - formatted out‐
7       put conversion
8

SYNOPSIS

10       /usr/ucb/cc [flag ...] file ...
11       #include <stdio.h>
12
13       int printf(format, ...)
14       const char *format;
15
16
17       int fprintf(stream, format, va_list)
18       FILE *stream;
19       char *format;
20       va_dcl;
21
22
23       char *sprintf(s, format, va_list)
24       char *s, *format;
25       va_dcl;
26
27
28       int vprintf(format, ap)
29       char *format;
30       va_list ap;
31
32
33       int vfprintf(stream, format, ap)
34       FILE *stream;
35       char *format;
36       va_list ap;
37
38
39       char *vsprintf(s, format, ap)
40       char *s, *format;
41       va_list ap;
42
43

DESCRIPTION

45       printf() places output on the standard output stream stdout.  fprintf()
46       places  output  on  the named output stream. sprintf() places "output,"
47       followed by the NULL character (\0), in consecutive bytes  starting  at
48       *s;  it  is  the user's responsibility to ensure that enough storage is
49       available.
50
51
52       vprintf(),  vfprintf(),  and  vsprintf()  are  the  same  as  printf(),
53       fprintf(),  and  sprintf()  respectively,  except that instead of being
54       called with a variable number of arguments, they  are  called  with  an
55       argument list as defined by <varargs.h>.
56
57
58       Each  of  these  functions converts, formats, and prints its args under
59       control of the format. The format is a character string which  contains
60       two  types of objects: plain characters, which are simply copied to the
61       output stream, and conversion specifications, each of which causes con‐
62       version and printing of zero or more args. The results are undefined if
63       there are insufficient args for the format. If the format is  exhausted
64       while args remain, the excess args are simply ignored.
65
66
67       Each  conversion  specification is introduced by the character %. After
68       the %, the following appear in sequence:
69
70           o      Zero or more flags, which modify the meaning of the  conver‐
71                  sion specification.
72
73           o      An  optional decimal digit string specifying a minimum field
74                  width. If the converted value has fewer characters than  the
75                  field width, it will be padded on the left (or right, if the
76                  left-adjustment flag `', described below, has  been  given)
77                  to  the  field  width. The padding is with blanks unless the
78                  field width digit string starts with a zero, in  which  case
79                  the padding is with zeros.
80
81           o      A  precision  that  gives  the  minimum  number of digits to
82                  appear for the d, i, o, u, x, or X conversions,  the  number
83                  of  digits  to  appear after the decimal point for the e, E,
84                  and f conversions, the maximum number of significant  digits
85                  for the g and G conversion, or the maximum number of charac‐
86                  ters to be printed from a string in s conversion. The preci‐
87                  sion  takes  the  form of a period (.) followed by a decimal
88                  digit string; a NULL digit string is treated as  zero.  Pad‐
89                  ding specified by the precision overrides the padding speci‐
90                  fied by the field width.
91
92           o      An optional l (ell) specifying that a following d, i, o,  u,
93                  x,  or X conversion character applies to a long integer arg.
94                  An l before any other conversion character is ignored.
95
96           o      A character that indicates the  type  of  conversion  to  be
97                  applied.
98
99
100       A  field width or precision or both may be indicated by an asterisk (*)
101       instead of a digit string. In this case, an integer  arg  supplies  the
102       field  width  or  precision.  The arg that is actually converted is not
103       fetched until the conversion letter is seen,  so  the  args  specifying
104       field width or precision must appear before the arg (if any) to be con‐
105       verted. A negative field width argument is taken as a `' flag followed
106       by  a  positive  field width. If the precision argument is negative, it
107       will be changed to zero.
108
109
110       The flag characters and their meanings are:
111
112       The result of the conversion will be left-justified within the
113                field.
114
115
116       +        The  result  of  a  signed conversion will always begin with a
117                sign (+ or ).
118
119
120       blank    If the first character of a signed conversion is not a sign, a
121                blank will be prefixed to the result. This implies that if the
122                blank and + flags both appear, the blank flag will be ignored.
123
124
125       #        This flag specifies that the value is to be  converted  to  an
126                "alternate  form." For c, d, i, s, and u conversions, the flag
127                has no effect. For o conversion, it increases the precision to
128                force  the  first digit of the result to be a zero. For x or X
129                conversion, a non-zero result will have 0x or 0X  prefixed  to
130                it.  For e, E, f, g, and G conversions, the result will always
131                contain a decimal point, even if no digits  follow  the  point
132                (normally, a decimal point appears in the result of these con‐
133                versions only if a digit follows it). For g and G conversions,
134                trailing  zeroes  will  not  be removed from the result (which
135                they normally are).
136
137
138
139       The conversion characters and their meanings are:
140
141       d,i,o,u,x,X    The integer arg is converted to signed decimal (d or i),
142                      unsigned  octal  (o),  unsigned decimal (u), or unsigned
143                      hexadecimal notation (x and X), respectively;  the  let‐
144                      ters  abcdef  are  used for x conversion and the letters
145                      ABCDEF for X conversion.  The  precision  specifies  the
146                      minimum  number  of digits to appear; if the value being
147                      converted can be represented in fewer digits, it will be
148                      expanded  with  leading  zeroes. (For compatibility with
149                      older versions, padding with leading zeroes may alterna‐
150                      tively  be  specified  by prepending a zero to the field
151                      width. This does not imply an octal value for the  field
152                      width.)  The  default precision is 1. The result of con‐
153                      verting a zero value with a precision of zero is a  NULL
154                      string.
155
156
157       f              The float or double arg is converted to decimal notation
158                      in the style []ddd.ddd where the number of digits after
159                      the  decimal  point is equal to the precision specifica‐
160                      tion. If the precision is missing, 6 digits  are  given;
161                      if the precision is explicitly 0, no digits and no deci‐
162                      mal point are printed.
163
164
165       e,E            The float or  double  arg  is  converted  in  the  style
166                      []d.dddddd, where there is one digit before the deci‐
167                      mal point and the number of digits after it is equal  to
168                      the  precision;  when the precision is missing, 6 digits
169                      are produced; if the precision is zero, no decimal point
170                      appears.  The E format code will produce a number with E
171                      instead of e  introducing  the  exponent.  The  exponent
172                      always contains at least two digits.
173
174
175       g,G            The  float  or double arg is printed in style f or e (or
176                      in style E in the case of a G  format  code),  with  the
177                      precision  specifying  the number of significant digits.
178                      The style used depends on the value converted:  style  e
179                      or  E  will  be used only if the exponent resulting from
180                      the conversion is less than −4 or greater than the  pre‐
181                      cision.  Trailing  zeroes are removed from the result; a
182                      decimal point appears only if it is followed by a digit.
183
184
185
186       The e, E f, g, and G formats print IEEE indeterminate values  (infinity
187       or not-a-number) as "Infinity" or "NaN" respectively.
188
189       c    The character arg is printed.
190
191
192       s    The arg is taken to be a string (character pointer) and characters
193            from the string are printed until a NULL character (\0) is encoun‐
194            tered or until the number of characters indicated by the precision
195            specification is reached. If the precision is missing, it is taken
196            to  be  infinite, so all characters up to the first NULL character
197            are printed. A NULL value for arg will yield undefined results.
198
199
200       %    Print a %; no argument is converted.
201
202
203
204       In no case does a nonexistent or small field width cause truncation  of
205       a  field;  if the result of a conversion is wider than the field width,
206       the field is simply expanded to contain the conversion result.  Padding
207       takes place only if the specified field width exceeds the actual width.
208       Characters generated by  printf()  and  fprintf()  are  printed  as  if
209       putc(3C) had been called.
210

RETURN VALUES

212       Upon  success,  printf()  and fprintf() return the number of characters
213       transmitted, excluding the null  character.  vprintf()  and  vfprintf()
214       return  the  number of characters transmitted. sprintf() and vsprintf()
215       always return s. If an output error is encountered, printf(), fprint(),
216       vprintf(), and vfprintf() return EOF.
217

EXAMPLES

219       Example 1 Examples of the printf Command To Print a Date and Time
220
221
222       To  print  a  date  and time in the form "Sunday, July 3, 10:02," where
223       weekday and month are pointers to NULL-terminated strings:
224
225
226         printf("%s, %s %i, %d:%.2d", weekday, month, day, hour, min);
227
228
229       Example 2 Examples of the printf  Command  To  Print  to  Five  Decimal
230       Places
231
232
233       To print to five decimal places:
234
235
236         printf("pi = %.5f", 4 * atan(1. 0));
237
238

SEE ALSO

240       econvert(3C), putc(3C), scanf(3C), vprintf(3C)
241

NOTES

243       Use of these interfaces should be restricted to only applications writ‐
244       ten on BSD platforms. Use of these interfaces with any  of  the  system
245       libraries or in multi-thread applications is unsupported.
246
247
248       Very wide fields (>128 characters) fail.
249
250
251
252SunOS 5.11                        30 Oct 2007                     printf(3UCB)
Impressum