1.\" $OpenBSD: printf.3,v 1.92 2021/09/01 17:47:33 jmc 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 $Mdocdate: September 1 2021 $ 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 dprintf , 46.Nm vprintf , 47.Nm vfprintf , 48.Nm vsprintf , 49.Nm vsnprintf , 50.Nm vasprintf , 51.Nm vdprintf 52.Nd formatted output conversion 53.Sh SYNOPSIS 54.In stdio.h 55.Ft int 56.Fn printf "const char *format" ... 57.Ft int 58.Fn fprintf "FILE *stream" "const char *format" ... 59.Ft int 60.Fn sprintf "char *str" "const char *format" ... 61.Ft int 62.Fn snprintf "char *str" "size_t size" "const char *format" ... 63.Ft int 64.Fn asprintf "char **ret" "const char *format" ... 65.Ft int 66.Fn dprintf "int fd" "const char * restrict format" ... 67.In stdarg.h 68.In stdio.h 69.Ft int 70.Fn vprintf "const char *format" "va_list ap" 71.Ft int 72.Fn vfprintf "FILE *stream" "const char *format" "va_list ap" 73.Ft int 74.Fn vsprintf "char *str" "const char *format" "va_list ap" 75.Ft int 76.Fn vsnprintf "char *str" "size_t size" "const char *format" "va_list ap" 77.Ft int 78.Fn vasprintf "char **ret" "const char *format" "va_list ap" 79.Ft int 80.Fn vdprintf "int fd" "const char * restrict format" "va_list ap" 81.Sh DESCRIPTION 82The 83.Fn printf 84family of functions produce output according to the given 85.Fa format 86as described below. 87This format may contain 88.Dq conversion specifiers ; 89the results of such conversions, if any, depend on the arguments 90following the 91.Fa format 92string. 93.Pp 94The 95.Fn printf 96and 97.Fn vprintf 98functions write output to the standard output stream, 99.Em stdout ; 100.Fn fprintf 101and 102.Fn vfprintf 103write output to the supplied stream pointer 104.Fa stream ; 105.Fn dprintf 106and 107.Fn vdprintf 108write output to the given file descriptor; 109.Fn sprintf , 110.Fn snprintf , 111.Fn vsprintf , 112and 113.Fn vsnprintf 114write to the character string 115.Fa str ; 116.Fn asprintf 117and 118.Fn vasprintf 119write to a dynamically allocated string that is stored in 120.Fa ret . 121.Pp 122These functions write the output under the control of a 123.Fa format 124string that specifies how subsequent arguments 125(or arguments accessed via the variable-length argument facilities of 126.Xr va_start 3 ) 127are converted for output. 128.Pp 129.Fn snprintf 130and 131.Fn vsnprintf 132write at most 133.Fa size Ns \-1 134characters to 135.Fa str , 136followed by a terminating 137.Ql \e0 . 138If 139.Fa size 140is zero, 141no characters are written and 142.Fa str 143may be a 144.Dv NULL 145pointer. 146.Pp 147.Fn sprintf 148and 149.Fn vsprintf 150effectively assume an infinite 151.Fa size ; 152their use is not recommended. 153.Pp 154The format string is composed of zero or more directives: 155ordinary 156.\" multibyte 157characters (not 158.Cm % ) , 159which are copied unchanged to the output stream, 160and conversion specifications, each of which results 161in fetching zero or more subsequent arguments. 162The arguments must correspond properly (after type promotion) 163with the conversion specifiers. 164.Pp 165The overall syntax of a conversion specification is: 166.Bd -filled -offset indent 167.Sm off 168.Cm % 169.Op Ar argno Cm $ 170.Op Ar flags 171.Op Ar width 172.Op . Ar precision 173.Op Ar size 174.Ar conversion 175.Sm on 176.Ed 177.Pp 178Not all combinations of these parts are meaningful; 179see the description of the individual 180.Ar conversion 181specifiers for details. 182.Pp 183The parts of a conversion specification are as follows: 184.Bl -tag -width Ds 185.It Cm % 186A literal percent character begins a conversion specification. 187.It Ar argno Ns Cm $ 188An unsigned decimal digit string followed by a dollar character 189specifies the index of the next argument to access. 190By default, the argument following the last argument accessed is used. 191Arguments are numbered starting at 1. 192.It Ar flags 193Zero or more of the following flag characters can be given: 194.Bl -tag -width 11n 195.It Cm # Pq hash 196Use an alternate form for the output. 197The effect differs depending on the conversion specifier. 198.It So \~ Sc Pq space 199For signed conversions, print a space character before a positive number. 200.It Cm + Pq plus 201For signed conversions, always print a sign before the number, 202even if it is positive. 203This overrides the space flag if both are specified. 204.It Cm 0 Pq zero 205Pad numbers with leading zeros instead of space characters 206to fill the field 207.Ar width . 208This flag is ignored if the 209.Ar precision 210modifier is also given, which in this case specifies 211.Ar mindigits . 212.It Cm \- Pq minus 213Left adjust: pad to the field 214.Ar width 215with space characters on the right rather than on the left. 216This overrides the 217.Sq Cm 0 218flag if both are specified. 219.El 220.It Ar width 221An unsigned decimal digit string specifies a minimum field width in bytes. 222Unless the 223.Sq Cm 0 224or 225.Sq Cm \- 226flag is given, the value is right adjusted in the field and 227padded with space characters on the left. 228By default, no padding is added. 229In no case does a non-existent or small field 230.Ar width 231cause truncation of a field; if the result of a conversion is wider 232than the field width, the field is expanded to contain the conversion 233result. 234.It Pf . Ar precision 235The meaning of an unsigned decimal digit string prefixed with a 236period character depends on the conversion specifier: 237it provides the minimum number of digits for integer conversions, 238of decimals for some floating point conversions and of significant 239digits for others, or the maximum number of bytes to print for 240string conversions. 241.Pp 242A field 243.Ar width 244or 245.Ar precision , 246or both, may alternatively be indicated as 247.Cm * Ns Op Ar argno Ns Cm $ , 248i.e. as an asterisk optionally followed 249by an unsigned decimal digit string and a dollar sign. 250In this case, an additional 251.Vt int 252argument supplies the field width or precision. 253If a single conversion specification tries to use arguments 254both with and without 255.Ar argno Ns Cm $ 256modifiers, the result is undefined. 257.It Ar size 258An argument size modifier. 259The syntax, the precise meaning, and the default size of the argument 260depend on the following 261.Ar conversion 262character. 263.It Ar conversion 264Each conversion specification ends with a conversion specifier, 265which is a single letter determining which argument type is expected 266and how it is formatted. 267.El 268.Pp 269The conversion specifiers are: 270.Bl -tag -width Ds 271.It Cm %a 272.Sm off 273.Cm % 274.Op Ar argno Cm $ 275.Op Cm # 276.Op Cm \~ | + 277.Op Cm \- | 0 278.Op Ar width 279.Op . Ar hexadecimals 280.Op Cm L | l 281.Cm a 282.Sm on 283.Pp 284The 285.Vt double 286argument is converted to the hexadecimal notation 287.Sm off 288.Oo \- Oc Sy 0x No h.hhh Sy p No \(+-d 289.Sm on 290with one digit before the hexadecimal point. 291If specified, the number is rounded to 292.Ar hexadecimals 293after the hexadecimal point; otherwise, 294enough digits are printed to represent it exactly. 295The hexadecimal point is only printed if at least one digit follows it 296or if the 297.Sq Cm # 298flag is given. 299.Pp 300The exponent is expressed in base 2, not in base 16. 301Consequently, there are multiple ways to represent a number in this format. 302For example, 0x3.24p+0, 0x6.48p-1, and 0xc.9p-2 are all equivalent. 303The format chosen depends on the internal representation of the 304number, but the implementation guarantees that the length of the 305mantissa is minimized. 306Zeroes are always represented with a mantissa of 307.Ql 0 308(preceded by a sign if appropriate) and an exponent of 309.Ql +0 . 310.Pp 311If the argument is infinity, it is converted to 312.Ql [-]inf . 313If the argument is not-a-number (NaN), it is converted to 314.Ql [-]nan . 315.Pp 316.Cm %La 317is similar to 318.Cm %a 319except that it takes an argument of 320.Vt long double . 321.Cm %la Pq ell a 322is an alias for 323.Cm %a . 324.It Cm \&%A 325Identical to 326.Cm %a 327except that upper case is used, i.e.\& 328.Ql 0X 329for the prefix, 330.Ql 0123456789ABCDEF 331for the digits, 332.Ql P 333to introduce the exponent, 334and 335.Ql [-]INF 336and 337.Ql [-]NAN 338for infinity and not-a-number, respectively. 339.It Cm %c 340.Sm off 341.Cm % 342.Op Ar argno Cm $ 343.Op Cm \- 344.Op Ar width 345.Cm c 346.Sm on 347.Pp 348The 349.Vt int 350argument is converted to an 351.Vt unsigned char , 352and the resulting single-byte character is written, with optional padding. 353.It Cm %lc 354.Sm off 355.Cm % 356.Op Ar argno Cm $ 357.Op Cm \- 358.Op Ar width 359.Cm lc 360.Sm on 361.Pp 362The 363.Vt wint_t 364argument is converted to a multibyte character according to the current 365.Dv LC_CTYPE 366.Xr locale 1 , 367and that character is written. 368For example, under a UTF-8 locale on 369.Ox , 370.Ql printf("%lc", 0x03c0) 371writes the greek letter pi, whereas the same call fails 372under the default POSIX locale. 373Padding assures at least 374.Ar width 375bytes are printed; the number of characters printed may be smaller, 376and the number of display columns occupied may be smaller or larger. 377.It Cm %d 378.Sm off 379.Cm % 380.Op Ar argno Cm $ 381.Op Cm \~ | + 382.Op Cm \- | 0 383.Op Ar width 384.Op . Ar mindigits 385.Op Ar size 386.Cm d 387.Sm on 388.Pp 389The 390.Vt int 391argument is converted to signed decimal notation. 392If specified, at least 393.Ar mindigits 394are printed, padding with leading zeros if needed. 395The following are similar to 396.Cm %d 397except that they take an argument of a different size: 398.Bl -column %hhd 399.It Cm %hhd Ta Vt signed char 400.It Cm %hd Ta Vt signed short 401.It Cm %d Ta Vt signed int 402.It Cm %ld Ta Vt signed long Pq percent ell dee 403.It Cm %lld Ta Vt signed long long Pq percent ell ell dee 404.It Cm %jd Ta Vt intmax_t 405.It Cm %td Ta Vt ptrdiff_t 406.It Cm %zd Ta Vt ssize_t 407.It Cm %qd Ta Vt quad_t Pq deprecated 408.El 409.It Cm \&%D 410A deprecated alias for 411.Cm %ld . 412.It Cm %e 413.Sm off 414.Cm % 415.Op Ar argno Cm $ 416.Op Cm # 417.Op Cm \~ | + 418.Op Cm \- | 0 419.Op Ar width 420.Op . Ar decimals 421.Op Cm L | l 422.Cm e 423.Sm on 424.Pp 425The 426.Vt double 427argument is rounded and converted to the scientific notation 428.Pf [\-]d.dddddd Sy e Ns \(+-dd 429with one digit before the decimal point and 430.Ar decimals , 431or six digits by default, after it. 432If 433.Ar decimals 434is zero and the 435.Sq Cm # 436flag is not given, the decimal point is omitted. 437The exponent always contains at least two digits; if the value is zero, 438the exponent is 439.Ql +00 . 440If the argument is infinity, it is converted to 441.Ql [-]inf . 442If the argument is not-a-number (NaN), it is converted to 443.Ql [-]nan . 444.Pp 445.Cm %Le 446is similar to 447.Cm %e 448except that it takes an argument of 449.Vt long double . 450.Cm %le Pq ell e 451is an alias for 452.Cm %e . 453.It Cm \&%E 454Identical to 455.Cm %e 456except that upper case is used, i.e.\& 457.Ql E 458instead of 459.Ql e 460to introduce the exponent and 461.Ql [-]INF 462and 463.Ql [-]NAN 464for infinity and not-a-number, respectively. 465.It Cm %f 466.Sm off 467.Cm % 468.Op Ar argno Cm $ 469.Op Cm # 470.Op Cm \~ | + 471.Op Cm \- | 0 472.Op Ar width 473.Op . Ar decimals 474.Op Cm L | l 475.Cm f 476.Sm on 477.Pp 478The 479.Vt double 480argument is rounded and converted to the decimal notation [\-]ddd.dddddd with 481.Ar decimals , 482or six digits by default, after the decimal point. 483If 484.Ar decimals 485is zero and the 486.Sq Cm # 487flag is not given, the decimal point is omitted. 488If a decimal point appears, at least one digit appears before it. 489If the argument is infinity, it is converted to 490.Ql [-]inf . 491If the argument is not-a-number (NaN), it is converted to 492.Ql [-]nan . 493.Pp 494.Cm %Lf 495is similar to 496.Cm %f 497except that it takes an argument of 498.Vt long double . 499.Cm %lf Pq ell eff 500is an alias for 501.Cm %f . 502.It Cm \&%F 503Identical to 504.Cm %f 505except that upper case is used, i.e.\& 506.Ql [-]INF 507and 508.Ql [-]NAN 509for infinity and not-a-number, respectively. 510.It Cm %g 511.Sm off 512.Cm % 513.Op Ar argno Cm $ 514.Op Cm # 515.Op Cm \~ | + 516.Op Cm \- | 0 517.Op Ar width 518.Op . Ar significant 519.Op Cm L | l 520.Cm g 521.Sm on 522.Pp 523The 524.Vt double 525argument is converted in style 526.Cm %f 527or 528.Cm %e 529.Pq general floating point notation 530with 531.Ar significant 532digits, or six significant digits by default. 533If 534.Ar significant 535is zero, one is used instead. 536Style 537.Cm %e 538is used if the exponent from its conversion is less than \-4 539or greater than or equal to 540.Ar significant . 541Unless the 542.Sq Cm # 543flag is given, trailing zeros are removed from the fractional 544part of the result, and the decimal point only appears if it is 545followed by at least one digit. 546.Pp 547.Cm %Lg 548is similar to 549.Cm %g 550except that it takes an argument of 551.Vt long double . 552.Cm %lg Pq ell gee 553is an alias for 554.Cm %g . 555.It Cm \&%G 556Identical to 557.Cm %g 558except that upper case is used, i.e.\& 559.Ql E 560instead of 561.Ql e 562to introduce the exponent and 563.Ql [-]INF 564and 565.Ql [-]NAN 566for infinity and not-a-number, respectively. 567.It Cm %i 568An alias for 569.Cm %d , 570supporting the same modifiers. 571.It Cm %n 572.Sm off 573.Cm % 574.Op Ar argno Cm $ 575.Op Ar size 576.Cm n 577.Sm on 578.Pp 579The 580.Cm %n 581conversion specifier has serious security implications, so it was changed to 582no longer store the number of bytes written so far into the variable indicated 583by the pointer argument. 584Instead a 585.Xr syslog 3 586message will be generated, after which the program is aborted with 587.Dv SIGABRT . 588.It Cm %o 589.Sm off 590.Cm % 591.Op Ar argno Cm $ 592.Op Cm # 593.Op Cm \- | 0 594.Op Ar width 595.Op . Ar mindigits 596.Op Ar size 597.Cm o 598.Sm on 599.Pp 600Similar to 601.Cm %u 602except that the 603.Vt unsigned int 604argument is converted to unsigned octal notation. 605If the 606.Sq Cm # 607flag is given, 608.Ar mindigits 609is increased such that the first digit printed is a zero, 610except if a zero value is printed with an explicit 611.Ar mindigits 612of zero. 613.It Cm \&%O 614A deprecated alias for 615.Cm %lo . 616.It Cm %p 617The 618.Vt void * 619pointer argument is printed in hexadecimal, similar to 620.Cm %#x 621or 622.Cm %#lx 623depending on the size of pointers. 624.It Cm %s 625.Sm off 626.Cm % 627.Op Ar argno Cm $ 628.Op Cm \- 629.Op Ar width 630.Op . Ar maxbytes 631.Cm s 632.Sm on 633.Pp 634Characters from the 635.Vt char * Pq string 636argument are written up to (but not including) a terminating NUL character. 637If 638.Ar maxbytes 639is specified, at most 640.Ar maxbytes 641bytes are written; in that case, no NUL character needs to be present. 642.It Cm %ls 643.Sm off 644.Cm % 645.Op Ar argno Cm $ 646.Op Cm \- 647.Op Ar width 648.Op . Ar maxbytes 649.Cm ls 650.Sm on 651.Pp 652The 653.Vt wchar_t * Pq wide character string 654argument is converted to a multibyte character string 655according to the current 656.Dv LC_CTYPE 657.Xr locale 1 658up to (but not including) a terminating NUL character, 659and that multibyte character string is written. 660If 661.Ar maxbytes 662is specified, at most 663.Ar maxbytes 664bytes are written; in that case, no NUL character needs to be present. 665If a multibyte character does not fit into the rest of 666.Ar maxbytes , 667it is omitted together with the rest of the argument string; 668partial characters are not written. 669Locale dependency and padding work in the same way as for 670.Cm %lc . 671.It Cm %u 672.Sm off 673.Cm % 674.Op Ar argno Cm $ 675.Op Cm \- | 0 676.Op Ar width 677.Op . Ar mindigits 678.Op Ar size 679.Cm u 680.Sm on 681.Pp 682The 683.Vt unsigned int 684argument is converted to unsigned decimal notation. 685If specified, at least 686.Ar mindigits 687are printed, padding with leading zeros if needed. 688The following are similar to 689.Cm %u 690except that they take an argument of a different size: 691.Bl -column %hhu 692.It Cm %hhu Ta Vt unsigned char 693.It Cm %hu Ta Vt unsigned short 694.It Cm %u Ta Vt unsigned int 695.It Cm %lu Ta Vt unsigned long Pq percent ell u 696.It Cm %llu Ta Vt unsigned long long Pq percent ell ell u 697.It Cm %ju Ta Vt uintmax_t 698.It Cm %tu Ta unsigned type of same size as Vt ptrdiff_t 699.It Cm %zu Ta Vt size_t 700.It Cm %qu Ta Vt u_quad_t Pq deprecated 701.El 702.It Cm \&%U 703A deprecated alias for 704.Cm %lu . 705.It Cm %x 706.Sm off 707.Cm % 708.Op Ar argno Cm $ 709.Op Cm # 710.Op Cm \- | 0 711.Op Ar width 712.Op . Ar mindigits 713.Op Ar size 714.Cm x 715.Sm on 716.Pp 717Similar to 718.Cm %u 719except that the 720.Vt unsigned int 721argument is converted to unsigned hexadecimal notation using the digits 722.Ql 0123456789abcdef . 723If the 724.Sq Cm # 725flag is given, the string 726.Ql 0x 727is prepended unless the value is zero. 728.It Cm \&%X 729Identical to 730.Cm %x 731except that upper case is used, i.e.\& 732.Ql 0X 733for the optional prefix and 734.Ql 0123456789ABCDEF 735for the digits. 736.It Cm %% 737A single percent sign 738.Pq Ql % 739is written. 740No argument is converted. 741The complete conversion specification is 742.Ql %% ; 743no modifiers can be inserted between the two percent signs. 744.El 745.Sh RETURN VALUES 746For all these functions if an output or encoding error occurs, a value 747less than 0 is returned. 748.Pp 749The 750.Fn printf , 751.Fn dprintf , 752.Fn fprintf , 753.Fn sprintf , 754.Fn vprintf , 755.Fn vdprintf , 756.Fn vfprintf , 757.Fn vsprintf , 758.Fn asprintf , 759and 760.Fn vasprintf 761functions 762return the number of bytes printed 763(not including the trailing 764.Ql \e0 765used to end output to strings). 766.Pp 767The 768.Fn snprintf 769and 770.Fn vsnprintf 771functions return the number of bytes that would have 772been output if the 773.Fa size 774were unlimited 775.Po 776again, not including the final 777.Ql \e0 778.Pc . 779A return value greater than or equal to the 780.Fa size 781argument indicates that the string was too small and some characters 782were discarded. 783.Pp 784The 785.Fn asprintf 786and 787.Fn vasprintf 788functions return the number of bytes that were output 789to the newly allocated string 790(excluding the final 791.Ql \e0 ) . 792A pointer to the newly allocated string is returned in 793.Fa ret ; 794it should be passed to 795.Xr free 3 796to release the allocated storage 797when it is no longer needed. 798If sufficient space cannot be allocated or some other error occurs, 799these functions return \-1. 800The value of 801.Fa ret 802in this situation is implementation-dependent. 803On 804.Ox , 805.Fa ret 806is set to the 807.Dv NULL 808pointer, but other implementations may leave 809.Fa ret 810unchanged. 811.Sh ENVIRONMENT 812.Bl -tag -width LC_CTYPE 813.It Ev LC_CTYPE 814The character encoding 815.Xr locale 1 . 816It decides which 817.Vt wchar_t 818values represent valid wide characters for the 819.Cm %lc 820and 821.Cm %ls 822conversion specifiers and how they are encoded into multibyte characters. 823If unset or set to 824.Qq C , 825.Qq POSIX , 826or an unsupported value, 827.Cm %lc 828and 829.Cm %ls 830only work correctly for ASCII characters 831and fail for arguments greater than 255. 832.El 833.Sh EXAMPLES 834To print a date and time in the form `Sunday, July 3, 10:02', 835where 836.Va weekday 837and 838.Va month 839are pointers to strings: 840.Bd -literal -offset indent 841#include <stdio.h> 842 843fprintf(stdout, "%s, %s %d, %.2d:%.2d\en", 844 weekday, month, day, hour, min); 845.Ed 846.Pp 847To print \*(Pi 848to five decimal places: 849.Bd -literal -offset indent 850#include <math.h> 851#include <stdio.h> 852 853fprintf(stdout, "pi = %.5f\en", 4 * atan(1.0)); 854.Ed 855.Pp 856To allocate a 128-byte string and print into it: 857.Bd -literal -offset indent 858#include <stdarg.h> 859#include <stdio.h> 860#include <stdlib.h> 861 862char * 863newfmt(const char *fmt, ...) 864{ 865 char *p; 866 va_list ap; 867 868 if ((p = malloc(128)) == NULL) 869 return (NULL); 870 va_start(ap, fmt); 871 (void) vsnprintf(p, 128, fmt, ap); 872 va_end(ap); 873 return (p); 874} 875.Ed 876.Sh ERRORS 877In addition to the errors documented for the 878.Xr write 2 879system call, the 880.Fn printf 881family of functions may fail if: 882.Bl -tag -width Er 883.It Bq Er EILSEQ 884An invalid wide character code was encountered. 885.It Bq Er ENOMEM 886Insufficient storage space is available. 887.It Bq Er EOVERFLOW 888The return value would be too large to be represented by an 889.Vt int . 890.El 891.Sh SEE ALSO 892.Xr printf 1 , 893.Xr scanf 3 , 894.Xr wprintf 3 895.Sh STANDARDS 896The 897.Fn fprintf , 898.Fn printf , 899.Fn snprintf , 900.Fn sprintf , 901.Fn vfprintf , 902.Fn vprintf , 903.Fn vsnprintf , 904and 905.Fn vsprintf 906functions conform to 907.St -isoC-99 . 908The 909.Fn dprintf 910and 911.Fn vdprintf 912functions conform to 913.St -p1003.1-2008 . 914.Sh HISTORY 915The predecessors 916.Fn ftoa 917and 918.Fn itoa 919first appeared in 920.At v1 . 921The function 922.Fn printf 923first appeared in 924.At v2 , 925and 926.Fn fprintf 927and 928.Fn sprintf 929in 930.At v7 . 931.Pp 932The functions 933.Fn snprintf 934and 935.Fn vsnprintf 936first appeared in 937.Bx 4.4 . 938.Pp 939The functions 940.Fn asprintf 941and 942.Fn vasprintf 943first appeared in the GNU C library. 944This implementation first appeared in 945.Ox 2.3 . 946.Pp 947The functions 948.Fn dprintf 949and 950.Fn vdprintf 951first appeared in 952.Ox 5.3 . 953.Sh CAVEATS 954The conversion formats 955.Cm \&%D , 956.Cm \&%O , 957and 958.Cm \&%U 959are not standard and 960are provided only for backward compatibility. 961The effect of padding the 962.Cm %p 963format with zeros (either by the 964.Sq Cm 0 965flag or by specifying a precision), and the benign effect (i.e., none) 966of the 967.Sq Cm # 968flag on 969.Cm %n 970and 971.Cm %p 972conversions, as well as other 973nonsensical combinations such as 974.Cm %Ld , 975are not standard; such combinations 976should be avoided. 977.Pp 978Because 979.Fn sprintf 980and 981.Fn vsprintf 982assume an infinitely long string, 983callers must be careful not to overflow the actual space; 984this is often impossible to assure. 985For safety, programmers should use the 986.Fn snprintf 987and 988.Fn asprintf 989family of interfaces instead. 990Unfortunately, the 991.Fn asprintf 992interface is not available on all systems as it is not part of 993.St -isoC-99 . 994.Pp 995It is important never to pass a string with user-supplied data as a 996format without using 997.Ql %s . 998An attacker can put format specifiers in the string to mangle the stack, 999leading to a possible security hole. 1000This holds true even if the string has been built 1001.Dq by hand 1002using a function like 1003.Fn snprintf , 1004as the resulting string may still contain user-supplied conversion specifiers 1005for later interpolation by 1006.Fn printf . 1007.Pp 1008Be sure to use the proper secure idiom: 1009.Bd -literal -offset indent 1010int ret = snprintf(buffer, sizeof(buffer), "%s", string); 1011if (ret < 0 || (size_t)ret >= sizeof(buffer)) 1012 goto toolong; 1013.Ed 1014.Pp 1015There is no way for 1016.Fn printf 1017to know the size of each argument passed. 1018If positional arguments are used, care must be taken to ensure that all 1019parameters, up to the 1020last positionally specified parameter, are used in the format string. 1021This allows for the format string to be parsed for this information. 1022Failure to do this will mean the code is non-portable and liable to fail. 1023.Pp 1024On systems other than 1025.Ox , 1026the 1027.Dv LC_NUMERIC 1028.Xr locale 1 1029category can cause erratic output; see CAVEATS in 1030.Xr setlocale 3 1031for details. 1032