1.\" $OpenBSD: printf.3,v 1.15 1999/03/01 20:41:56 deraadt 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. All advertising materials mentioning features or use of this software 19.\" must display the following acknowledgement: 20.\" This product includes software developed by the University of 21.\" California, Berkeley and its contributors. 22.\" 4. Neither the name of the University nor the names of its contributors 23.\" may be used to endorse or promote products derived from this software 24.\" without specific prior written permission. 25.\" 26.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36.\" SUCH DAMAGE. 37.\" 38.\" @(#)printf.3 8.1 (Berkeley) 6/4/93 39.\" 40.Dd June 4, 1993 41.Dt PRINTF 3 42.Os 43.Sh NAME 44.Nm printf , 45.Nm fprintf , 46.Nm sprintf , 47.Nm snprintf , 48.Nm asprintf , 49.Nm vprintf , 50.Nm vfprintf , 51.Nm vsprintf , 52.Nm vsnprintf , 53.Nm vasprintf , 54.Nm vansprintf 55.Nd formatted output conversion 56.Sh SYNOPSIS 57.Fd #include <stdio.h> 58.Ft int 59.Fn printf "const char *format" ... 60.Ft int 61.Fn fprintf "FILE *stream" "const char *format" ... 62.Ft int 63.Fn sprintf "char *str" "const char *format" ... 64.Ft int 65.Fn snprintf "char *str" "size_t size" "const char *format" ... 66.Ft int 67.Fn asprintf "char **ret" "const char *format" ... 68.Fd #include <stdarg.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.Sh DESCRIPTION 80The 81.Fn printf 82family of functions produces output according to a 83.Fa format 84as described below. 85.Fn printf 86and 87.Fn vprintf 88write output to 89.Em stdout, 90the standard output stream; 91.Fn fprintf 92and 93.Fn vfprintf 94write output to the given output 95.Fa stream ; 96.Fn sprintf , 97.Fn snprintf , 98.Fn vsprintf 99and 100.Fn vsnprintf 101write to the character string 102.Fa str ; 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 116These functions return 117the number of characters printed 118(not including the trailing 119.Ql \e0 120used to end output to strings). 121.Pp 122.Fn asprintf 123and 124.Fn vasprintf 125return a pointer to a buffer sufficiently large to hold the 126string in the 127.Fa ret 128argument. 129This pointer should be passed to 130.Xr free 3 131to release the allocated storage when it is no longer needed. 132If sufficient space cannot be allocated, these functions 133will return -1 and set 134.Fa ret 135to be a NULL pointer. 136.Pp 137If 138.Fn asprintf 139or 140.Fn vasprintf 141are given a 142.Fa size 143of 0, they set 144.Fa ret 145to be a NULL pointer. 146.Pp 147.Fn snprintf 148and 149.Fn vsnprintf 150will write at most 151.Fa size Ns \-1 152of the characters printed into the output string 153(the 154.Fa size Ns 'th 155character then gets the terminating 156.Ql \e0 ) ; 157if the return value is greater than or equal to the 158.Fa size 159argument, the string was too short 160and some of the printed characters were discarded. 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 . 193.It 194Zero or more of the following flags: 195.Bl -hyphen 196.It 197A 198.Cm # 199character 200specifying that the value should be converted to an ``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 e , 229.Cm E , 230.Cm f , 231.Cm g , 232and 233.Cm G , 234conversions, the result will always contain a decimal point, even if no 235digits follow it (normally, a decimal point appears in the results of 236those conversions only if a digit follows). 237For 238.Cm g 239and 240.Cm G 241conversions, trailing zeros are not removed from the result as they 242would otherwise be. 243.It 244A zero 245.Sq Cm \&0 246character specifying zero padding. 247For all conversions except 248.Cm n , 249the converted value is padded on the left with zeros rather than blanks. 250If a precision is given with a numeric conversion 251.Pf ( Cm d , 252.Cm i , 253.Cm o , 254.Cm u , 255.Cm i , 256.Cm x , 257and 258.Cm X ) , 259the 260.Sq Cm \&0 261flag is ignored. 262.It 263A negative field width flag 264.Sq Cm \- 265indicates the 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 276A space, specifying that a blank should be left before a positive number 277produced by a signed conversion 278.Pf ( Cm d , 279.Cm e , 280.Cm E , 281.Cm f , 282.Cm g , 283.Cm G , 284or 285.Cm i ) . 286.It 287A 288.Sq Cm + 289character specifying that a sign always be placed before a 290number produced by a signed conversion. 291A 292.Sq Cm + 293overrides a space if both are used. 294.El 295.It 296An optional decimal digit string specifying a minimum field width. 297If the converted value has fewer characters than the field width, it will 298be padded with spaces on the left (or right, if the left-adjustment 299flag has been given) to fill out 300the field width. 301.It 302An optional precision, in the form of a period 303.Sq Cm \&. 304followed by an 305optional digit string. If the digit string is omitted, the precision 306is taken as zero. This gives the minimum number of digits to appear for 307.Cm d , 308.Cm i , 309.Cm o , 310.Cm u , 311.Cm x , 312and 313.Cm X 314conversions, the number of digits to appear after the decimal-point for 315.Cm e , 316.Cm E , 317and 318.Cm f 319conversions, the maximum number of significant digits for 320.Cm g 321and 322.Cm G 323conversions, or the maximum number of characters to be printed from a 324string for 325.Cm s 326conversions. 327.It 328The optional character 329.Cm h , 330specifying that a following 331.Cm d , 332.Cm i , 333.Cm o , 334.Cm u , 335.Cm x , 336or 337.Cm X 338conversion corresponds to a 339.Em short int 340or 341.Em unsigned short int 342argument, or that a following 343.Cm n 344conversion corresponds to a pointer to a 345.Em short int 346argument. 347.It 348The optional character 349.Cm l 350(ell) specifying that a following 351.Cm d , 352.Cm i , 353.Cm o , 354.Cm u , 355.Cm x , 356or 357.Cm X 358conversion applies to a pointer to a 359.Em long int 360or 361.Em unsigned long int 362argument, or that a following 363.Cm n 364conversion corresponds to a pointer to a 365.Em long int 366argument. 367.It 368The optional character 369.Cm q , 370specifying that a following 371.Cm d , 372.Cm i , 373.Cm o , 374.Cm u , 375.Cm x , 376or 377.Cm X 378conversion corresponds to a 379.Em quad int 380or 381.Em unsigned quad int 382argument, or that a following 383.Cm n 384conversion corresponds to a pointer to a 385.Em quad int 386argument. 387.It 388The character 389.Cm L 390specifying that a following 391.Cm e , 392.Cm E , 393.Cm f , 394.Cm g , 395or 396.Cm G 397conversion corresponds to a 398.Em long double 399argument (but note that long double values are not currently supported 400by the 401.Tn VAX 402and 403.Tn Tahoe 404compilers). 405.It 406A character that specifies the type of conversion to be applied. 407.El 408.Pp 409A field width or precision, or both, may be indicated by 410an asterisk 411.Ql * 412or an asterisk followed by one or more decimal digits and a 413.Ql $ 414instead of a 415digit string. 416In this case, an 417.Em int 418argument supplies the field width or precision. 419A negative field width is treated as a left adjustment flag followed by a 420positive field width; a negative precision is treated as though it were 421missing. 422If a single format directive mixes positional (nn$) and 423non-positional arguments, the results are undefined. 424.Pp 425The conversion specifiers and their meanings are: 426.Bl -tag -width "diouxX" 427.It Cm diouxX 428The 429.Em int 430(or appropriate variant) argument is converted to signed decimal 431.Pf ( Cm d 432and 433.Cm i ) , 434unsigned octal 435.Pq Cm o , 436unsigned decimal 437.Pq Cm u , 438or unsigned hexadecimal 439.Pf ( Cm x 440and 441.Cm X ) 442notation. The letters 443.Cm abcdef 444are used for 445.Cm x 446conversions; the letters 447.Cm ABCDEF 448are used for 449.Cm X 450conversions. 451The precision, if any, gives the minimum number of digits that must 452appear; if the converted value requires fewer digits, it is padded on 453the left with zeros. 454.It Cm DOU 455The 456.Em long int 457argument is converted to signed decimal, unsigned octal, or unsigned 458decimal, as if the format had been 459.Cm ld , 460.Cm lo , 461or 462.Cm lu 463respectively. 464These conversion characters are deprecated, and will eventually disappear. 465.It Cm eE 466The 467.Em double 468argument is rounded and converted in the style 469.Sm off 470.Pf [\-]d Cm \&. No ddd Cm e No \\*(Pmdd 471.Sm on 472where there is one digit before the 473decimal-point character 474and the number of digits after it is equal to the precision; 475if the precision is missing, 476it is taken as 6; if the precision is 477zero, no decimal-point character appears. 478An 479.Cm E 480conversion uses the letter 481.Cm E 482(rather than 483.Cm e ) 484to introduce the exponent. 485The exponent always contains at least two digits; if the value is zero, 486the exponent is 00. 487.It Cm f 488The 489.Em double 490argument is rounded and converted to decimal notation in the style 491.Sm off 492.Pf [-]ddd Cm \&. No ddd , 493.Sm on 494where the number of digits after the decimal-point character 495is equal to the precision specification. 496If the precision is missing, it is taken as 6; if the precision is 497explicitly zero, no decimal-point character appears. 498If a decimal point appears, at least one digit appears before it. 499.It Cm g 500The 501.Em double 502argument is converted in style 503.Cm f 504or 505.Cm e 506(or 507.Cm E 508for 509.Cm G 510conversions). 511The precision specifies the number of significant digits. 512If the precision is missing, 6 digits are given; if the precision is zero, 513it is treated as 1. 514Style 515.Cm e 516is used if the exponent from its conversion is less than -4 or greater than 517or equal to the precision. 518Trailing zeros are removed from the fractional part of the result; a 519decimal point appears only if it is followed by at least one digit. 520.It Cm c 521The 522.Em int 523argument is converted to an 524.Em unsigned char , 525and the resulting character is written. 526.It Cm s 527The 528.Dq Em char * 529argument is expected to be a pointer to an array of character type (pointer 530to a string). 531Characters from the array are written up to (but not including) 532a terminating 533.Dv NUL 534character; 535if a precision is specified, no more than the number specified are 536written. 537If a precision is given, no null character 538need be present; if the precision is not specified, or is greater than 539the size of the array, the array must contain a terminating 540.Dv NUL 541character. 542.It Cm p 543The 544.Dq Em void * 545pointer argument is printed in hexadecimal (as if by 546.Ql %#x 547or 548.Ql %#lx ) . 549.It Cm n 550The number of characters written so far is stored into the 551integer indicated by the 552.Dq Em int * 553(or variant) pointer argument. 554No argument is converted. 555.It Cm % 556A 557.Ql % 558is written. No argument is converted. The complete conversion specification 559is 560.Ql %% . 561.El 562.Pp 563In no case does a non-existent or small field width cause truncation of 564a field; if the result of a conversion is wider than the field width, the 565field is expanded to contain the conversion result. 566.Pp 567.Sh EXAMPLES 568.br 569To print a date and time in the form `Sunday, July 3, 10:02', 570where 571.Em weekday 572and 573.Em month 574are pointers to strings: 575.Bd -literal -offset indent 576#include <stdio.h> 577 578fprintf(stdout, "%s, %s %d, %.2d:%.2d\en", 579 weekday, month, day, hour, min); 580.Ed 581.Pp 582To print \*(Pi 583to five decimal places: 584.Bd -literal -offset indent 585#include <math.h> 586#include <stdio.h> 587 588fprintf(stdout, "pi = %.5f\en", 4 * atan(1.0)); 589.Ed 590.Pp 591To allocate a 128 byte string and print into it: 592.Bd -literal -offset indent 593#include <stdio.h> 594#include <stdlib.h> 595#include <stdarg.h> 596 597char *newfmt(const char *fmt, ...) 598{ 599 char *p; 600 va_list ap; 601 602 if ((p = malloc(128)) == NULL) 603 return (NULL); 604 va_start(ap, fmt); 605 (void) vsnprintf(p, 128, fmt, ap); 606 va_end(ap); 607 return (p); 608} 609.Ed 610.Sh SEE ALSO 611.Xr printf 1 , 612.Xr scanf 3 613.Sh STANDARDS 614The 615.Fn fprintf , 616.Fn printf , 617.Fn sprintf , 618.Fn vprintf , 619.Fn vfprintf , 620and 621.Fn vsprintf 622functions 623conform to 624.St -ansiC . 625.Sh HISTORY 626The functions 627.Fn snprintf 628and 629.Fn vsnprintf 630first appeared in 631.Bx 4.4 . 632.Pp 633The functions 634.Fn asprintf 635and 636.Fn vasprintf 637first appeared in the GNU C library. This implementation first appeared in 638.Ox 2.3 . 639.Sh BUGS 640The conversion formats 641.Cm \&%D , 642.Cm \&%O , 643and 644.Cm %U 645are not standard and 646are provided only for backward compatibility. 647The effect of padding the 648.Cm %p 649format with zeros (either by the 650.Sq Cm 0 651flag or by specifying a precision), and the benign effect (i.e., none) 652of the 653.Sq Cm # 654flag on 655.Cm %n 656and 657.Cm %p 658conversions, as well as other 659nonsensical combinations such as 660.Cm %Ld , 661are not standard; such combinations 662should be avoided. 663.Pp 664Because 665.Fn sprintf 666and 667.Fn vsprintf 668assume an infinitely long string, 669callers must be careful not to overflow the actual space; 670this is often impossible to assure. 671For safety, programmers should use the 672.Fn snprintf 673and 674.Fn asprintf 675family of interfaces instead. 676Unfortunately, the 677.Fn snprintf 678interface is not available on older 679systems and the 680.Fn asprintf 681interface is not portable. 682.Pp 683There is no way for printf to know the size of each argument passed. If 684you use positional arguments you must ensure that all parameters, up to the 685last positionally-specified parameter, are used in the format string. This 686allows for the format string to be parsed for this information. Failure 687to do this will mean your code is non-portable and liable to fail. 688