1printf(3C)               Standard C Library Functions               printf(3C)
2
3
4

NAME

6       printf, fprintf, sprintf, snprintf, asprintf - print formatted output
7

SYNOPSIS

9       #include <stdio.h>
10
11       int printf(const char *restrict format,
12            /* args*/ ...);
13
14
15       int fprintf(FILE *restrict stream, const char *restrict format,
16            /* args*/ ...);
17
18
19       int sprintf(char *restrict s, const char *restrict format,
20            /* args*/ ...);
21
22
23       int snprintf(char *restrict s, size_t n,
24            const char *restrict format, /* args*/ ...);
25
26
27       int asprintf(char ** ret, const char *restrict format,
28            /* args*/ ...);
29
30

DESCRIPTION

32       The  printf() function places output on the standard output stream std‐
33       out.
34
35
36       The fprintf() function places output on  on  the  named  output  stream
37       stream.
38
39
40       The  sprintf()  function places output, followed by the null byte (\0),
41       in consecutive bytes starting at s; it is the user's responsibility  to
42       ensure that enough storage is available.
43
44
45       The  snprintf() function is identical to sprintf() with the addition of
46       the argument n, which specifies the size of the buffer referred  to  by
47       s.  If  n  is 0, nothing is written and s can be a null pointer. Other‐
48       wise, output bytes beyond the n-1st  are  discarded  instead  of  being
49       written to the array and a null byte is written at the end of the bytes
50       actually written into the array.
51
52
53       The asprintf() function is the same as the  sprintf()  function  except
54       that  it  returns,  in  the  ret argument, a pointer to a buffer suffi‐
55       ciently large to hold the output string. This pointer should be  passed
56       to  free(3C)  to  release  the  allocated  storage when it is no longer
57       needed. If sufficient space cannot be allocated, the  asprintf()  func‐
58       tion returns -1 and sets ret to be a NULL pointer.
59
60
61       Each  of  these  functions  converts, formats, and prints its arguments
62       under control of the format. The format is a character  string,  begin‐
63       ning  and ending in its initial shift state, if any. The format is com‐
64       posed of zero or more directives: ordinary characters, which are simply
65       copied  to  the  output  stream  and conversion specifications, each of
66       which results in the fetching of zero or more  arguments.  The  results
67       are  undefined  if  there are insufficient arguments for the format. If
68       the format is exhausted while arguments remain,  the  excess  arguments
69       are evaluated but are otherwise ignored.
70
71
72       Conversions  can be applied to the nth argument after the format in the
73       argument list, rather than to the next unused argument. In  this  case,
74       the conversion specifier % (see below) is replaced by the sequence %n$,
75       where n is a decimal integer in the range [1,  NL_ARGMAX],  giving  the
76       position  of  the  argument in the argument list. This feature provides
77       for the definition of format strings that select arguments in an  order
78       appropriate to specific languages (see the EXAMPLES section).
79
80
81       In format strings containing the %n$ form of conversion specifications,
82       numbered arguments in the argument list can be referenced from the for‐
83       mat string as many times as required.
84
85
86       In  format  strings containing the % form of conversion specifications,
87       each argument in the argument list is used exactly once.
88
89
90       All forms of the printf() functions allow for the insertion of  a  lan‐
91       guage-dependent radix character in the output string. The radix charac‐
92       ter is defined by the program's locale (category  LC_NUMERIC).  In  the
93       POSIX  locale, or in a locale where the radix character is not defined,
94       the radix character defaults to a period (.).
95
96   Conversion Specifications
97       Each conversion specification is introduced by the %  character  or  by
98       the  character  sequence  %n$,  after  which  the  following  appear in
99       sequence:
100
101           o      An optional field, consisting of a decimal digit string fol‐
102                  lowed  by a $, specifying the next argument to be converted.
103                  If this field is not provided, the args following  the  last
104                  argument converted will be used.
105
106           o      Zero  or more flags (in any order), which modify the meaning
107                  of the conversion specification.
108
109           o      An optional minimum field width. If the converted value  has
110                  fewer  bytes  than  the  field width, it will be padded with
111                  spaces by default on the left; it  will  be  padded  on  the
112                  right,  if the left-adjustment flag (), described below, is
113                  given to the field width. The field width takes the form  of
114                  an asterisk (*), described below, or a decimal integer.
115
116                  If  the  conversion  specifier  is  s, a standard-conforming
117                  application (see standards(5)) interprets the field width as
118                  the  minimum  number  of bytes to be printed; an application
119                  that is not standard-conforming interprets the  field  width
120                  as  the  minimum number of columns of screen display. For an
121                  application that is not standard-conforming, %10s  means  if
122                  the  converted value has a screen width of 7 columns, 3 spa‐
123                  ces would be padded on the right.
124
125                  If the format is %ws, then the field width should be  inter‐
126                  preted as the minimum number of columns of screen display.
127
128           o      An  optional precision that gives the minimum number of dig‐
129                  its to appear for the d, i, o, u, x, and X conversions  (the
130                  field is padded with leading zeros); the number of digits to
131                  appear after the radix character for the a, A, e, E, f,  and
132                  F  conversions, the maximum number of significant digits for
133                  the g and G conversions; or the maximum number of  bytes  to
134                  be  printed from a string in s and S conversions. The preci‐
135                  sion takes the form of a period (.) followed  either  by  an
136                  asterisk  (*), described below, or an optional decimal digit
137                  string, where a null digit string is treated as 0. If a pre‐
138                  cision  appears  with  any  other  conversion specifier, the
139                  behavior is undefined.
140
141                  If the conversion specifier is s or S, a standard-conforming
142                  application  (see  standards(5)) interprets the precision as
143                  the maximum number of bytes to be  written;  an  application
144                  that  is not standard-conforming interprets the precision as
145                  the maximum number of columns  of  screen  display.  For  an
146                  application  that  is  not  standard-conforming,  %.5s would
147                  print only the portion of the string that would display in 5
148                  screen columns. Only complete characters are written.
149
150                  For  %ws, the precision should be interpreted as the maximum
151                  number of columns of screen display. The precision takes the
152                  form  of  a period (.) followed by a decimal digit string; a
153                  null digit string is treated as zero. Padding  specified  by
154                  the  precision  overrides the padding specified by the field
155                  width.
156
157           o      An optional length modifier that specified the size  of  the
158                  argument.
159
160           o      A conversion specifier that indicates the type of conversion
161                  to be applied.
162
163
164       A field width, or precision, or both can be indicated  by  an  asterisk
165       (*) . In this case, an argument of type int supplies the field width or
166       precision. Arguments specifying field width, or precision, or both must
167       appear  in  that  order before the argument, if any, to be converted. A
168       negative field width is taken as a − flag followed by a positive  field
169       width.  A negative precision is taken as if the precision were omitted.
170       In format strings containing the %n$ form of  a  conversion  specifica‐
171       tion,  a field width or precision may be indicated by the sequence *m$,
172       where m is a decimal integer in the range  [1,  NL_ARGMAX]  giving  the
173       position in the argument list (after the format argument) of an integer
174       argument containing the field width or precision, for example:
175
176         printf("%1$d:%2$.*3$d:%4$.*3$d\n", hour, min, precision, sec);
177
178
179
180       The format can contain either numbered  argument  specifications  (that
181       is, %n$ and *m$), or unnumbered argument specifications (that is, % and
182       *), but normally not both. The only exception to this is that %% can be
183       mixed  with the %n$ form. The results of mixing numbered and unnumbered
184       argument specifications in a format string are undefined. When numbered
185       argument  specifications are used, specifying the Nth argument requires
186       that all the leading arguments, from the  first  to  the  (N-1)th,  are
187       specified in the format string.
188
189   Flag Characters
190       The flag characters and their meanings are:
191
192       '        The integer portion of the result of a decimal conversion (%i,
193                %d, %u, %f, %F, %g, or %G) will be formatted  with  thousands'
194                grouping  characters.  For  other  conversions the behavior is
195                undefined. The non-monetary grouping character is used.
196
197
198       The result of the conversion will be left-justified within the
199                field.  The conversion will be right-justified if this flag is
200                not specified.
201
202
203       +        The result of a signed conversion will  always  begin  with  a
204                sign (+ or -). The conversion will begin with a sign only when
205                a negative value is converted if this flag is not specified.
206
207
208       space    If the first character of a signed conversion is not a sign or
209                if  a signed conversion results in no characters, a space will
210                be placed before the result. This means that if the space  and
211                + flags both appear, the space flag will be ignored.
212
213
214       #        The  value  is to be converted to an alternate form. For c, d,
215                i, s, and u conversions, the flag has no effect. For an o con‐
216                version,  it  increases  the precision (if necessary) to force
217                the first digit of the result to be a zero. For x or X conver‐
218                sion,  a non-zero result will have 0x (or 0X) prepended to it.
219                For a, A, e, E, f, F, g, and G conversions,  the  result  will
220                always contain a radix character, even if no digits follow the
221                radix  character.  Without  this  flag,  the  radix  character
222                appears  in  the  result  of these conversions only if a digit
223                follows it. For g and G conversions, trailing zeros  will  not
224                be removed from the result as they normally are.
225
226
227       0        For  d, i, o, u, x, X, a, A, e, E, f, F, g, and G conversions,
228                leading zeros (following any indication of sign or  base)  are
229                used to pad to the field width; no space padding is performed.
230                If the 0 and flags both appear, the 0 flag will be  ignored.
231                For d, i, o, u, x, and X conversions, if a precision is speci‐
232                fied, the 0 flag will be ignored. If the 0 and  '  flags  both
233                appear,  the grouping characters are inserted before zero pad‐
234                ding. For other conversions, the behavior is undefined.
235
236
237   Length Modifiers
238       The length modifiers and their meanings are:
239
240       hh              Specifies that a following d, i, o, u, x, or X  conver‐
241                       sion  specifier  applies  to  a signed char or unsigned
242                       char argument (the argument  will  have  been  promoted
243                       according to the integer promotions, but its value will
244                       be converted to signed char  or  unsigned  char  before
245                       printing);  or  that a following n conversion specifier
246                       applies to a pointer to a signed char argument.
247
248
249       h               Specifies that a following d, i, o, u, x, or X  conver‐
250                       sion  specifier  applies  to  a short or unsigned short
251                       argument (the argument will have been promoted  accord‐
252                       ing  to  the  integer promotions, but its value will be
253                       converted to short or unsigned short before  printing);
254                       or that a following n conversion specifier applies to a
255                       pointer to a short argument.
256
257
258       l (ell)         Specifies that a following d, i, o, u, x, or X  conver‐
259                       sion specifier applies to a long or unsigned long argu‐
260                       ment; that a following n conversion  specifier  applies
261                       to  a  pointer  to  a long argument; that a following c
262                       conversion specifier applies to a wint_t argument; that
263                       a following s conversion specifier applies to a pointer
264                       to a wchar_t argument; or has no effect on a  following
265                       a, A, e, E, f, F, g, or G conversion specifier.
266
267
268       ll (ell-ell)    Specifies  that a following d, i, o, u, x, or X conver‐
269                       sion specifier applies to a long long or unsigned  long
270                       long  argument; or that a following n conversion speci‐
271                       fier applies to a pointer to a long long argument.
272
273
274       j               Specifies that a following d, i, o, u, x, or X  conver‐
275                       sion  specifier  applies  to  an  intmax_t or uintmax_t
276                       argument; or that a following  n  conversion  specifier
277                       applies  to  a  pointer  to  an  intmax_t argument. See
278                       NOTES.
279
280
281       z               Specifies that a following d, i, o, u, x, or X  conver‐
282                       sion specifier applies to a size_t or the corresponding
283                       signed integer type argument; or  that  a  following  n
284                       conversion  specifier  applies to a pointer to a signed
285                       integer type corresponding to size_t argument.
286
287
288       t               Specifies that a following d, i, o, u, x, or X  conver‐
289                       sion  specifier  applies  to  a ptrdiff_t or the corre‐
290                       sponding unsigned type argument; or that a following  n
291                       conversion   specifier   applies  to  a  pointer  to  a
292                       ptrdiff_t argument.
293
294
295       L               Specifies that a following a, A, e, E, f, F,  g,  or  G
296                       conversion specifier applies to a long double argument.
297
298
299
300       If  a  length modifier appears with any conversion specifier other than
301       as specified above, the behavior is undefined.
302
303   Conversion Specifiers
304       Each conversion specifier results in fetching zero or  more  arguments.
305       The  results  are undefined if there are insufficient arguments for the
306       format. If the format is exhausted while arguments remain,  the  excess
307       arguments are ignored.
308
309
310       The conversion specifiers and their meanings are:
311
312       d, i    The  int argument is converted to a signed decimal in the style
313               []dddd. The precision specifies the minimum number  of  digits
314               to  appear;  if the value being converted can be represented in
315               fewer digits, it will  be  expanded  with  leading  zeros.  The
316               default  precision  is  1.  The  result of converting 0 with an
317               explicit precision of 0 is no characters.
318
319
320       o       The unsigned int argument is converted to unsigned octal format
321               in  the  style dddd. The precision specifies the minimum number
322               of digits to appear; if the value being converted can be repre‐
323               sented in fewer digits, it will be expanded with leading zeros.
324               The default precision is 1. The result of converting 0 with  an
325               explicit precision of 0 is no characters.
326
327
328       u       The unsigned int argument is converted to unsigned decimal for‐
329               mat in the style dddd. The precision specifies the minimum num‐
330               ber  of  digits  to appear; if the value being converted can be
331               represented in fewer digits, it will be expanded  with  leading
332               zeros.  The  default precision is 1. The result of converting 0
333               with an explicit precision of 0 is no characters.
334
335
336       x       The unsigned int argument is converted to unsigned  hexadecimal
337               format in the style dddd; the letters abcdef are used. The pre‐
338               cision specifies the minimum number of digits to appear; if the
339               value  being  converted  can be represented in fewer digits, it
340               will be expanded with leading zeros. The default  precision  is
341               1.  The  result of converting 0 with an explicit precision of 0
342               is no characters.
343
344
345       X       Behaves the same as the x conversion specifier except that let‐
346               ters ABCDEF are used instead of abcdef.
347
348
349       f, F    The  double  argument  is  converted to decimal notation in the
350               style []ddd.ddd, where the number of digits  after  the  radix
351               character  (see setlocale(3C)) is equal to the precision speci‐
352               fication. If the precision is missing it is taken as 6; if  the
353               precision  is  explicitly 0 and the # flag is not specified, no
354               radix character appears. If a radix character appears, at least
355               1  digit  appears  before it. The converted value is rounded to
356               fit the specified output format  according  to  the  prevailing
357               floating  point  rounding  direction mode. If the conversion is
358               not exact, an inexact exception is raised.
359
360               For the f specifier, a double argument representing an infinity
361               or NaN is converted in the style of the e conversion specifier,
362               except that for an infinite argument, "infinity" or  "Infinity"
363               is  printed when the precision is at least 8 and "inf" or "Inf"
364               is printed otherwise.
365
366               For the F specifier, a double argument representing an infinity
367               or  NaN  is  converted  in  the SUSv3 style of the E conversion
368               specifier, except that for an infinite argument, "INFINITY"  is
369               printed  when  the  precision  is  at  least  8 and or "INF" is
370               printed otherwise.
371
372
373       e, E    The double argument is converted  to  the  style  []d.ddde±dd,
374               where  there  is one digit before the radix character (which is
375               non-zero if the argument is non-zero) and the number of  digits
376               after it is equal to the precision. When the precision is miss‐
377               ing it is taken as 6; if the precision is 0 and the #  flag  is
378               not  specified,  no  radix  character appears. The E conversion
379               specifier will produce a number with E instead of e introducing
380               the exponent. The exponent always contains at least two digits.
381               The converted value is rounded to fit the specified output for‐
382               mat  according to the prevailing floating point rounding direc‐
383               tion mode. If the conversion is not exact, an inexact exception
384               is raised.
385
386               Infinity  and  NaN  values  are handled in one of the following
387               ways:
388
389               SUSv3      For the e specifier, a double argument  representing
390                          an  infinity  is  printed as "[−]infinity", when the
391                          precision for the conversion is at least  7  and  as
392                          "[−]inf" otherwise. A double argument representing a
393                          NaN is printed as "[−]nan".  For  the  E  specifier,
394                          "INF",  "INFINITY", and "NAN" are printed instead of
395                          "inf", "infinity", and "nan", respectively. Printing
396                          of the sign follows the rules described above.
397
398
399               Default    A   double  argument  representing  an  infinity  is
400                          printed as "[−]Infinity", when the precision for the
401                          conversion  is at least 7 and as "[−]Inf" otherwise.
402                          A double argument representing a NaN is  printed  as
403                          "[−]NaN".  Printing  of  the  sign follows the rules
404                          described above.
405
406
407
408       g, G    The double argument is printed in style f or e (or in  style  E
409               in  the  case  of a G conversion specifier), with the precision
410               specifying the number of significant  digits.  If  an  explicit
411               precision is 0, it is taken as 1. The style used depends on the
412               value converted: style e (or E) will be used only if the  expo‐
413               nent  resulting  from the conversion is less than -4 or greater
414               than or equal to the precision. Trailing zeros are removed from
415               the  fractional  part  of the result. A radix character appears
416               only if it is followed by a digit.
417
418               A double argument representing an infinity or NaN is  converted
419               in  the  style  of the e or E conversion specifier, except that
420               for an infinite argument, "infinity", "INFINITY", or "Infinity"
421               is  printed  when the precision is at least 8 and "inf", "INF",
422               or "Inf" is printed otherwise.
423
424
425       a, A    A double argument representing a floating-point number is  con‐
426               verted in the style "[-]0xh.hhhhp±d", where the single hexadec‐
427               imal digit preceding the radix point is 0  if  the  value  con‐
428               verted  is  zero  and 1 otherwise and the number of hexadecimal
429               digits after it is equal to the precision; if the precision  is
430               missing,  the number of digits printed after the radix point is
431               13 for the conversion of a double value, 16 for the  conversion
432               of  a  long double value on x86, and 28 for the conversion of a
433               long double value on SPARC; if the precision is  zero  and  the
434               '#'  flag  is  not  specified,  no decimal-point character will
435               appear. The letters "abcdef" are used for a conversion and  the
436               letters  "ABCDEF"  for A conversion. The A conversion specifier
437               produces a number with 'X' and 'P' instead of 'x' and 'p'.  The
438               exponent  will  always  contain at least one digit, and only as
439               many more digits as necessary to represent the decimal exponent
440               of 2. If the value is zero, the exponent is zero.
441
442               The converted value is rounded to fit the specified output for‐
443               mat according to the prevailing floating point rounding  direc‐
444               tion mode. If the conversion is not exact, an inexact exception
445               is raised.
446
447               A double argument representing an infinity or NaN is  converted
448               in the SUSv3 style of an e or E conversion specifier.
449
450
451       c       The  int  argument  is  converted  to an unsigned char, and the
452               resulting byte is printed.
453
454               If an l (ell) qualifier is present, the wint_t argument is con‐
455               verted  as  if by an ls conversion specification with no preci‐
456               sion and an argument that points to a two-element array of type
457               wchar_t,  the  first element of which contains the wint_t argu‐
458               ment to the ls conversion specification and the second  element
459               contains a null wide-character.
460
461
462       C       Same as lc.
463
464
465       wc      The  int  argument  is converted to a wide character (wchar_t),
466               and the resulting wide character is printed.
467
468
469       s       The argument must be a pointer to an array of char. Bytes  from
470               the array are written up to (but not including) any terminating
471               null byte. If a precision is specified,  a  standard-conforming
472               application  (see  standards(5))  will write only the number of
473               bytes specified by precision; an application that is not  stan‐
474               dard-conforming  will write only the portion of the string that
475               will display in the number of columns of screen display  speci‐
476               fied  by  precision.  If  the precision is not specified, it is
477               taken to be infinite, so all bytes up to the  first  null  byte
478               are printed. An argument with a null value will yield undefined
479               results.
480
481               If an l (ell) qualifier is present,  the  argument  must  be  a
482               pointer  to  an array of type wchar_t. Wide-characters from the
483               array are converted to characters (each as if by a call to  the
484               wcrtomb(3C) function, with the conversion state described by an
485               mbstate_t object initialized to zero  before  the  first  wide-
486               character  is converted) up to and including a terminating null
487               wide-character. The resulting characters are written up to (but
488               not  including)  the  terminating  null character (byte). If no
489               precision is specified, the array must  contain  a  null  wide-
490               character.  If a precision is specified, no more than that many
491               characters (bytes) are written (including shift  sequences,  if
492               any),  and  the array must contain a null wide-character if, to
493               equal the character sequence length given by the precision, the
494               function would need to access a wide-character one past the end
495               of the array. In no case is a partial character written.
496
497
498       S       Same as ls.
499
500
501       ws      The argument must be a pointer to an array  of  wchar_t.  Bytes
502               from the array are written up to (but not including) any termi‐
503               nating null character. If the precision is specified, only that
504               portion  of  the  wide-character array that will display in the
505               number of columns of screen display specified by precision will
506               be  written.  If the precision is not specified, it is taken to
507               be infinite, so all wide characters up to the first null  char‐
508               acter  are  printed.  An  argument with a null value will yield
509               undefined results.
510
511
512       p       The argument must be a  pointer  to  void.  The  value  of  the
513               pointer is converted to a set of sequences of printable charac‐
514               ters, which should be the same as the set of sequences that are
515               matched by the %p conversion of the scanf(3C) function.
516
517
518       n       The  argument  must  be  a  pointer to an integer into which is
519               written the number of bytes written to the output standard  I/O
520               stream so far by this call to one of the printf() functions. No
521               argument is converted.
522
523
524       %       Print a %; no argument  is  converted.  The  entire  conversion
525               specification must be %%.
526
527
528
529       If  a  conversion  specification does not match one of the above forms,
530       the behavior is undefined.
531
532
533       In no case does a non-existent or small field width cause truncation of
534       a  field;  if the result of a conversion is wider than the field width,
535       the field is simply expanded to contain the conversion result.  Charac‐
536       ters generated by printf() and fprintf() are printed as if the putc(3C)
537       function had been called.
538
539
540       The st_ctime and st_mtime fields of the file will be marked for  update
541       between the call to a successful execution of printf() or fprintf() and
542       the next successful completion of a call to fflush(3C) or fclose(3C) on
543       the same stream or a call to exit(3C) or abort(3C).
544

RETURN VALUES

546       The printf(), fprintf(), sprintf(), and asprintf() functions return the
547       number of bytes transmitted (excluding the terminating null byte in the
548       case of sprintf() and asprintf()).
549
550
551       The  snprintf()  function  returns  the number of bytes that would have
552       been written to s if n had been sufficiently large (excluding the  ter‐
553       minating  null byte.) If the value of n is 0 on a call to snprintf(), s
554       can be a null pointer and the number of  bytes  that  would  have  been
555       written  if  n  had  been sufficiently large (excluding the terminating
556       null byte) is returned.
557
558
559       Each function returns a negative value if an output error  was  encoun‐
560       tered.
561

ERRORS

563       For the conditions under which printf() and fprintf() will fail and may
564       fail, refer to fputc(3C) or fputwc(3C).
565
566
567       The snprintf() function will fail if:
568
569       EOVERFLOW    The value of n is greater than INT_MAX or  the  number  of
570                    bytes  needed to hold the output excluding the terminating
571                    null is greater than INT_MAX.
572
573
574
575       The printf(), fprintf(), sprintf(), and snprintf() functions  may  fail
576       if:
577
578       EILSEQ    A  wide-character  code  that  does not correspond to a valid
579                 character has been detected.
580
581
582       EINVAL    There are insufficient arguments.
583
584
585
586       The printf(), fprintf(), and asprintf() functions may fail  due  to  an
587       underlying malloc(3C) failure if:
588
589       EAGAIN    Storage space is temporarily unavailable.
590
591
592       ENOMEM    Insufficient storage space is available.
593
594

USAGE

596       If  the  application  calling the printf() functions has any objects of
597       type wint_t or wchar_t, it must also include the  header  <wchar.h>  to
598       have these objects defined.
599
600   Escape Character Sequences
601       It  is  common  to  use the following escape sequences built into the C
602       language when entering format strings for the printf()  functions,  but
603       these  sequences  are  processed by the C compiler, not by the printf()
604       function.
605
606       \a     Alert. Ring the bell.
607
608
609       \b     Backspace. Move the printing position to  one  character  before
610              the  current  position, unless the current position is the start
611              of a line.
612
613
614       \f     Form feed. Move the printing position to  the  initial  printing
615              position of the next logical page.
616
617
618       \n     Newline.  Move  the  printing  position to the start of the next
619              line.
620
621
622       \r     Carriage return. Move the printing position to the start of  the
623              current line.
624
625
626       \t     Horizontal tab. Move the printing position to the next implemen‐
627              tation-defined horizontal tab position on the current line.
628
629
630       \v     Vertical tab. Move the printing position to  the  start  of  the
631              next implementation-defined vertical tab position.
632
633
634
635       In addition, the C language supports character sequences of the form
636
637
638       \octal-number
639
640
641       and
642
643
644       \hex-number
645
646
647       which  translates  into the character represented by the octal or hexa‐
648       decimal number. For example, if ASCII representations are  being  used,
649       the  letter 'a' may be written as '\141' and 'Z' as '\132'. This syntax
650       is most frequently used to represent the null character as  '\0'.  This
651       is  exactly  equivalent to the numeric constant zero (0). Note that the
652       octal number does not include the zero prefix as it would for a  normal
653       octal  constant. To specify a hexadecimal number, omit the zero so that
654       the prefix is an 'x' (uppercase 'X' is not allowed  in  this  context).
655       Support  for  hexadecimal  sequences  is  an  ANSI extension. See stan‐
656       dards(5).
657

EXAMPLES

659       Example 1 To print the language-independent date and time  format,  the
660       following statement could be used:
661
662         printf (format, weekday, month, day, hour, min);
663
664
665
666       For American usage, format could be a pointer to the string:
667
668
669         "%s, %s %d, %d:%.2d\n"
670
671
672
673       producing the message:
674
675
676         Sunday, July 3, 10:02
677
678
679
680       whereas for German usage, format could be a pointer to the string:
681
682
683         "%1$s, %3$d. %2$s, %4$d:%5$.2d\n"
684
685
686
687       producing the message:
688
689
690         Sonntag, 3. Juli, 10:02
691
692
693       Example  2  To print a date and time in the form Sunday, July 3, 10:02,
694       where weekday and month are pointers to null-terminated strings:
695
696         printf("%s, %s %i, %d:%.2d", weekday, month, day, hour, min);
697
698
699       Example 3 To print pi to 5 decimal places:
700
701         printf("pi = %.5f", 4 * atan(1.0));
702
703
704   Default
705       Example 4 The following example applies only to applications  that  are
706       not  standard-conforming. To print a list of names in columns which are
707       20 characters wide:
708
709         printf("%20s%20s%20s", lastname, firstname, middlename);
710
711

ATTRIBUTES

713       See attributes(5) for descriptions of the following attributes:
714
715
716
717
718       ┌─────────────────────────────┬─────────────────────────────┐
719       │ATTRIBUTE TYPE               │ATTRIBUTE VALUE              │
720       ├─────────────────────────────┼─────────────────────────────┤
721       │CSI                          │Enabled                      │
722       ├─────────────────────────────┼─────────────────────────────┤
723       │Interface Stability          │Committed                    │
724       ├─────────────────────────────┼─────────────────────────────┤
725       │MT-Level                     │See below.                   │
726       ├─────────────────────────────┼─────────────────────────────┤
727       │Standard                     │See below.                   │
728       └─────────────────────────────┴─────────────────────────────┘
729
730
731       All of these functions can be used  safely  in  multithreaded  applica‐
732       tions,  as  long  as  setlocale(3C)  is  not being called to change the
733       locale. The sprintf() and snprintf() functions are Async-Signal-Safe.
734
735
736       See standards(5) for the standards conformance of printf(),  fprintf(),
737       sprintf(),  and  snprintf().  The asprintf() function is modeled on the
738       one that appears in the FreeBSD, NetBSD, and GNU C libraries.
739

SEE ALSO

741       exit(2), lseek(2), write(2), abort(3C), ecvt(3C), exit(3C), fclose(3C),
742       fflush(3C), fputwc(3C), free(3C), malloc(3C), putc(3C), scanf(3C), set‐
743       locale(3C),   stdio(3C),   vprintf(3C),    wcstombs(3C),    wctomb(3C),
744       attributes(5), environ(5), standards(5)
745

NOTES

747       If  the  j  length modifier is used, 32-bit applications that were com‐
748       piled using c89 on releases prior to Solaris 10 will  experience  unde‐
749       fined behavior.
750
751
752       The  snprintf()  return  value when n = 0 was changed in the Solaris 10
753       release. The change was based on the SUSv3 specification. The  previous
754       behavior was based on the initial SUSv2 specification, where snprintf()
755       when n = 0 returns an unspecified value less than 1.
756
757
758
759SunOS 5.11                        7 Jan 2009                        printf(3C)
Impressum