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