1.\" Copyright (c) 1990, 1991, 1993 2.\" The Regents of the University of California. All rights reserved. 3.\" 4.\" This code is derived from software contributed to Berkeley by 5.\" Chris Torek and the American National Standards Committee X3, 6.\" on Information Processing Systems. 7.\" 8.\" Redistribution and use in source and binary forms, with or without 9.\" modification, are permitted provided that the following conditions 10.\" are met: 11.\" 1. Redistributions of source code must retain the above copyright 12.\" notice, this list of conditions and the following disclaimer. 13.\" 2. Redistributions in binary form must reproduce the above copyright 14.\" notice, this list of conditions and the following disclaimer in the 15.\" documentation and/or other materials provided with the distribution. 16.\" 3. All advertising materials mentioning features or use of this software 17.\" must display the following acknowledgement: 18.\" This product includes software developed by the University of 19.\" California, Berkeley and its contributors. 20.\" 4. Neither the name of the University nor the names of its contributors 21.\" may be used to endorse or promote products derived from this software 22.\" without specific prior written permission. 23.\" 24.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34.\" SUCH DAMAGE. 35.\" 36.\" @(#)printf.3 8.1 (Berkeley) 6/4/93 37.\" $FreeBSD$ 38.\" 39.Dd June 4, 1993 40.Dt PRINTF 3 41.Os 42.Sh NAME 43.Nm printf , 44.Nm fprintf , 45.Nm sprintf , 46.Nm snprintf , 47.Nm asprintf , 48.Nm vprintf , 49.Nm vfprintf, 50.Nm vsprintf , 51.Nm vsnprintf , 52.Nm vasprintf 53.Nd formatted output conversion 54.Sh LIBRARY 55.Lb libc 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 ; 103and 104.Fn asprintf 105and 106.Fn vasprintf 107dynamically allocate a new string with 108.Xr malloc 3 . 109.Pp 110These functions write the output under the control of a 111.Fa format 112string that specifies how subsequent arguments 113(or arguments accessed via the variable-length argument facilities of 114.Xr stdarg 3 ) 115are converted for output. 116.Pp 117These functions return 118the number of characters printed 119(not including the trailing 120.Ql \e0 121used to end output to strings). 122.Pp 123.Fn Asprintf 124and 125.Fn vasprintf 126set 127.Fa *ret 128to be a pointer to a buffer sufficiently large to hold the formatted string. 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, 133.Fn asprintf 134and 135.Fn vasprintf 136will return -1 and set 137.Fa ret 138to be a NULL pointer. 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. 154.Pp 155.Fn Sprintf 156and 157.Fn vsprintf 158effectively assume an infinite 159.Fa size . 160.Pp 161The format string is composed of zero or more directives: 162ordinary 163.\" multibyte 164characters (not 165.Cm % ) , 166which are copied unchanged to the output stream; 167and conversion specifications, each of which results 168in fetching zero or more subsequent arguments. 169Each conversion specification is introduced by 170the character 171.Cm % . 172The arguments must correspond properly (after type promotion) 173with the conversion specifier. 174After the 175.Cm % , 176the following appear in sequence: 177.Bl -bullet 178.It 179An optional field, consisting of a decimal digit string followed by a 180.Cm $ , 181specifying the next argument to access . 182If this field is not provided, the argument following the last 183argument accessed will be used. 184Arguments are numbered starting at 185.Cm 1 . 186If unaccessed arguments in the format string are interspersed with ones that 187are accessed the results will be indeterminate. 188.It 189Zero or more of the following flags: 190.Bl -hyphen 191.It 192A 193.Cm # 194character 195specifying that the value should be converted to an ``alternate form''. 196For 197.Cm c , 198.Cm d , 199.Cm i , 200.Cm n , 201.Cm p , 202.Cm s , 203and 204.Cm u , 205conversions, this option has no effect. 206For 207.Cm o 208conversions, the precision of the number is increased to force the first 209character of the output string to a zero (except if a zero value is printed 210with an explicit precision of zero). 211For 212.Cm x 213and 214.Cm X 215conversions, a non-zero result has the string 216.Ql 0x 217(or 218.Ql 0X 219for 220.Cm X 221conversions) prepended to it. 222For 223.Cm e , 224.Cm E , 225.Cm f , 226.Cm g , 227and 228.Cm G , 229conversions, the result will always contain a decimal point, even if no 230digits follow it (normally, a decimal point appears in the results of 231those conversions only if a digit follows). 232For 233.Cm g 234and 235.Cm G 236conversions, trailing zeros are not removed from the result as they 237would otherwise be. 238.It 239A zero 240.Sq Cm \&0 241character specifying zero padding. 242For all conversions except 243.Cm n , 244the converted value is padded on the left with zeros rather than blanks. 245If a precision is given with a numeric conversion 246.Pf ( Cm d , 247.Cm i , 248.Cm o , 249.Cm u , 250.Cm i , 251.Cm x , 252and 253.Cm X ) , 254the 255.Sq Cm \&0 256flag is ignored. 257.It 258A negative field width flag 259.Sq Cm \- 260indicates the converted value is to be left adjusted on the field boundary. 261Except for 262.Cm n 263conversions, the converted value is padded on the right with blanks, 264rather than on the left with blanks or zeros. 265A 266.Sq Cm \- 267overrides a 268.Sq Cm \&0 269if both are given. 270.It 271A space, specifying that a blank should be left before a positive number 272produced by a signed conversion 273.Pf ( Cm d , 274.Cm e , 275.Cm E , 276.Cm f , 277.Cm g , 278.Cm G , 279or 280.Cm i ) . 281.It 282A 283.Sq Cm + 284character specifying that a sign always be placed before a 285number produced by a signed conversion. 286A 287.Sq Cm + 288overrides a space if both are used. 289.El 290.It 291An optional decimal digit string specifying a minimum field width. 292If the converted value has fewer characters than the field width, it will 293be padded with spaces on the left (or right, if the left-adjustment 294flag has been given) to fill out 295the field width. 296.It 297An optional precision, in the form of a period 298.Sq Cm .\& 299followed by an 300optional digit string. If the digit string is omitted, the precision 301is taken as zero. This gives the minimum number of digits to appear for 302.Cm d , 303.Cm i , 304.Cm o , 305.Cm u , 306.Cm x , 307and 308.Cm X 309conversions, the number of digits to appear after the decimal-point for 310.Cm e , 311.Cm E , 312and 313.Cm f 314conversions, the maximum number of significant digits for 315.Cm g 316and 317.Cm G 318conversions, or the maximum number of characters to be printed from a 319string for 320.Cm s 321conversions. 322.It 323The optional character 324.Cm h , 325specifying that a following 326.Cm d , 327.Cm i , 328.Cm o , 329.Cm u , 330.Cm x , 331or 332.Cm X 333conversion corresponds to a 334.Em short int 335or 336.Em unsigned short int 337argument, or that a following 338.Cm n 339conversion corresponds to a pointer to a 340.Em short int 341argument. 342.It 343The optional character 344.Cm l 345(ell) specifying that a following 346.Cm d , 347.Cm i , 348.Cm o , 349.Cm u , 350.Cm x , 351or 352.Cm X 353conversion applies to a pointer to a 354.Em long int 355or 356.Em unsigned long int 357argument, or that a following 358.Cm n 359conversion corresponds to a pointer to a 360.Em long int 361argument. 362.It 363The optional character 364.Cm q , 365specifying that a following 366.Cm d , 367.Cm i , 368.Cm o , 369.Cm u , 370.Cm x , 371or 372.Cm X 373conversion corresponds to a 374.Em quad int 375or 376.Em unsigned quad int 377argument, or that a following 378.Cm n 379conversion corresponds to a pointer to a 380.Em quad int 381argument. 382.It 383The character 384.Cm L 385specifying that a following 386.Cm e , 387.Cm E , 388.Cm f , 389.Cm g , 390or 391.Cm G 392conversion corresponds to a 393.Em long double 394argument. 395.It 396A character that specifies the type of conversion to be applied. 397.El 398.Pp 399A field width or precision, or both, may be indicated by 400an asterisk 401.Ql * 402or an asterisk followed by one or more decimal digits and a 403.Ql $ 404instead of a 405digit string. 406In this case, an 407.Em int 408argument supplies the field width or precision. 409A negative field width is treated as a left adjustment flag followed by a 410positive field width; a negative precision is treated as though it were 411missing. 412If a single format directive mixes positional (nn$) 413and non-positional arguments, the results are undefined. 414.Pp 415The conversion specifiers and their meanings are: 416.Bl -tag -width "diouxX" 417.It Cm diouxX 418The 419.Em int 420(or appropriate variant) argument is converted to signed decimal 421.Pf ( Cm d 422and 423.Cm i ) , 424unsigned octal 425.Pq Cm o , 426unsigned decimal 427.Pq Cm u , 428or unsigned hexadecimal 429.Pf ( Cm x 430and 431.Cm X ) 432notation. The letters 433.Cm abcdef 434are used for 435.Cm x 436conversions; the letters 437.Cm ABCDEF 438are used for 439.Cm X 440conversions. 441The precision, if any, gives the minimum number of digits that must 442appear; if the converted value requires fewer digits, it is padded on 443the left with zeros. 444.It Cm DOU 445The 446.Em long int 447argument is converted to signed decimal, unsigned octal, or unsigned 448decimal, as if the format had been 449.Cm ld , 450.Cm lo , 451or 452.Cm lu 453respectively. 454These conversion characters are deprecated, and will eventually disappear. 455.It Cm eE 456The 457.Em double 458argument is rounded and converted in the style 459.Sm off 460.Pf [\-]d Cm \&. No ddd Cm e No \\*(Pmdd 461.Sm on 462where there is one digit before the 463decimal-point character 464and the number of digits after it is equal to the precision; 465if the precision is missing, 466it is taken as 6; if the precision is 467zero, no decimal-point character appears. 468An 469.Cm E 470conversion uses the letter 471.Cm E 472(rather than 473.Cm e ) 474to introduce the exponent. 475The exponent always contains at least two digits; if the value is zero, 476the exponent is 00. 477.It Cm f 478The 479.Em double 480argument is rounded and converted to decimal notation in the style 481.Sm off 482.Pf [-]ddd Cm \&. No ddd , 483.Sm on 484where the number of digits after the decimal-point character 485is equal to the precision specification. 486If the precision is missing, it is taken as 6; if the precision is 487explicitly zero, no decimal-point character appears. 488If a decimal point appears, at least one digit appears before it. 489.It Cm gG 490The 491.Em double 492argument is converted in style 493.Cm f 494or 495.Cm e 496(or 497.Cm E 498for 499.Cm G 500conversions). 501The precision specifies the number of significant digits. 502If the precision is missing, 6 digits are given; if the precision is zero, 503it is treated as 1. 504Style 505.Cm e 506is used if the exponent from its conversion is less than -4 or greater than 507or equal to the precision. 508Trailing zeros are removed from the fractional part of the result; a 509decimal point appears only if it is followed by at least one digit. 510.It Cm c 511The 512.Em int 513argument is converted to an 514.Em unsigned char , 515and the resulting character is written. 516.It Cm s 517The 518.Dq Em char * 519argument is expected to be a pointer to an array of character type (pointer 520to a string). 521Characters from the array are written up to (but not including) 522a terminating 523.Dv NUL 524character; 525if a precision is specified, no more than the number specified are 526written. 527If a precision is given, no null character 528need be present; if the precision is not specified, or is greater than 529the size of the array, the array must contain a terminating 530.Dv NUL 531character. 532.It Cm p 533The 534.Dq Em void * 535pointer argument is printed in hexadecimal (as if by 536.Ql %#x 537or 538.Ql %#lx ) . 539.It Cm n 540The number of characters written so far is stored into the 541integer indicated by the 542.Dq Em int * 543(or variant) pointer argument. 544No argument is converted. 545.It Cm % 546A 547.Ql % 548is written. 549No argument is converted. 550The complete conversion specification 551is 552.Ql %% . 553.El 554.Pp 555In no case does a non-existent or small field width cause truncation of 556a field; if the result of a conversion is wider than the field width, the 557field is expanded to contain the conversion result. 558.Pp 559.Sh EXAMPLES 560To print a date and time in the form `Sunday, July 3, 10:02', 561where 562.Em weekday 563and 564.Em month 565are pointers to strings: 566.Bd -literal -offset indent 567#include <stdio.h> 568fprintf(stdout, "%s, %s %d, %.2d:%.2d\en", 569 weekday, month, day, hour, min); 570.Ed 571.Pp 572To print \*(Pi 573to five decimal places: 574.Bd -literal -offset indent 575#include <math.h> 576#include <stdio.h> 577fprintf(stdout, "pi = %.5f\en", 4 * atan(1.0)); 578.Ed 579.Pp 580To allocate a 128 byte string and print into it: 581.Bd -literal -offset indent 582#include <stdio.h> 583#include <stdlib.h> 584#include <stdarg.h> 585char *newfmt(const char *fmt, ...) 586{ 587 char *p; 588 va_list ap; 589 if ((p = malloc(128)) == NULL) 590 return (NULL); 591 va_start(ap, fmt); 592 (void) vsnprintf(p, 128, fmt, ap); 593 va_end(ap); 594 return (p); 595} 596.Ed 597.Sh SEE ALSO 598.Xr printf 1 , 599.Xr scanf 3 600.Sh STANDARDS 601The 602.Fn fprintf , 603.Fn printf , 604.Fn sprintf , 605.Fn vprintf , 606.Fn vfprintf , 607and 608.Fn vsprintf 609functions 610conform to 611.St -ansiC . 612.Sh HISTORY 613The functions 614.Fn asprintf 615and 616.Fn vasprintf 617first appeared in the GNU C library. 618These were implemented by Peter Wemm <peter@FreeBSD.org> in 619.Fx 2.2 , 620but were later replaced with a different implementation 621from Todd C. Miller <Todd.Miller@courtesan.com> for 622.Ox 2.3 . 623.Sh BUGS 624The conversion formats 625.Cm \&%D , 626.Cm \&%O , 627and 628.Cm %U 629are not standard and 630are provided only for backward compatibility. 631The effect of padding the 632.Cm %p 633format with zeros (either by the 634.Sq Cm 0 635flag or by specifying a precision), and the benign effect (i.e., none) 636of the 637.Sq Cm # 638flag on 639.Cm %n 640and 641.Cm %p 642conversions, as well as other 643nonsensical combinations such as 644.Cm %Ld , 645are not standard; such combinations 646should be avoided. 647.Pp 648Because 649.Fn sprintf 650and 651.Fn vsprintf 652assume an infinitely long string, 653callers must be careful not to overflow the actual space; 654this is often hard to assure. 655For safety, programmers should use the 656.Fn snprintf 657interface instead. 658Unfortunately, this interface is not portable. 659