1.\" $NetBSD: printf.3,v 1.42 2006/03/19 10:46:21 wiz Exp $ 2.\" 3.\" Copyright (c) 1990, 1991, 1993 4.\" The Regents of the University of California. All rights reserved. 5.\" 6.\" This code is derived from software contributed to Berkeley by 7.\" Chris Torek and the American National Standards Committee X3, 8.\" on Information Processing Systems. 9.\" 10.\" Redistribution and use in source and binary forms, with or without 11.\" modification, are permitted provided that the following conditions 12.\" are met: 13.\" 1. Redistributions of source code must retain the above copyright 14.\" notice, this list of conditions and the following disclaimer. 15.\" 2. Redistributions in binary form must reproduce the above copyright 16.\" notice, this list of conditions and the following disclaimer in the 17.\" documentation and/or other materials provided with the distribution. 18.\" 3. Neither the name of the University nor the names of its contributors 19.\" may be used to endorse or promote products derived from this software 20.\" without specific prior written permission. 21.\" 22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32.\" SUCH DAMAGE. 33.\" 34.\" @(#)printf.3 8.1 (Berkeley) 6/4/93 35.\" 36.Dd March 17, 2006 37.Dt PRINTF 3 38.Os 39.Sh NAME 40.Nm printf , 41.Nm fprintf , 42.Nm sprintf , 43.Nm snprintf , 44.Nm asprintf , 45.Nm vprintf , 46.Nm vfprintf , 47.Nm vsprintf , 48.Nm vsnprintf , 49.Nm vasprintf 50.Nd formatted output conversion 51.Sh LIBRARY 52.Lb libc 53.Sh SYNOPSIS 54.In stdio.h 55.Ft int 56.Fn printf "const char * restrict format" ... 57.Ft int 58.Fn fprintf "FILE * restrict stream" "const char * restrict format" ... 59.Ft int 60.Fn sprintf "char * restrict str" "const char * restrict format" ... 61.Ft int 62.Fn snprintf "char * restrict str" "size_t size" "const char * restrict format" ... 63.Ft int 64.Fn asprintf "char ** restrict ret" "const char * restrict format" ... 65.In stdarg.h 66.Ft int 67.Fn vprintf "const char * restrict format" "va_list ap" 68.Ft int 69.Fn vfprintf "FILE * restrict stream" "const char * restrict format" "va_list ap" 70.Ft int 71.Fn vsprintf "char * restrict str" "const char * restrict format" "va_list ap" 72.Ft int 73.Fn vsnprintf "char * restrict str" "size_t size" "const char * restrict format" "va_list ap" 74.Ft int 75.Fn vasprintf "char ** restrict ret" "const char * restrict 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 ; 100.Fn asprintf 101and 102.Fn vasprintf 103write to a dynamically allocated string that is stored in 104.Fa ret . 105.Pp 106These functions write the output under the control of a 107.Fa format 108string that specifies how subsequent arguments 109(or arguments accessed via the variable-length argument facilities of 110.Xr stdarg 3 ) 111are converted for output. 112.Pp 113These functions return 114the number of characters printed 115(not including the trailing 116.Ql \e0 117used to end output to strings). 118If an output error was encountered, these functions shall return a 119negative value. 120.Pp 121.Fn asprintf 122and 123.Fn vasprintf 124return a pointer to a buffer sufficiently large to hold the 125string in the 126.Fa ret 127argument. 128This pointer should be passed to 129.Xr free 3 130to release the allocated storage when it is no longer needed. 131If sufficient space cannot be allocated, these functions 132will return \-1 and set 133.Fa ret 134to be a 135.Dv NULL 136pointer. 137Please note that these functions are not standardized, and not all 138implementations can be assumed to set the 139.Fa ret 140argument to 141.Dv NULL 142on error. 143It is more portable to check for a return value of \-1 instead. 144.Pp 145.Fn snprintf 146and 147.Fn vsnprintf 148will write at most 149.Fa size Ns \-1 150of the characters printed into the output string 151(the 152.Fa size Ns 'th 153character then gets the terminating 154.Ql \e0 ) ; 155if the return value is greater than or equal to the 156.Fa size 157argument, the string was too short 158and some of the printed characters were discarded. 159If 160.Fa size 161is zero, nothing is written and 162.Fa str 163may be a 164.Dv NULL 165pointer. 166.Pp 167.Fn sprintf 168and 169.Fn vsprintf 170effectively assume an infinite 171.Fa size . 172.Pp 173The format string is composed of zero or more directives: 174ordinary 175.\" multibyte 176characters (not 177.Cm % ) , 178which are copied unchanged to the output stream; 179and conversion specifications, each of which results 180in fetching zero or more subsequent arguments. 181Each conversion specification is introduced by 182the character 183.Cm % . 184The arguments must correspond properly (after type promotion) 185with the conversion specifier. 186After the 187.Cm % , 188the following appear in sequence: 189.Bl -bullet 190.It 191Zero or more of the following flags: 192.Bl -hyphen 193.It 194A 195.Cm # 196character 197specifying that the value should be converted to an ``alternative form''. 198For 199.Cm c , 200.Cm d , 201.Cm i , 202.Cm n , 203.Cm p , 204.Cm s , 205and 206.Cm u 207conversions, this option has no effect. 208For 209.Cm o 210conversions, the precision of the number is increased to force the first 211character of the output string to a zero (except if a zero value is printed 212with an explicit precision of zero). 213For 214.Cm x 215and 216.Cm X 217conversions, a non-zero result has the string 218.Ql 0x 219(or 220.Ql 0X 221for 222.Cm X 223conversions) prepended to it. 224For 225.Cm e , 226.Cm E , 227.Cm f , 228.Cm F , 229.Cm g , 230and 231.Cm G 232conversions, the result will always contain a decimal point, even if no 233digits follow it (normally, a decimal point appears in the results of 234those conversions only if a digit follows). 235For 236.Cm g 237and 238.Cm G 239conversions, trailing zeros are not removed from the result as they 240would otherwise be. 241.It 242A zero 243.Sq Cm \&0 244character specifying zero padding. 245For all conversions except 246.Cm n , 247the converted value is padded on the left with zeros rather than blanks. 248If a precision is given with a numeric conversion 249.Pf ( Cm d , 250.Cm i , 251.Cm o , 252.Cm u , 253.Cm i , 254.Cm x , 255and 256.Cm X ) , 257the 258.Sq Cm \&0 259flag is ignored. 260.It 261A negative field width flag 262.Sq Cm \- 263indicates the converted value is to be left adjusted on the field boundary. 264Except for 265.Cm n 266conversions, the converted value is padded on the right with blanks, 267rather than on the left with blanks or zeros. 268A 269.Sq Cm \- 270overrides a 271.Sq Cm \&0 272if both are given. 273.It 274A space, specifying that a blank should be left before a positive number 275produced by a signed conversion 276.Pf ( Cm d , 277.Cm e , 278.Cm E , 279.Cm f , 280.Cm F , 281.Cm g , 282.Cm G , 283or 284.Cm i ) . 285.It 286A 287.Sq Cm + 288character specifying that a sign always be placed before a 289number produced by a signed conversion. 290A 291.Sq Cm + 292overrides a space if both are used. 293.El 294.It 295An optional decimal digit string specifying a minimum field width. 296If the converted value has fewer characters than the field width, it will 297be padded with spaces on the left (or right, if the left-adjustment 298flag has been given) to fill out the field width. 299.It 300An optional precision, in the form of a period 301.Sq Cm \&. 302followed by an optional digit string. 303If the digit string is omitted, the precision is taken as zero. 304This gives the minimum number of digits to appear for 305.Cm d , 306.Cm i , 307.Cm o , 308.Cm u , 309.Cm x , 310and 311.Cm X 312conversions, the number of digits to appear after the decimal-point for 313.Cm e , 314.Cm E , 315.Cm f , 316and 317.Cm F 318conversions, the maximum number of significant digits for 319.Cm g 320and 321.Cm G 322conversions, or the maximum number of characters to be printed from a 323string for 324.Cm s 325conversions. 326.It 327The optional character 328.Cm h , 329specifying that a following 330.Cm d , 331.Cm i , 332.Cm o , 333.Cm u , 334.Cm x , 335or 336.Cm X 337conversion corresponds to a 338.Em short int 339or 340.Em unsigned short int 341argument, or that a following 342.Cm n 343conversion corresponds to a pointer to a 344.Em short int 345argument. 346.It 347The optional character 348.Cm j , 349specifying that a following 350.Cm d , 351.Cm i , 352.Cm o , 353.Cm u , 354.Cm x , 355or 356.Cm X 357conversion corresponds to an 358.Em intmax_t 359or 360.Em uintmax_t 361argument, or that a following 362.Cm n 363conversion corresponds to a pointer to a 364.Em intmax_t 365argument. 366.It 367The optional character 368.Cm l 369(ell) specifying that a following 370.Cm d , 371.Cm i , 372.Cm o , 373.Cm u , 374.Cm x , 375or 376.Cm X 377conversion corresponds to a 378.Em long int 379or 380.Em unsigned long int 381argument, or that a following 382.Cm n 383conversion corresponds to a pointer to a 384.Em long int 385argument. 386.It 387Two optional consecutive 388.Cm l 389(ell) characters, 390specifying that a following 391.Cm d , 392.Cm i , 393.Cm o , 394.Cm u , 395.Cm x , 396or 397.Cm X 398conversion corresponds to a 399.Em long long 400or 401.Em unsigned long long 402argument, or that a following 403.Cm n 404conversion corresponds to a pointer to a 405.Em long long 406argument. 407.It 408The optional character 409.Cm q , 410specifying that a following 411.Cm d , 412.Cm i , 413.Cm o , 414.Cm u , 415.Cm x , 416or 417.Cm X 418conversion corresponds to a 419.Em quad_t 420or 421.Em u_quad_t 422argument, or that a following 423.Cm n 424conversion corresponds to a pointer to a 425.Em quad_t 426argument. 427The 428.Cm q 429modifier is a BSD extension and should not be used in portable programs. 430.It 431The optional character 432.Cm t , 433specifying that a following 434.Cm d , 435.Cm i , 436.Cm o , 437.Cm u , 438.Cm x , 439or 440.Cm X 441conversion corresponds to a 442.Em ptrdiff_t 443or 444the corresponding unsigned integer type 445argument, or that a following 446.Cm n 447conversion corresponds to a pointer to a 448.Em ptrdiff_t 449argument. 450.It 451The optional character 452.Cm z , 453specifying that a following 454.Cm d , 455.Cm i , 456.Cm o , 457.Cm u , 458.Cm x , 459or 460.Cm X 461conversion corresponds to a 462.Em size_t 463or 464the corresponding signed integer type 465argument, or that a following 466.Cm n 467conversion corresponds to a pointer to a 468signed integer type corresponding to 469.Em size_t 470argument. 471.It 472The character 473.Cm L 474specifying that a following 475.Cm e , 476.Cm E , 477.Cm f , 478.Cm F , 479.Cm g , 480or 481.Cm G 482conversion corresponds to a 483.Em long double 484argument. 485.It 486A character that specifies the type of conversion to be applied. 487.El 488.Pp 489A field width or precision, or both, may be indicated by 490an asterisk 491.Ql * 492instead of a digit string. 493In this case, an 494.Em int 495argument supplies the field width or precision. 496A negative field width is treated as a left adjustment flag followed by a 497positive field width; a negative precision is treated as though it were 498missing. 499.Pp 500The conversion specifiers and their meanings are: 501.Bl -tag -width "diouxX" 502.It Cm diouxX 503The 504.Em int 505(or appropriate variant) argument is converted to signed decimal 506.Pf ( Cm d 507and 508.Cm i ) , 509unsigned octal 510.Pq Cm o , 511unsigned decimal 512.Pq Cm u , 513or unsigned hexadecimal 514.Pf ( Cm x 515and 516.Cm X ) 517notation. 518The letters 519.Cm abcdef 520are used for 521.Cm x 522conversions; the letters 523.Cm ABCDEF 524are used for 525.Cm X 526conversions. 527The precision, if any, gives the minimum number of digits that must 528appear; if the converted value requires fewer digits, it is padded on 529the left with zeros. 530.It Cm DOU 531The 532.Em long int 533argument is converted to signed decimal, unsigned octal, or unsigned 534decimal, as if the format had been 535.Cm ld , 536.Cm lo , 537or 538.Cm lu 539respectively. 540These conversion characters are deprecated, and will eventually disappear. 541.It Cm fF 542The 543.Em double 544argument is rounded and converted to decimal notation in the style 545.Sm off 546.Pf [-]ddd Cm \&. No ddd , 547.Sm on 548where the number of digits after the decimal-point character 549is equal to the precision specification. 550If the precision is missing, it is taken as 6; if the precision is 551explicitly zero, no decimal-point character appears. 552If a decimal point appears, at least one digit appears before it. 553.Pp 554If the double argument represents an infinity it is converted 555in the style 556.Pf [-] Cm inf . 557If the double argument represents a NaN it is converted 558in the style 559.Pf [-] Cm nan . 560An 561.Cm F 562conversion produces 563.Pf [-] Cm INF 564and 565.Pf [-] Cm NAN , 566respectively. 567.It Cm eE 568The 569.Em double 570argument is rounded and converted in the style 571.Sm off 572.Pf [\-]d Cm \&. No ddd Cm e No \\*(Pmdd 573.Sm on 574where there is one digit before the 575decimal-point character 576and the number of digits after it is equal to the precision; 577if the precision is missing, 578it is taken as 6; if the precision is 579zero, no decimal-point character appears. 580An 581.Cm E 582conversion uses the letter 583.Cm E 584(rather than 585.Cm e ) 586to introduce the exponent. 587The exponent always contains at least two digits; if the value is zero, 588the exponent is 00. 589.Pp 590Double arguments representing infinities or NaNs are converted in the 591same styles as in the 592.Cm f 593and 594.Cm F 595conversions. 596.It Cm gG 597The 598.Em double 599argument is converted in style 600.Cm f 601or 602.Cm e 603(or in style 604.Cm F 605or 606.Cm E 607for 608.Cm G 609conversions). 610The precision specifies the number of significant digits. 611If the precision is missing, 6 digits are given; if the precision is zero, 612it is treated as 1. 613Style 614.Cm e 615is used if the exponent from its conversion is less than -4 or greater than 616or equal to the precision. 617Trailing zeros are removed from the fractional part of the result; a 618decimal point appears only if it is followed by at least one digit. 619.Pp 620Double arguments representing infinities or NaNs are converted in the 621same styles as in the 622.Cm f 623and 624.Cm F 625conversions. 626.It Cm c 627The 628.Em int 629argument is converted to an 630.Em unsigned char , 631and the resulting character is written. 632.It Cm s 633The 634.Dq Em char * 635argument is expected to be a pointer to an array of character type (pointer 636to a string). 637Characters from the array are written up to (but not including) 638a terminating 639.Dv NUL 640character; 641if a precision is specified, no more than the number specified are 642written. 643If a precision is given, no null character 644need be present; if the precision is not specified, or is greater than 645the size of the array, the array must contain a terminating 646.Dv NUL 647character. 648.It Cm p 649The 650.Dq Em void * 651pointer argument is printed in hexadecimal (as if by 652.Ql %#x 653or 654.Ql %#lx ) . 655.It Cm n 656The number of characters written so far is stored into the 657integer indicated by the 658.Dq Em int * 659(or variant) pointer argument. 660No argument is converted. 661.It Cm % 662A 663.Ql % 664is written. 665No argument is converted. 666The complete conversion specification is 667.Ql %% . 668.El 669.Pp 670In no case does a non-existent or small field width cause truncation of 671a field; if the result of a conversion is wider than the field width, the 672field is expanded to contain the conversion result. 673.Sh RETURN VALUES 674Upon successful completion 675.Fn printf , 676.Fn fprintf , 677.Fn vprintf , 678and 679.Fn vfprintf 680return the number of characters printed. 681Otherwise \-1 is returned and 682.Dv errno 683is set to indicate the error. 684.Pp 685Upon successful completion 686.Fn sprintf 687and 688.Fn vsprintf 689return the number of characters written to 690.Fa str , 691excluding the terminating 692.Dv NUL 693character. 694Otherwise \-1 is returned and 695.Dv errno 696is set to indicate the error. 697.Pp 698Upon successful completion 699.Fn snprintf 700and 701.Fn vsnprintf 702return the number of characters that would have been written 703to a sufficiently sized 704.Fa str , 705excluding the terminating 706.Dv NUL 707character. 708.Pp 709Upon successful completion 710.Fn asprintf 711and 712.Fn vasprintf 713return the number of characters written to 714.Fa ret , 715excluding the terminating 716.Dv NUL 717character. 718Otherwise \-1 is returned, 719.Fa ret 720is set to 721.Dv NULL , 722and 723.Dv errno 724is set to indicate the error. 725.Sh EXAMPLES 726.br 727To print a date and time in the form `Sunday, July 3, 10:02', 728where 729.Em weekday 730and 731.Em month 732are pointers to strings: 733.Bd -literal -offset indent 734#include \*[Lt]stdio.h\*[Gt] 735fprintf(stdout, "%s, %s %d, %.2d:%.2d\en", 736 weekday, month, day, hour, min); 737.Ed 738.Pp 739To print \*(Pi 740to five decimal places: 741.Bd -literal -offset indent 742#include \*[Lt]math.h\*[Gt] 743#include \*[Lt]stdio.h\*[Gt] 744fprintf(stdout, "pi = %.5f\en", 4 * atan(1.0)); 745.Ed 746.Pp 747To allocate a 128 byte string and print into it: 748.Bd -literal -offset indent 749#include \*[Lt]stdio.h\*[Gt] 750#include \*[Lt]stdlib.h\*[Gt] 751#include \*[Lt]stdarg.h\*[Gt] 752char *newfmt(const char *fmt, ...) 753{ 754 char *p; 755 va_list ap; 756 if ((p = malloc(128)) == NULL) 757 return (NULL); 758 va_start(ap, fmt); 759 (void) vsnprintf(p, 128, fmt, ap); 760 va_end(ap); 761 return (p); 762} 763.Ed 764.Sh SEE ALSO 765.Xr printf 1 , 766.Xr scanf 3 , 767.Xr printf 9 768.Sh STANDARDS 769The 770.Fn fprintf , 771.Fn printf , 772.Fn sprintf , 773.Fn vprintf , 774.Fn vfprintf , 775and 776.Fn vsprintf 777functions 778conform to 779.St -isoC-90 . 780The conversion format modifiers 781.Cm %j , 782.Cm %t 783and 784.Cm %z 785conform to 786.St -isoC-99 . 787The 788.Fn snprintf 789and 790.Fn vsnprintf 791functions 792conform to 793.St -isoC-99 . 794.Sh HISTORY 795The functions 796.Fn snprintf 797and 798.Fn vsnprintf 799first appeared in 800.Bx 4.4 . 801The functions 802.Fn asprintf 803and 804.Fn vasprintf 805are modeled on the ones that first appeared in the GNU C library. 806.Sh CAVEATS 807Because 808.Fn sprintf 809and 810.Fn vsprintf 811assume an infinitely long string, callers must be careful not to 812overflow the actual space; this is often impossible to assure. 813For safety, programmers should use the 814.Fn snprintf 815and 816.Fn asprintf 817family of interfaces instead. 818Unfortunately, the 819.Fn snprintf 820interfaces are not available on older 821systems and the 822.Fn asprintf 823interfaces are not yet portable. 824.Pp 825It is important never to pass a string with user-supplied data as a 826format without using 827.Ql %s . 828An attacker can put format specifiers in the string to mangle your stack, 829leading to a possible security hole. 830This holds true even if you have built the string 831.Dq by hand 832using a function like 833.Fn snprintf , 834as the resulting string may still contain user-supplied conversion specifiers 835for later interpolation by 836.Fn printf . 837.Pp 838Be sure to use the proper secure idiom: 839.Bd -literal -offset indent 840snprintf(buffer, sizeof(buffer), "%s", string); 841.Ed 842.Pp 843There is no way for printf to know the size of each argument passed. 844If you use positional arguments you must ensure that all parameters, up to the 845last positionally specified parameter, are used in the format string. 846This allows for the format string to be parsed for this information. 847Failure to do this will mean your code is non-portable and liable to fail. 848.Pp 849In this implementation, passing a 850.Dv NULL 851.Vt char * 852argument to the 853.Cm %s 854format specifier will output 855.Em "(null)" 856instead of crashing. 857Programs that depend on this behavior are non-portable and may crash 858on other systems or in the future. 859.Sh BUGS 860The conversion formats 861.Cm \&%D , 862.Cm \&%O , 863and 864.Cm %U 865are not standard and are provided only for backward compatibility. 866The effect of padding the 867.Cm %p 868format with zeros (either by the 869.Sq Cm 0 870flag or by specifying a precision), and the benign effect (i.e. none) 871of the 872.Sq Cm # 873flag on 874.Cm %n 875and 876.Cm %p 877conversions, as well as other nonsensical combinations such as 878.Cm %Ld , 879are not standard; such combinations should be avoided. 880