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