1.\" $OpenBSD: printf.3,v 1.89 2020/09/13 12:58:08 tb 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 13 2020 $ 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 number of bytes written so far is stored at the variable indicated 580by the pointer argument. 581The 582.Cm %n 583conversion specifier produces no output. 584.Pp 585Make sure the 586.Ar size 587modifier matches the type of the pointer passed: 588.Bl -column %hhn 589.It Cm %hhn Ta Vt signed char * 590.It Cm %hn Ta Vt signed short * 591.It Cm %n Ta Vt signed int * 592.It Cm %ln Ta Vt signed long * Pq percent ell en 593.It Cm %lln Ta Vt signed long long * Pq percent ell ell en 594.It Cm %jn Ta Vt intmax_t * 595.It Cm %tn Ta Vt ptrdiff_t * 596.It Cm %zn Ta Vt ssize_t * 597.It Cm %qn Ta Vt quad_t * Pq deprecated 598.El 599.It Cm %o 600.Sm off 601.Cm % 602.Op Ar argno Cm $ 603.Op Cm # 604.Op Cm \- | 0 605.Op Ar width 606.Op . Ar mindigits 607.Op Ar size 608.Cm o 609.Sm on 610.Pp 611Similar to 612.Cm %u 613except that the 614.Vt unsigned int 615argument is converted to unsigned octal notation. 616If the 617.Sq Cm # 618flag is given, 619.Ar mindigits 620is increased such that the first digit printed is a zero, 621except if a zero value is printed with an explicit 622.Ar mindigits 623of zero. 624.It Cm \&%O 625A deprecated alias for 626.Cm %lo . 627.It Cm %p 628The 629.Vt void * 630pointer argument is printed in hexadecimal, similar to 631.Cm %#x 632or 633.Cm %#lx 634depending on the size of pointers. 635.It Cm %s 636.Sm off 637.Cm % 638.Op Ar argno Cm $ 639.Op Cm \- 640.Op Ar width 641.Op . Ar maxbytes 642.Cm s 643.Sm on 644.Pp 645Characters from the 646.Vt char * Pq string 647argument are written up to (but not including) a terminating NUL character. 648If 649.Ar maxbytes 650is specified, at most 651.Ar maxbytes 652bytes are written; in that case, no NUL character needs to be present. 653.It Cm %ls 654.Sm off 655.Cm % 656.Op Ar argno Cm $ 657.Op Cm \- 658.Op Ar width 659.Op . Ar maxbytes 660.Cm ls 661.Sm on 662.Pp 663The 664.Vt wchar_t * Pq wide character string 665argument is converted to a multibyte character string 666according to the current 667.Dv LC_CTYPE 668.Xr locale 1 669up to (but not including) a terminating NUL character, 670and that multibyte character string is written. 671If 672.Ar maxbytes 673is specified, at most 674.Ar maxbytes 675bytes are written; in that case, no NUL character needs to be present. 676If a multibyte character does not fit into the rest of 677.Ar maxbytes , 678it is omitted together with the rest of the argument string; 679partial characters are not written. 680Locale dependency and padding work in the same way as for 681.Cm %lc . 682.It Cm %u 683.Sm off 684.Cm % 685.Op Ar argno Cm $ 686.Op Cm \- | 0 687.Op Ar width 688.Op . Ar mindigits 689.Op Ar size 690.Cm u 691.Sm on 692.Pp 693The 694.Vt unsigned int 695argument is converted to unsigned decimal notation. 696If specified, at least 697.Ar mindigits 698are printed, padding with leading zeros if needed. 699The following are similar to 700.Cm %u 701except that they take an argument of a different size: 702.Bl -column %hhu 703.It Cm %hhu Ta Vt unsigned char 704.It Cm %hu Ta Vt unsigned short 705.It Cm %u Ta Vt unsigned int 706.It Cm %lu Ta Vt unsigned long Pq percent ell u 707.It Cm %llu Ta Vt unsigned long long Pq percent ell ell u 708.It Cm %ju Ta Vt uintmax_t 709.It Cm %tu Ta unsigned type of same size as Vt ptrdiff_t 710.It Cm %zu Ta Vt size_t 711.It Cm %qu Ta Vt u_quad_t Pq deprecated 712.El 713.It Cm \&%U 714A deprecated alias for 715.Cm %lu . 716.It Cm %x 717.Sm off 718.Cm % 719.Op Ar argno Cm $ 720.Op Cm # 721.Op Cm \- | 0 722.Op Ar width 723.Op . Ar mindigits 724.Op Ar size 725.Cm x 726.Sm on 727.Pp 728Similar to 729.Cm %u 730except that the 731.Vt unsigned int 732argument is converted to unsigned hexadecimal notation using the digits 733.Ql 0123456789abcdef . 734If the 735.Sq Cm # 736flag is given, the string 737.Ql 0x 738is prepended unless the value is zero. 739.It Cm \&%X 740Identical to 741.Cm %x 742except that upper case is used, i.e.\& 743.Ql 0X 744for the optional prefix and 745.Ql 0123456789ABCDEF 746for the digits. 747.It Cm %% 748A single percent sign 749.Pq Ql % 750is written. 751No argument is converted. 752The complete conversion specification is 753.Ql %% ; 754no modifiers can be inserted between the two percent signs. 755.El 756.Sh RETURN VALUES 757For all these functions if an output or encoding error occurs, a value 758less than 0 is returned. 759.Pp 760The 761.Fn printf , 762.Fn dprintf , 763.Fn fprintf , 764.Fn sprintf , 765.Fn vprintf , 766.Fn vdprintf , 767.Fn vfprintf , 768.Fn vsprintf , 769.Fn asprintf , 770and 771.Fn vasprintf 772functions 773return the number of bytes printed 774(not including the trailing 775.Ql \e0 776used to end output to strings). 777.Pp 778The 779.Fn snprintf 780and 781.Fn vsnprintf 782functions return the number of bytes that would have 783been output if the 784.Fa size 785were unlimited 786.Po 787again, not including the final 788.Ql \e0 789.Pc . 790A return value greater than or equal to the 791.Fa size 792argument indicates that the string was too small and some characters 793were discarded. 794.Pp 795The 796.Fn asprintf 797and 798.Fn vasprintf 799functions return the number of bytes that were output 800to the newly allocated string 801(excluding the final 802.Ql \e0 ) . 803A pointer to the newly allocated string is returned in 804.Fa ret ; 805it should be passed to 806.Xr free 3 807to release the allocated storage 808when it is no longer needed. 809If sufficient space cannot be allocated or some other error occurs, 810these functions return \-1. 811The value of 812.Fa ret 813in this situation is implementation-dependent. 814On 815.Ox , 816.Fa ret 817is set to the 818.Dv NULL 819pointer, but other implementations may leave 820.Fa ret 821unchanged. 822.Sh ENVIRONMENT 823.Bl -tag -width LC_CTYPE 824.It Ev LC_CTYPE 825The character encoding 826.Xr locale 1 . 827It decides which 828.Vt wchar_t 829values represent valid wide characters for the 830.Cm %lc 831and 832.Cm %ls 833conversion specifiers and how they are encoded into multibyte characters. 834If unset or set to 835.Qq C , 836.Qq POSIX , 837or an unsupported value, 838.Cm %lc 839and 840.Cm %ls 841only work correctly for ASCII characters 842and fail for arguments greater than 255. 843.El 844.Sh EXAMPLES 845To print a date and time in the form `Sunday, July 3, 10:02', 846where 847.Va weekday 848and 849.Va month 850are pointers to strings: 851.Bd -literal -offset indent 852#include <stdio.h> 853 854fprintf(stdout, "%s, %s %d, %.2d:%.2d\en", 855 weekday, month, day, hour, min); 856.Ed 857.Pp 858To print \*(Pi 859to five decimal places: 860.Bd -literal -offset indent 861#include <math.h> 862#include <stdio.h> 863 864fprintf(stdout, "pi = %.5f\en", 4 * atan(1.0)); 865.Ed 866.Pp 867To allocate a 128-byte string and print into it: 868.Bd -literal -offset indent 869#include <stdarg.h> 870#include <stdio.h> 871#include <stdlib.h> 872 873char * 874newfmt(const char *fmt, ...) 875{ 876 char *p; 877 va_list ap; 878 879 if ((p = malloc(128)) == NULL) 880 return (NULL); 881 va_start(ap, fmt); 882 (void) vsnprintf(p, 128, fmt, ap); 883 va_end(ap); 884 return (p); 885} 886.Ed 887.Sh ERRORS 888In addition to the errors documented for the 889.Xr write 2 890system call, the 891.Fn printf 892family of functions may fail if: 893.Bl -tag -width Er 894.It Bq Er EILSEQ 895An invalid wide character code was encountered. 896.It Bq Er ENOMEM 897Insufficient storage space is available. 898.It Bq Er EOVERFLOW 899The return value would be too large to be represented by an 900.Vt int . 901.El 902.Sh SEE ALSO 903.Xr printf 1 , 904.Xr scanf 3 , 905.Xr wprintf 3 906.Sh STANDARDS 907The 908.Fn fprintf , 909.Fn printf , 910.Fn snprintf , 911.Fn sprintf , 912.Fn vfprintf , 913.Fn vprintf , 914.Fn vsnprintf , 915and 916.Fn vsprintf 917functions conform to 918.St -isoC-99 . 919The 920.Fn dprintf 921and 922.Fn vdprintf 923functions conform to 924.St -p1003.1-2008 . 925.Sh HISTORY 926The predecessors 927.Fn ftoa 928and 929.Fn itoa 930first appeared in 931.At v1 . 932The function 933.Fn printf 934first appeared in 935.At v2 , 936and 937.Fn fprintf 938and 939.Fn sprintf 940in 941.At v7 . 942.Pp 943The functions 944.Fn snprintf 945and 946.Fn vsnprintf 947first appeared in 948.Bx 4.4 . 949.Pp 950The functions 951.Fn asprintf 952and 953.Fn vasprintf 954first appeared in the GNU C library. 955This implementation first appeared in 956.Ox 2.3 . 957.Pp 958The functions 959.Fn dprintf 960and 961.Fn vdprintf 962first appeared in 963.Ox 5.3 . 964.Sh CAVEATS 965The conversion formats 966.Cm \&%D , 967.Cm \&%O , 968and 969.Cm \&%U 970are not standard and 971are provided only for backward compatibility. 972The effect of padding the 973.Cm %p 974format with zeros (either by the 975.Sq Cm 0 976flag or by specifying a precision), and the benign effect (i.e., none) 977of the 978.Sq Cm # 979flag on 980.Cm %n 981and 982.Cm %p 983conversions, as well as other 984nonsensical combinations such as 985.Cm %Ld , 986are not standard; such combinations 987should be avoided. 988.Pp 989Because 990.Fn sprintf 991and 992.Fn vsprintf 993assume an infinitely long string, 994callers must be careful not to overflow the actual space; 995this is often impossible to assure. 996For safety, programmers should use the 997.Fn snprintf 998and 999.Fn asprintf 1000family of interfaces instead. 1001Unfortunately, the 1002.Fn asprintf 1003interface is not available on all systems as it is not part of 1004.St -isoC-99 . 1005.Pp 1006It is important never to pass a string with user-supplied data as a 1007format without using 1008.Ql %s . 1009An attacker can put format specifiers in the string to mangle the stack, 1010leading to a possible security hole. 1011This holds true even if the string has been built 1012.Dq by hand 1013using a function like 1014.Fn snprintf , 1015as the resulting string may still contain user-supplied conversion specifiers 1016for later interpolation by 1017.Fn printf . 1018.Pp 1019Be sure to use the proper secure idiom: 1020.Bd -literal -offset indent 1021int ret = snprintf(buffer, sizeof(buffer), "%s", string); 1022if (ret < 0 || ret >= sizeof(buffer)) 1023 goto toolong; 1024.Ed 1025.Pp 1026There is no way for 1027.Fn printf 1028to know the size of each argument passed. 1029If positional arguments are used, care must be taken to ensure that all 1030parameters, up to the 1031last positionally specified parameter, are used in the format string. 1032This allows for the format string to be parsed for this information. 1033Failure to do this will mean the code is non-portable and liable to fail. 1034.Pp 1035On systems other than 1036.Ox , 1037the 1038.Dv LC_NUMERIC 1039.Xr locale 1 1040category can cause erratic output; see CAVEATS in 1041.Xr setlocale 3 1042for details. 1043