1.\" $NetBSD: printf.3,v 1.54 2010/09/06 19:47:37 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 August 28, 2010 37.Dt PRINTF 3 38.Os 39.Sh NAME 40.Nm printf , 41.Nm fprintf , 42.Nm dprintf 43.Nm sprintf , 44.Nm snprintf , 45.Nm asprintf , 46.Nm vprintf , 47.Nm vfprintf , 48.Nm vsprintf , 49.Nm vdprintf , 50.Nm vsnprintf , 51.Nm vasprintf 52.Nd formatted output conversion 53.Sh LIBRARY 54.Lb libc 55.Sh SYNOPSIS 56.In stdio.h 57.Ft int 58.Fn printf "const char * restrict format" ... 59.Ft int 60.Fn fprintf "FILE * restrict stream" "const char * restrict format" ... 61.Ft int 62.Fn dprintf "int fd" "const char * restrict format" ... 63.Ft int 64.Fn sprintf "char * restrict str" "const char * restrict format" ... 65.Ft int 66.Fn snprintf "char * restrict str" "size_t size" "const char * restrict format" ... 67.Ft int 68.Fn asprintf "char ** restrict ret" "const char * restrict format" ... 69.In stdarg.h 70.Ft int 71.Fn vprintf "const char * restrict format" "va_list ap" 72.Ft int 73.Fn vfprintf "FILE * restrict stream" "const char * restrict format" "va_list ap" 74.Ft int 75.Fn vsprintf "char * restrict str" "const char * restrict format" "va_list ap" 76.Ft int 77.Fn vdprintf "int fd" "const char * restrict format" "va_list ap" 78.Ft int 79.Fn vsnprintf "char * restrict str" "size_t size" "const char * restrict format" "va_list ap" 80.Ft int 81.Fn vasprintf "char ** restrict ret" "const char * restrict format" "va_list ap" 82.Sh DESCRIPTION 83The 84.Fn printf 85family of functions produces output according to a 86.Fa format 87as described below. 88The 89.Fn printf 90and 91.Fn vprintf 92functions 93write output to 94.Em stdout , 95the standard output stream; 96.Fn fprintf 97and 98.Fn vfprintf 99write output to the given output 100.Fa stream ; 101.Fn dprintf 102and 103.Fn vdprintf 104write output to the give file descriptor 105.Fa fd ; 106.Fn sprintf , 107.Fn snprintf , 108.Fn vsprintf , 109and 110.Fn vsnprintf 111write to the character string 112.Fa str ; 113and 114.Fn asprintf 115and 116.Fn vasprintf 117write to a dynamically allocated string that is stored in 118.Fa ret . 119.Pp 120These functions write the output under the control of a 121.Fa format 122string that specifies how subsequent arguments 123(or arguments accessed via the variable-length argument facilities of 124.Xr stdarg 3 ) 125are converted for output. 126.Pp 127.Fn asprintf 128and 129.Fn vasprintf 130return a pointer to a buffer sufficiently large to hold the 131string in the 132.Fa ret 133argument. 134This pointer should be passed to 135.Xr free 3 136to release the allocated storage when it is no longer needed. 137If sufficient space cannot be allocated, these functions 138will return \-1 and set 139.Fa ret 140to be a 141.Dv NULL 142pointer. 143Please note that these functions are not standardized, and not all 144implementations can be assumed to set the 145.Fa ret 146argument to 147.Dv NULL 148on error. 149It is more portable to check for a return value of \-1 instead. 150.Pp 151.Fn snprintf 152and 153.Fn vsnprintf 154will write at most 155.Fa size Ns \-1 156of the characters printed into the output string 157(the 158.Fa size Ns 'th 159character then gets the terminating 160.Ql \e0 ) ; 161if the return value is greater than or equal to the 162.Fa size 163argument, the string was too short 164and some of the printed characters were discarded. 165If 166.Fa size 167is zero, nothing is written and 168.Fa str 169may be a 170.Dv NULL 171pointer. 172.Pp 173.Fn sprintf 174and 175.Fn vsprintf 176effectively assume an infinite 177.Fa size . 178.Pp 179The format string is composed of zero or more directives: 180ordinary 181.\" multibyte 182characters (not 183.Cm % ) , 184which are copied unchanged to the output stream; 185and conversion specifications, each of which results 186in fetching zero or more subsequent arguments. 187Each conversion specification is introduced by 188the character 189.Cm % . 190The arguments must correspond properly (after type promotion) 191with the conversion specifier. 192After the 193.Cm % , 194the following appear in sequence: 195.Bl -bullet 196.It 197An optional field, consisting of a decimal digit string followed by a 198.Cm $ , 199specifying the next argument to access. 200If this field is not provided, the argument following the last 201argument accessed will be used. 202Arguments are numbered starting at 203.Cm 1 . 204If unaccessed arguments in the format string are interspersed with ones that 205are accessed the results will be indeterminate. 206.It 207Zero or more of the following flags: 208.Bl -tag -width ".So \ Sc (space)" 209.It Sq Cm # 210The value should be converted to an 211.Dq alternate form . 212For 213.Cm c , 214.Cm d , 215.Cm i , 216.Cm n , 217.Cm p , 218.Cm s , 219and 220.Cm u 221conversions, this option has no effect. 222For 223.Cm o 224conversions, the precision of the number is increased to force the first 225character of the output string to a zero (except if a zero value is printed 226with an explicit precision of zero). 227For 228.Cm x 229and 230.Cm X 231conversions, a non-zero result has the string 232.Ql 0x 233(or 234.Ql 0X 235for 236.Cm X 237conversions) prepended to it. 238For 239.Cm a , 240.Cm A , 241.Cm e , 242.Cm E , 243.Cm f , 244.Cm F , 245.Cm g , 246and 247.Cm G 248conversions, the result will always contain a decimal point, even if no 249digits follow it (normally, a decimal point appears in the results of 250those conversions only if a digit follows). 251For 252.Cm g 253and 254.Cm G 255conversions, trailing zeros are not removed from the result as they 256would otherwise be. 257.It So Cm 0 Sc (zero) 258Zero padding. 259For all conversions except 260.Cm n , 261the converted value is padded on the left with zeros rather than blanks. 262If a precision is given with a numeric conversion 263.Pf ( Cm d , 264.Cm i , 265.Cm o , 266.Cm u , 267.Cm i , 268.Cm x , 269and 270.Cm X ) , 271the 272.Cm 0 273flag is ignored. 274.It Sq Cm \- 275A negative field width flag; 276the converted value is to be left adjusted on the field boundary. 277Except for 278.Cm n 279conversions, the converted value is padded on the right with blanks, 280rather than on the left with blanks or zeros. 281A 282.Sq Cm \- 283overrides a 284.Sq Cm \&0 285if both are given. 286.It So "\ " Sc (space) 287A blank should be left before a positive number 288produced by a signed conversion 289.Pf ( Cm a , 290.Cm A 291.Cm d , 292.Cm e , 293.Cm E , 294.Cm f , 295.Cm F , 296.Cm g , 297.Cm G , 298or 299.Cm i ) . 300.It Sq Cm + 301A sign must always be placed before a 302number produced by a signed conversion. 303A 304.Sq Cm + 305overrides a space if both are used. 306.It Sq Cm ' 307Decimal conversions 308.Cm ( d , u , 309or 310.Cm i ) 311or the integral portion of a floating point conversion 312.Cm ( f 313or 314.Cm F ) 315should be grouped and separated by thousands using 316the non-monetary separator returned by 317.Xr localeconv 3 . 318.El 319.It 320An optional decimal digit string specifying a minimum field width. 321If the converted value has fewer characters than the field width, it will 322be padded with spaces on the left (or right, if the left-adjustment 323flag has been given) to fill out the field width. 324.It 325An optional precision, in the form of a period 326.Sq Cm \&. 327followed by an optional digit string. 328If the digit string is omitted, the precision is taken as zero. 329This gives the minimum number of digits to appear for 330.Cm d , 331.Cm i , 332.Cm o , 333.Cm u , 334.Cm x , 335and 336.Cm X 337conversions, the number of digits to appear after the decimal-point for 338.Cm a , 339.Cm A , 340.Cm e , 341.Cm E , 342.Cm f , 343and 344.Cm F 345conversions, the maximum number of significant digits for 346.Cm g 347and 348.Cm G 349conversions, or the maximum number of characters to be printed from a 350string for 351.Cm s 352conversions. 353.It 354An optional length modifier, that specifies the size of the argument. 355The following length modifiers are valid for the 356.Cm d , i , n , o , u , x , 357or 358.Cm X 359conversion: 360.Bl -column ".Cm q Em (deprecated)" ".Vt signed char" ".Vt unsigned long long" ".Vt long long *" 361.It Sy Modifier Ta Cm d , i Ta Cm o , u , x , X Ta Cm n 362.It Cm hh Ta Vt "signed char" Ta Vt "unsigned char" Ta Vt "signed char *" 363.It Cm h Ta Vt short Ta Vt "unsigned short" Ta Vt "short *" 364.It Cm l No (ell) Ta Vt long Ta Vt "unsigned long" Ta Vt "long *" 365.It Cm ll No (ell ell) Ta Vt "long long" Ta Vt "unsigned long long" Ta Vt "long long *" 366.It Cm j Ta Vt intmax_t Ta Vt uintmax_t Ta Vt "intmax_t *" 367.It Cm t Ta Vt ptrdiff_t Ta (see note) Ta Vt "ptrdiff_t *" 368.It Cm z Ta (see note) Ta Vt size_t Ta (see note) 369.It Cm q Em (deprecated) Ta Vt quad_t Ta Vt u_quad_t Ta Vt "quad_t *" 370.El 371.Pp 372Note: 373the 374.Cm t 375modifier, when applied to a 376.Cm o , u , x , 377or 378.Cm X 379conversion, indicates that the argument is of an unsigned type 380equivalent in size to a 381.Vt ptrdiff_t . 382The 383.Cm z 384modifier, when applied to a 385.Cm d 386or 387.Cm i 388conversion, indicates that the argument is of a signed type equivalent in 389size to a 390.Vt size_t . 391Similarly, when applied to an 392.Cm n 393conversion, it indicates that the argument is a pointer to a signed type 394equivalent in size to a 395.Vt size_t . 396.Pp 397Note: 398if the standard integer types described in 399.Xr stdint 3 400are used, it is recommended that the predefined format string specifier 401macros are used when possible. 402These are further described in 403.Xr inttypes 3 . 404.Pp 405The following length modifier is valid for the 406.Cm a , 407.Cm A , 408.Cm e , 409.Cm E , 410.Cm f , 411.Cm F , 412.Cm g , 413or 414.Cm G 415conversion: 416.Bl -column ".Sy Modifier" ".Cm a , A , e , E , f , F , g , G" 417.It Sy Modifier Ta Cm a , A , e , E , f , F , g , G 418.It Cm l No (ell) Ta Vt double 419(ignored, same behavior as without it) 420.It Cm L Ta Vt "long double" 421.El 422.Pp 423The following length modifier is valid for the 424.Cm c 425or 426.Cm s 427conversion: 428.Bl -column ".Sy Modifier" ".Vt wint_t" ".Vt wchar_t *" 429.It Sy Modifier Ta Cm c Ta Cm s 430.It Cm l No (ell) Ta Vt wint_t Ta Vt "wchar_t *" 431.El 432.It 433A character that specifies the type of conversion to be applied. 434.El 435.Pp 436A field width or precision, or both, may be indicated by 437an asterisk 438.Ql * 439or an asterisk followed by one or more decimal digits and a 440.Ql $ 441instead of a 442digit string. 443In this case, an 444.Vt int 445argument supplies the field width or precision. 446A negative field width is treated as a left adjustment flag followed by a 447positive field width; a negative precision is treated as though it were 448missing. 449If a single format directive mixes positional 450.Pq Li nn$ 451and non-positional arguments, the results are undefined. 452.Pp 453The conversion specifiers and their meanings are: 454.Bl -tag -width ".Cm diouxX" 455.It Cm diouxX 456The 457.Vt int 458(or appropriate variant) argument is converted to signed decimal 459.Pf ( Cm d 460and 461.Cm i ) , 462unsigned octal 463.Pq Cm o , 464unsigned decimal 465.Pq Cm u , 466or unsigned hexadecimal 467.Pf ( Cm x 468and 469.Cm X ) 470notation. 471The letters 472.Dq Li abcdef 473are used for 474.Cm x 475conversions; the letters 476.Dq Li ABCDEF 477are used for 478.Cm X 479conversions. 480The precision, if any, gives the minimum number of digits that must 481appear; if the converted value requires fewer digits, it is padded on 482the left with zeros. 483.It Cm DOU 484The 485.Vt long int 486argument is converted to signed decimal, unsigned octal, or unsigned 487decimal, as if the format had been 488.Cm ld , 489.Cm lo , 490or 491.Cm lu 492respectively. 493These conversion characters are deprecated, and will eventually disappear. 494.It Cm eE 495The 496.Vt double 497argument is rounded and converted in the style 498.Sm off 499.Oo \- Oc Ar d Li \&. Ar ddd Li e \*[Pm] Ar dd 500.Sm on 501where there is one digit before the 502decimal-point character 503and the number of digits after it is equal to the precision; 504if the precision is missing, 505it is taken as 6; if the precision is 506zero, no decimal-point character appears. 507An 508.Cm E 509conversion uses the letter 510.Ql E 511(rather than 512.Ql e ) 513to introduce the exponent. 514The exponent always contains at least two digits; if the value is zero, 515the exponent is 00. 516.Pp 517For 518.Cm a , 519.Cm A , 520.Cm e , 521.Cm E , 522.Cm f , 523.Cm F , 524.Cm g , 525and 526.Cm G 527conversions, positive and negative infinity are represented as 528.Li inf 529and 530.Li -inf 531respectively when using the lowercase conversion character, and 532.Li INF 533and 534.Li -INF 535respectively when using the uppercase conversion character. 536Similarly, NaN is represented as 537.Li nan 538when using the lowercase conversion, and 539.Li NAN 540when using the uppercase conversion. 541.It Cm fF 542The 543.Vt double 544argument is rounded and converted to decimal notation in the style 545.Sm off 546.Oo \- Oc Ar ddd Li \&. Ar 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.It Cm gG 554The 555.Vt double 556argument is converted in style 557.Cm f 558or 559.Cm e 560(or in style 561.Cm F 562or 563.Cm E 564for 565.Cm G 566conversions). 567The precision specifies the number of significant digits. 568If the precision is missing, 6 digits are given; if the precision is zero, 569it is treated as 1. 570Style 571.Cm e 572is used if the exponent from its conversion is less than \-4 or greater than 573or equal to the precision. 574Trailing zeros are removed from the fractional part of the result; a 575decimal point appears only if it is followed by at least one digit. 576.It Cm aA 577The 578.Vt double 579argument is rounded and converted to hexadecimal notation in the style 580.Sm off 581.Oo \- Oc Li 0x Ar h Li \&. Ar hhhp Oo \*[Pm] Oc Ar d , 582.Sm on 583where the number of digits after the hexadecimal-point character 584is equal to the precision specification. 585If the precision is missing, it is taken as enough to represent 586the floating-point number exactly, and no rounding occurs. 587If the precision is zero, no hexadecimal-point character appears. 588The 589.Cm p 590is a literal character 591.Ql p , 592and the exponent consists of a positive or negative sign 593followed by a decimal number representing an exponent of 2. 594The 595.Cm A 596conversion uses the prefix 597.Dq Li 0X 598(rather than 599.Dq Li 0x ) , 600the letters 601.Dq Li ABCDEF 602(rather than 603.Dq Li abcdef ) 604to represent the hex digits, and the letter 605.Ql P 606(rather than 607.Ql p ) 608to separate the mantissa and exponent. 609.Pp 610Note that there may be multiple valid ways to represent floating-point 611numbers in this hexadecimal format. 612For example, 613.Li 0x3.24p+0 , 0x6.48p-1 614and 615.Li 0xc.9p-2 616are all equivalent. 617The format chosen depends on the internal representation of the 618number, but the implementation guarantees that the length of the 619mantissa will be minimized. 620Zeroes are always represented with a mantissa of 0 (preceded by a 621.Ql - 622if appropriate) and an exponent of 623.Li +0 . 624.It Cm C 625Treated as 626.Cm c 627with the 628.Cm l 629(ell) modifier. 630.It Cm c 631The 632.Vt int 633argument is converted to an 634.Vt "unsigned char" , 635and the resulting character is written. 636.Pp 637If the 638.Cm l 639(ell) modifier is used, the 640.Vt wint_t 641argument shall be converted to a 642.Vt wchar_t , 643and the (potentially multi-byte) sequence representing the 644single wide character is written, including any shift sequences. 645If a shift sequence is used, the shift state is also restored 646to the original state after the character. 647.It Cm S 648Treated as 649.Cm s 650with the 651.Cm l 652(ell) modifier. 653.It Cm s 654The 655.Vt "char *" 656argument is expected to be a pointer to an array of character type (pointer 657to a string). 658Characters from the array are written up to (but not including) 659a terminating 660.Dv NUL 661character; 662if a precision is specified, no more than the number specified are 663written. 664If a precision is given, no null character 665need be present; if the precision is not specified, or is greater than 666the size of the array, the array must contain a terminating 667.Dv NUL 668character. 669.Pp 670If the 671.Cm l 672(ell) modifier is used, the 673.Vt "wchar_t *" 674argument is expected to be a pointer to an array of wide characters 675(pointer to a wide string). 676For each wide character in the string, the (potentially multi-byte) 677sequence representing the 678wide character is written, including any shift sequences. 679If any shift sequence is used, the shift state is also restored 680to the original state after the string. 681Wide characters from the array are written up to (but not including) 682a terminating wide 683.Dv NUL 684character; 685if a precision is specified, no more than the number of bytes specified are 686written (including shift sequences). 687Partial characters are never written. 688If a precision is given, no null character 689need be present; if the precision is not specified, or is greater than 690the number of bytes required to render the multibyte representation of 691the string, the array must contain a terminating wide 692.Dv NUL 693character. 694.It Cm p 695The 696.Vt "void *" 697pointer argument is printed in hexadecimal (as if by 698.Ql %#x 699or 700.Ql %#lx ) . 701.It Cm n 702The number of characters written so far is stored into the 703integer indicated by the 704.Vt "int *" 705(or variant) pointer argument. 706No argument is converted. 707.It Cm % 708A 709.Ql % 710is written. 711No argument is converted. 712The complete conversion specification is 713.Ql %% . 714.El 715.Pp 716The decimal point 717character is defined in the program's locale (category 718.Dv LC_NUMERIC ) . 719.Pp 720In no case does a non-existent or small field width cause truncation of 721a numeric field; if the result of a conversion is wider than the field 722width, the 723field is expanded to contain the conversion result. 724.Sh RETURN VALUES 725These functions return 726the number of characters printed, or that would be printed if there 727was adequate space in case of 728.Fn snprintf 729and 730.Fn vsnprintf , 731(not including the trailing 732.Ql \e0 733used to end output to strings). 734If an output error was encountered, these functions shall return a 735negative value. 736.Sh EXAMPLES 737To print a date and time in the form 738.Dq Li "Sunday, July 3, 10:02" , 739where 740.Fa weekday 741and 742.Fa month 743are pointers to strings: 744.Bd -literal -offset indent 745#include \*[Lt]stdio.h\*[Gt] 746fprintf(stdout, "%s, %s %d, %.2d:%.2d\en", 747 weekday, month, day, hour, min); 748.Ed 749.Pp 750To print \*(Pi 751to five decimal places: 752.Bd -literal -offset indent 753#include \*[Lt]math.h\*[Gt] 754#include \*[Lt]stdio.h\*[Gt] 755fprintf(stdout, "pi = %.5f\en", 4 * atan(1.0)); 756.Ed 757.Pp 758To allocate a 128 byte string and print into it: 759.Bd -literal -offset indent 760#include \*[Lt]stdio.h\*[Gt] 761#include \*[Lt]stdlib.h\*[Gt] 762#include \*[Lt]stdarg.h\*[Gt] 763char *newfmt(const char *fmt, ...) 764{ 765 char *p; 766 va_list ap; 767 if ((p = malloc(128)) == NULL) 768 return (NULL); 769 va_start(ap, fmt); 770 (void) vsnprintf(p, 128, fmt, ap); 771 va_end(ap); 772 return (p); 773} 774.Ed 775.Sh ERRORS 776In addition to the errors documented for the 777.Xr write 2 778system call, the 779.Fn printf 780family of functions may fail if: 781.Bl -tag -width Er 782.It Bq Er EILSEQ 783An invalid wide character code was encountered. 784.It Bq Er ENOMEM 785Insufficient storage space is available. 786.El 787.Sh SEE ALSO 788.Xr printf 1 , 789.Xr fmtcheck 3 , 790.Xr scanf 3 , 791.Xr setlocale 3 , 792.Xr wprintf 3 , 793.Xr printf 9 794.Sh STANDARDS 795Subject to the caveats noted in the 796.Sx BUGS 797section below, the 798.Fn fprintf , 799.Fn printf , 800.Fn sprintf , 801.Fn vprintf , 802.Fn vfprintf , 803and 804.Fn vsprintf 805functions 806conform to 807.St -ansiC 808and 809.St -isoC-99 . 810With the same reservation, the 811.Fn snprintf 812and 813.Fn vsnprintf 814functions conform to 815.St -isoC-99 . 816.Sh HISTORY 817The functions 818.Fn snprintf 819and 820.Fn vsnprintf 821first appeared in 822.Bx 4.4 . 823The functions 824.Fn asprintf 825and 826.Fn vasprintf 827are modeled on the ones that first appeared in the GNU C library. 828The functions 829.Fn dprintf 830and 831.Fn vdprintf 832are parts of 833.St -p1003.1-2008 834and appeared in 835.Nx 6.0 . 836.Sh CAVEATS 837Because 838.Fn sprintf 839and 840.Fn vsprintf 841assume an infinitely long string, callers must be careful not to 842overflow the actual space; this is often impossible to assure. 843For safety, programmers should use the 844.Fn snprintf 845and 846.Fn asprintf 847family of interfaces instead. 848Unfortunately, the 849.Fn snprintf 850interfaces are not available on older 851systems and the 852.Fn asprintf 853interfaces are not yet portable. 854.Pp 855It is important never to pass a string with user-supplied data as a 856format without using 857.Ql %s . 858An attacker can put format specifiers in the string to mangle your stack, 859leading to a possible security hole. 860This holds true even if you have built the string 861.Dq by hand 862using a function like 863.Fn snprintf , 864as the resulting string may still contain user-supplied conversion specifiers 865for later interpolation by 866.Fn printf . 867.Pp 868Be sure to use the proper secure idiom: 869.Bd -literal -offset indent 870snprintf(buffer, sizeof(buffer), "%s", string); 871.Ed 872.Pp 873There is no way for printf to know the size of each argument passed. 874If you use positional arguments you must ensure that all parameters, up to the 875last positionally specified parameter, are used in the format string. 876This allows for the format string to be parsed for this information. 877Failure to do this will mean your code is non-portable and liable to fail. 878.Pp 879In this implementation, passing a 880.Dv NULL 881.Vt char * 882argument to the 883.Cm %s 884format specifier will output 885.Em "(null)" 886instead of crashing. 887Programs that depend on this behavior are non-portable and may crash 888on other systems or in the future. 889.Sh BUGS 890The conversion formats 891.Cm \&%D , 892.Cm \&%O , 893and 894.Cm \&%U 895are not standard and are provided only for backward compatibility. 896The effect of padding the 897.Cm %p 898format with zeros (either by the 899.Sq Cm 0 900flag or by specifying a precision), and the benign effect (i.e. none) 901of the 902.Sq Cm # 903flag on 904.Cm %n 905and 906.Cm %p 907conversions, as well as other nonsensical combinations such as 908.Cm %Ld , 909are not standard; such combinations should be avoided. 910.Pp 911The 912.Nm 913family of functions do not correctly handle multibyte characters in the 914.Fa format 915argument. 916.Sh SECURITY CONSIDERATIONS 917The 918.Fn sprintf 919and 920.Fn vsprintf 921functions are easily misused in a manner which enables malicious users 922to arbitrarily change a running program's functionality through 923a buffer overflow attack. 924Because 925.Fn sprintf 926and 927.Fn vsprintf 928assume an infinitely long string, 929callers must be careful not to overflow the actual space; 930this is often hard to assure. 931For safety, programmers should use the 932.Fn snprintf 933interface instead. 934For example: 935.Bd -literal 936void 937foo(const char *arbitrary_string, const char *and_another) 938{ 939 char onstack[8]; 940 941#ifdef BAD 942 /* 943 * This first sprintf is bad behavior. Do not use sprintf! 944 */ 945 sprintf(onstack, "%s, %s", arbitrary_string, and_another); 946#else 947 /* 948 * The following two lines demonstrate better use of 949 * snprintf(). 950 */ 951 snprintf(onstack, sizeof(onstack), "%s, %s", arbitrary_string, 952 and_another); 953#endif 954} 955.Ed 956.Pp 957The 958.Fn printf 959and 960.Fn sprintf 961family of functions are also easily misused in a manner 962allowing malicious users to arbitrarily change a running program's 963functionality by either causing the program 964to print potentially sensitive data 965.Dq "left on the stack" , 966or causing it to generate a memory fault or bus error 967by dereferencing an invalid pointer. 968.Pp 969.Cm %n 970can be used to write arbitrary data to potentially carefully-selected 971addresses. 972Programmers are therefore strongly advised to never pass untrusted strings 973as the 974.Fa format 975argument, as an attacker can put format specifiers in the string 976to mangle your stack, 977leading to a possible security hole. 978This holds true even if the string was built using a function like 979.Fn snprintf , 980as the resulting string may still contain user-supplied conversion specifiers 981for later interpolation by 982.Fn printf . 983.Pp 984Always use the proper secure idiom: 985.Pp 986.Dl "snprintf(buffer, sizeof(buffer), \*q%s\*q, string);" 987