1.\" Copyright (c) 1990, 1991, 1993 2.\" The Regents of the University of California. All rights reserved. 3.\" 4.\" This code is derived from software contributed to Berkeley by 5.\" Chris Torek and the American National Standards Committee X3, 6.\" on Information Processing Systems. 7.\" 8.\" Redistribution and use in source and binary forms, with or without 9.\" modification, are permitted provided that the following conditions 10.\" are met: 11.\" 1. Redistributions of source code must retain the above copyright 12.\" notice, this list of conditions and the following disclaimer. 13.\" 2. Redistributions in binary form must reproduce the above copyright 14.\" notice, this list of conditions and the following disclaimer in the 15.\" documentation and/or other materials provided with the distribution. 16.\" 3. All advertising materials mentioning features or use of this software 17.\" must display the following acknowledgement: 18.\" This product includes software developed by the University of 19.\" California, Berkeley and its contributors. 20.\" 4. Neither the name of the University nor the names of its contributors 21.\" may be used to endorse or promote products derived from this software 22.\" without specific prior written permission. 23.\" 24.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34.\" SUCH DAMAGE. 35.\" 36.\" @(#)printf.3 8.1 (Berkeley) 6/4/93 37.\" 38.Dd June 4, 1993 39.Dt PRINTF 3 40.Os 41.Sh NAME 42.Nm printf , 43.Nm fprintf , 44.Nm sprintf , 45.Nm snprintf , 46.Nm asprintf , 47.Nm vprintf , 48.Nm vfprintf, 49.Nm vsprintf , 50.Nm vsnprintf , 51.Nm vasprintf 52.Nd formatted output conversion 53.Sh SYNOPSIS 54.Fd #include <stdio.h> 55.Ft int 56.Fn printf "const char *format" ... 57.Ft int 58.Fn fprintf "FILE *stream" "const char *format" ... 59.Ft int 60.Fn sprintf "char *str" "const char *format" ... 61.Ft int 62.Fn snprintf "char *str" "size_t size" "const char *format" ... 63.Ft int 64.Fn asprintf "char **ret" "const char *format" ... 65.Fd #include <stdarg.h> 66.Ft int 67.Fn vprintf "const char *format" "va_list ap" 68.Ft int 69.Fn vfprintf "FILE *stream" "const char *format" "va_list ap" 70.Ft int 71.Fn vsprintf "char *str" "const char *format" "va_list ap" 72.Ft int 73.Fn vsnprintf "char *str" "size_t size" "const char *format" "va_list ap" 74.Ft int 75.Fn vasprintf "char **ret" "const char *format" "va_list ap" 76.Sh DESCRIPTION 77The 78.Fn printf 79family of functions produces output according to a 80.Fa format 81as described below. 82.Fn Printf 83and 84.Fn vprintf 85write output to 86.Em stdout, 87the standard output stream; 88.Fn fprintf 89and 90.Fn vfprintf 91write output to the given output 92.Fa stream ; 93.Fn sprintf , 94.Fn snprintf , 95.Fn vsprintf , 96and 97.Fn vsnprintf 98write to the character string 99.Fa str ; 100and 101.Fn asprintf 102and 103.Fn vasprintf 104dynamically allocate a new string with 105.Xr malloc 3 106/ 107.Xr realloc 3 . 108.Pp 109These functions write the output under the control of a 110.Fa format 111string that specifies how subsequent arguments 112(or arguments accessed via the variable-length argument facilities of 113.Xr stdarg 3 ) 114are converted for output. 115.Pp 116These functions return 117the number of characters printed 118(not including the trailing 119.Ql \e0 120used to end output to strings). 121.Pp 122.Fn Asprintf 123and 124.Fn vasprintf 125return a pointer to a buffer sufficiently large to hold the 126string in the 127.Fa ret 128argument; 129This pointer should be passed to 130.Xr free 3 131to release the allocated storage when it is no longer needed. 132If sufficient space cannot be allocated, 133.Fn asprintf 134and 135.Fn vasprintf 136will return -1 and set 137.Fa ret 138to be a NULL pointer. 139.Pp 140.Fn Snprintf 141and 142.Fn vsnprintf 143will write at most 144.Fa size Ns \-1 145of the characters printed into the output string 146(the 147.Fa size Ns 'th 148character then gets the terminating 149.Ql \e0 ) ; 150if the return value is greater than or equal to the 151.Fa size 152argument, the string was too short 153and some of the printed characters were discarded. 154.Pp 155.Fn Sprintf 156and 157.Fn vsprintf 158effectively assume an infinite 159.Fa size . 160.Pp 161The format string is composed of zero or more directives: 162ordinary 163.\" multibyte 164characters (not 165.Cm % ) , 166which are copied unchanged to the output stream; 167and conversion specifications, each of which results 168in fetching zero or more subsequent arguments. 169Each conversion specification is introduced by 170the character 171.Cm % . 172The arguments must correspond properly (after type promotion) 173with the conversion specifier. 174After the 175.Cm % , 176the following appear in sequence: 177.Bl -bullet 178.It 179An optional field, consisting of a decimal digit string followed by a 180.Cm $ , 181specifying the next argument to access . 182If this field is not provided, the argument following the last 183argument accessed will be used. 184Arguments are numbered starting at 185.Cm 1 . 186If unaccessed arguments in the format string are interspersed with ones that 187are accessed the results will be indeterminate. 188.It 189Zero or more of the following flags: 190.Bl -hyphen 191.It 192A 193.Cm # 194character 195specifying that the value should be converted to an ``alternate form''. 196For 197.Cm c , 198.Cm d , 199.Cm i , 200.Cm n , 201.Cm p , 202.Cm s , 203and 204.Cm u , 205conversions, this option has no effect. 206For 207.Cm o 208conversions, the precision of the number is increased to force the first 209character of the output string to a zero (except if a zero value is printed 210with an explicit precision of zero). 211For 212.Cm x 213and 214.Cm X 215conversions, a non-zero result has the string 216.Ql 0x 217(or 218.Ql 0X 219for 220.Cm X 221conversions) prepended to it. 222For 223.Cm e , 224.Cm E , 225.Cm f , 226.Cm g , 227and 228.Cm G , 229conversions, the result will always contain a decimal point, even if no 230digits follow it (normally, a decimal point appears in the results of 231those conversions only if a digit follows). 232For 233.Cm g 234and 235.Cm G 236conversions, trailing zeros are not removed from the result as they 237would otherwise be. 238.It 239A zero 240.Sq Cm \&0 241character specifying zero padding. 242For all conversions except 243.Cm n , 244the converted value is padded on the left with zeros rather than blanks. 245If a precision is given with a numeric conversion 246.Pf ( Cm d , 247.Cm i , 248.Cm o , 249.Cm u , 250.Cm i , 251.Cm x , 252and 253.Cm X ) , 254the 255.Sq Cm \&0 256flag is ignored. 257.It 258A negative field width flag 259.Sq Cm \- 260indicates the converted value is to be left adjusted on the field boundary. 261Except for 262.Cm n 263conversions, the converted value is padded on the right with blanks, 264rather than on the left with blanks or zeros. 265A 266.Sq Cm \- 267overrides a 268.Sq Cm \&0 269if both are given. 270.It 271A space, specifying that a blank should be left before a positive number 272produced by a signed conversion 273.Pf ( Cm d , 274.Cm e , 275.Cm E , 276.Cm f , 277.Cm g , 278.Cm G , 279or 280.Cm i ) . 281.It 282A 283.Sq Cm + 284character specifying that a sign always be placed before a 285number produced by a signed conversion. 286A 287.Sq Cm + 288overrides a space if both are used. 289.El 290.It 291An optional decimal digit string specifying a minimum field width. 292If the converted value has fewer characters than the field width, it will 293be padded with spaces on the left (or right, if the left-adjustment 294flag has been given) to fill out 295the field width. 296.It 297An optional precision, in the form of a period 298.Sq Cm \&. 299followed by an 300optional digit string. If the digit string is omitted, the precision 301is taken as zero. This gives the minimum number of digits to appear for 302.Cm d , 303.Cm i , 304.Cm o , 305.Cm u , 306.Cm x , 307and 308.Cm X 309conversions, the number of digits to appear after the decimal-point for 310.Cm e , 311.Cm E , 312and 313.Cm f 314conversions, the maximum number of significant digits for 315.Cm g 316and 317.Cm G 318conversions, or the maximum number of characters to be printed from a 319string for 320.Cm s 321conversions. 322.It 323The optional character 324.Cm h , 325specifying that a following 326.Cm d , 327.Cm i , 328.Cm o , 329.Cm u , 330.Cm x , 331or 332.Cm X 333conversion corresponds to a 334.Em short int 335or 336.Em unsigned short int 337argument, or that a following 338.Cm n 339conversion corresponds to a pointer to a 340.Em short int 341argument. 342.It 343The optional character 344.Cm l 345(ell) specifying that a following 346.Cm d , 347.Cm i , 348.Cm o , 349.Cm u , 350.Cm x , 351or 352.Cm X 353conversion applies to a pointer to a 354.Em long int 355or 356.Em unsigned long int 357argument, or that a following 358.Cm n 359conversion corresponds to a pointer to a 360.Em long int 361argument. 362.It 363The optional character 364.Cm q , 365specifying that a following 366.Cm d , 367.Cm i , 368.Cm o , 369.Cm u , 370.Cm x , 371or 372.Cm X 373conversion corresponds to a 374.Em quad int 375or 376.Em unsigned quad int 377argument, or that a following 378.Cm n 379conversion corresponds to a pointer to a 380.Em quad int 381argument. 382.It 383The character 384.Cm L 385specifying that a following 386.Cm e , 387.Cm E , 388.Cm f , 389.Cm g , 390or 391.Cm G 392conversion corresponds to a 393.Em long double 394argument (but note that long double values are not currently supported 395by the 396.Tn VAX 397and 398.Tn Tahoe 399compilers). 400.It 401A character that specifies the type of conversion to be applied. 402.El 403.Pp 404A field width or precision, or both, may be indicated by 405an asterisk 406.Ql * 407or an asterisk followed by one or more decimal digits and a 408.Ql $ 409instead of a 410digit string. 411In this case, an 412.Em int 413argument supplies the field width or precision. 414A negative field width is treated as a left adjustment flag followed by a 415positive field width; a negative precision is treated as though it were 416missing. 417If a single format directive mixes positional (nn$) 418and non-positional arguments, the results are undefined. 419.Pp 420The conversion specifiers and their meanings are: 421.Bl -tag -width "diouxX" 422.It Cm diouxX 423The 424.Em int 425(or appropriate variant) argument is converted to signed decimal 426.Pf ( Cm d 427and 428.Cm i ) , 429unsigned octal 430.Pq Cm o , 431unsigned decimal 432.Pq Cm u , 433or unsigned hexadecimal 434.Pf ( Cm x 435and 436.Cm X ) 437notation. The letters 438.Cm abcdef 439are used for 440.Cm x 441conversions; the letters 442.Cm ABCDEF 443are used for 444.Cm X 445conversions. 446The precision, if any, gives the minimum number of digits that must 447appear; if the converted value requires fewer digits, it is padded on 448the left with zeros. 449.It Cm DOU 450The 451.Em long int 452argument is converted to signed decimal, unsigned octal, or unsigned 453decimal, as if the format had been 454.Cm ld , 455.Cm lo , 456or 457.Cm lu 458respectively. 459These conversion characters are deprecated, and will eventually disappear. 460.It Cm eE 461The 462.Em double 463argument is rounded and converted in the style 464.Sm off 465.Pf [\-]d Cm \&. No ddd Cm e No \\*(Pmdd 466.Sm on 467where there is one digit before the 468decimal-point character 469and the number of digits after it is equal to the precision; 470if the precision is missing, 471it is taken as 6; if the precision is 472zero, no decimal-point character appears. 473An 474.Cm E 475conversion uses the letter 476.Cm E 477(rather than 478.Cm e ) 479to introduce the exponent. 480The exponent always contains at least two digits; if the value is zero, 481the exponent is 00. 482.It Cm f 483The 484.Em double 485argument is rounded and converted to decimal notation in the style 486.Sm off 487.Pf [-]ddd Cm \&. No ddd , 488.Sm on 489where the number of digits after the decimal-point character 490is equal to the precision specification. 491If the precision is missing, it is taken as 6; if the precision is 492explicitly zero, no decimal-point character appears. 493If a decimal point appears, at least one digit appears before it. 494.It Cm g 495The 496.Em double 497argument is converted in style 498.Cm f 499or 500.Cm e 501(or 502.Cm E 503for 504.Cm G 505conversions). 506The precision specifies the number of significant digits. 507If the precision is missing, 6 digits are given; if the precision is zero, 508it is treated as 1. 509Style 510.Cm e 511is used if the exponent from its conversion is less than -4 or greater than 512or equal to the precision. 513Trailing zeros are removed from the fractional part of the result; a 514decimal point appears only if it is followed by at least one digit. 515.It Cm c 516The 517.Em int 518argument is converted to an 519.Em unsigned char , 520and the resulting character is written. 521.It Cm s 522The 523.Dq Em char * 524argument is expected to be a pointer to an array of character type (pointer 525to a string). 526Characters from the array are written up to (but not including) 527a terminating 528.Dv NUL 529character; 530if a precision is specified, no more than the number specified are 531written. 532If a precision is given, no null character 533need be present; if the precision is not specified, or is greater than 534the size of the array, the array must contain a terminating 535.Dv NUL 536character. 537.It Cm p 538The 539.Dq Em void * 540pointer argument is printed in hexadecimal (as if by 541.Ql %#x 542or 543.Ql %#lx ) . 544.It Cm n 545The number of characters written so far is stored into the 546integer indicated by the 547.Dq Em int * 548(or variant) pointer argument. 549No argument is converted. 550.It Cm % 551A 552.Ql % 553is written. No argument is converted. The complete conversion specification 554is 555.Ql %% . 556.El 557.Pp 558In no case does a non-existent or small field width cause truncation of 559a field; if the result of a conversion is wider than the field width, the 560field is expanded to contain the conversion result. 561.Pp 562.Sh EXAMPLES 563.br 564To print a date and time in the form `Sunday, July 3, 10:02', 565where 566.Em weekday 567and 568.Em month 569are pointers to strings: 570.Bd -literal -offset indent 571#include <stdio.h> 572fprintf(stdout, "%s, %s %d, %.2d:%.2d\en", 573 weekday, month, day, hour, min); 574.Ed 575.Pp 576To print \*(Pi 577to five decimal places: 578.Bd -literal -offset indent 579#include <math.h> 580#include <stdio.h> 581fprintf(stdout, "pi = %.5f\en", 4 * atan(1.0)); 582.Ed 583.Pp 584To allocate a 128 byte string and print into it: 585.Bd -literal -offset indent 586#include <stdio.h> 587#include <stdlib.h> 588#include <stdarg.h> 589char *newfmt(const char *fmt, ...) 590{ 591 char *p; 592 va_list ap; 593 if ((p = malloc(128)) == NULL) 594 return (NULL); 595 va_start(ap, fmt); 596 (void) vsnprintf(p, 128, fmt, ap); 597 va_end(ap); 598 return (p); 599} 600.Ed 601.Sh SEE ALSO 602.Xr printf 1 , 603.Xr scanf 3 604.Sh STANDARDS 605The 606.Fn fprintf , 607.Fn printf , 608.Fn sprintf , 609.Fn vprintf , 610.Fn vfprintf , 611and 612.Fn vsprintf 613functions 614conform to 615.St -ansiC . 616.Sh HISTORY 617The functions 618.Fn asprintf 619and 620.Fn vasprintf 621first appeared in the GNU C library. 622These were implemented by Peter Wemm <peter@FreeBSD.org> in 623.Fx 2.2 , 624but were later replaced with a different implementation 625from Todd C. Miller <Todd.Miller@courtesan.com> for 626.Ox 2.3 . 627.Sh BUGS 628The conversion formats 629.Cm \&%D , 630.Cm \&%O , 631and 632.Cm %U 633are not standard and 634are provided only for backward compatibility. 635The effect of padding the 636.Cm %p 637format with zeros (either by the 638.Sq Cm 0 639flag or by specifying a precision), and the benign effect (i.e., none) 640of the 641.Sq Cm # 642flag on 643.Cm %n 644and 645.Cm %p 646conversions, as well as other 647nonsensical combinations such as 648.Cm %Ld , 649are not standard; such combinations 650should be avoided. 651.Pp 652Because 653.Fn sprintf 654and 655.Fn vsprintf 656assume an infinitely long string, 657callers must be careful not to overflow the actual space; 658this is often hard to assure. 659For safety, programmers should use the 660.Fn snprintf 661interface instead. 662Unfortunately, this interface is not portable. 663