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), 122except for 123.Fn snprintf 124and 125.Fn vsnprintf , 126which return the number of characters that would have been printed if the 127.Fa size 128were unlimited 129.Po 130again, not including the final 131.Ql \e0 132.Pc . 133.Pp 134.Fn Asprintf 135and 136.Fn vasprintf 137set 138.Fa *ret 139to be a pointer to a buffer sufficiently large to hold the formatted string. 140This pointer should be passed to 141.Xr free 3 142to release the allocated storage when it is no longer needed. 143If sufficient space cannot be allocated, 144.Fn asprintf 145and 146.Fn vasprintf 147will return -1 and set 148.Fa ret 149to be a NULL pointer. 150.Pp 151.Fn Snprintf 152and 153.Fn vsnprintf 154will write at most 155.Fa size Ns \-1 156of the characters printed into the output string 157(the 158.Fa size Ns 'th 159character then gets the terminating 160.Ql \e0 ) ; 161if the return value is greater than or equal to the 162.Fa size 163argument, the string was too short 164and some of the printed characters were discarded. 165.Pp 166.Fn Sprintf 167and 168.Fn vsprintf 169effectively assume an infinite 170.Fa size . 171.Pp 172The format string is composed of zero or more directives: 173ordinary 174.\" multibyte 175characters (not 176.Cm % ) , 177which are copied unchanged to the output stream; 178and conversion specifications, each of which results 179in fetching zero or more subsequent arguments. 180Each conversion specification is introduced by 181the character 182.Cm % . 183The arguments must correspond properly (after type promotion) 184with the conversion specifier. 185After the 186.Cm % , 187the following appear in sequence: 188.Bl -bullet 189.It 190An optional field, consisting of a decimal digit string followed by a 191.Cm $ , 192specifying the next argument to access . 193If this field is not provided, the argument following the last 194argument accessed will be used. 195Arguments are numbered starting at 196.Cm 1 . 197If unaccessed arguments in the format string are interspersed with ones that 198are accessed the results will be indeterminate. 199.It 200Zero or more of the following flags: 201.Bl -hyphen 202.It 203A 204.Cm # 205character 206specifying that the value should be converted to an ``alternate form''. 207For 208.Cm c , 209.Cm d , 210.Cm i , 211.Cm n , 212.Cm p , 213.Cm s , 214and 215.Cm u , 216conversions, this option has no effect. 217For 218.Cm o 219conversions, the precision of the number is increased to force the first 220character of the output string to a zero (except if a zero value is printed 221with an explicit precision of zero). 222For 223.Cm x 224and 225.Cm X 226conversions, a non-zero result has the string 227.Ql 0x 228(or 229.Ql 0X 230for 231.Cm X 232conversions) prepended to it. 233For 234.Cm e , 235.Cm E , 236.Cm f , 237.Cm g , 238and 239.Cm G , 240conversions, the result will always contain a decimal point, even if no 241digits follow it (normally, a decimal point appears in the results of 242those conversions only if a digit follows). 243For 244.Cm g 245and 246.Cm G 247conversions, trailing zeros are not removed from the result as they 248would otherwise be. 249.It 250A zero 251.Sq Cm \&0 252character specifying zero padding. 253For all conversions except 254.Cm n , 255the converted value is padded on the left with zeros rather than blanks. 256If a precision is given with a numeric conversion 257.Pf ( Cm d , 258.Cm i , 259.Cm o , 260.Cm u , 261.Cm i , 262.Cm x , 263and 264.Cm X ) , 265the 266.Sq Cm \&0 267flag is ignored. 268.It 269A negative field width flag 270.Sq Cm \- 271indicates the converted value is to be left adjusted on the field boundary. 272Except for 273.Cm n 274conversions, the converted value is padded on the right with blanks, 275rather than on the left with blanks or zeros. 276A 277.Sq Cm \- 278overrides a 279.Sq Cm \&0 280if both are given. 281.It 282A space, specifying that a blank should be left before a positive number 283produced by a signed conversion 284.Pf ( Cm d , 285.Cm e , 286.Cm E , 287.Cm f , 288.Cm g , 289.Cm G , 290or 291.Cm i ) . 292.It 293A 294.Sq Cm + 295character specifying that a sign always be placed before a 296number produced by a signed conversion. 297A 298.Sq Cm + 299overrides a space if both are used. 300.El 301.It 302An optional decimal digit string specifying a minimum field width. 303If the converted value has fewer characters than the field width, it will 304be padded with spaces on the left (or right, if the left-adjustment 305flag has been given) to fill out 306the field width. 307.It 308An optional precision, in the form of a period 309.Sq Cm .\& 310followed by an 311optional digit string. If the digit string is omitted, the precision 312is taken as zero. This gives the minimum number of digits to appear for 313.Cm d , 314.Cm i , 315.Cm o , 316.Cm u , 317.Cm x , 318and 319.Cm X 320conversions, the number of digits to appear after the decimal-point for 321.Cm e , 322.Cm E , 323and 324.Cm f 325conversions, the maximum number of significant digits for 326.Cm g 327and 328.Cm G 329conversions, or the maximum number of characters to be printed from a 330string for 331.Cm s 332conversions. 333.It 334The optional character 335.Cm h , 336specifying that a following 337.Cm d , 338.Cm i , 339.Cm o , 340.Cm u , 341.Cm x , 342or 343.Cm X 344conversion corresponds to a 345.Em short int 346or 347.Em unsigned short int 348argument, or that a following 349.Cm n 350conversion corresponds to a pointer to a 351.Em short int 352argument. 353.It 354The optional character 355.Cm l 356(ell) specifying that a following 357.Cm d , 358.Cm i , 359.Cm o , 360.Cm u , 361.Cm x , 362or 363.Cm X 364conversion applies to a pointer to a 365.Em long int 366or 367.Em unsigned long int 368argument, or that a following 369.Cm n 370conversion corresponds to a pointer to a 371.Em long int 372argument. 373.It 374The optional character 375.Cm q , 376specifying that a following 377.Cm d , 378.Cm i , 379.Cm o , 380.Cm u , 381.Cm x , 382or 383.Cm X 384conversion corresponds to a 385.Em quad int 386or 387.Em unsigned quad int 388argument, or that a following 389.Cm n 390conversion corresponds to a pointer to a 391.Em quad int 392argument. 393.It 394The character 395.Cm L 396specifying that a following 397.Cm e , 398.Cm E , 399.Cm f , 400.Cm g , 401or 402.Cm G 403conversion corresponds to a 404.Em long double 405argument. 406.It 407A character that specifies the type of conversion to be applied. 408.El 409.Pp 410A field width or precision, or both, may be indicated by 411an asterisk 412.Ql * 413or an asterisk followed by one or more decimal digits and a 414.Ql $ 415instead of a 416digit string. 417In this case, an 418.Em int 419argument supplies the field width or precision. 420A negative field width is treated as a left adjustment flag followed by a 421positive field width; a negative precision is treated as though it were 422missing. 423If a single format directive mixes positional (nn$) 424and non-positional arguments, the results are undefined. 425.Pp 426The conversion specifiers and their meanings are: 427.Bl -tag -width "diouxX" 428.It Cm diouxX 429The 430.Em int 431(or appropriate variant) argument is converted to signed decimal 432.Pf ( Cm d 433and 434.Cm i ) , 435unsigned octal 436.Pq Cm o , 437unsigned decimal 438.Pq Cm u , 439or unsigned hexadecimal 440.Pf ( Cm x 441and 442.Cm X ) 443notation. The letters 444.Cm abcdef 445are used for 446.Cm x 447conversions; the letters 448.Cm ABCDEF 449are used for 450.Cm X 451conversions. 452The precision, if any, gives the minimum number of digits that must 453appear; if the converted value requires fewer digits, it is padded on 454the left with zeros. 455.It Cm DOU 456The 457.Em long int 458argument is converted to signed decimal, unsigned octal, or unsigned 459decimal, as if the format had been 460.Cm ld , 461.Cm lo , 462or 463.Cm lu 464respectively. 465These conversion characters are deprecated, and will eventually disappear. 466.It Cm eE 467The 468.Em double 469argument is rounded and converted in the style 470.Sm off 471.Pf [\-]d Cm \&. No ddd Cm e No \\*(Pmdd 472.Sm on 473where there is one digit before the 474decimal-point character 475and the number of digits after it is equal to the precision; 476if the precision is missing, 477it is taken as 6; if the precision is 478zero, no decimal-point character appears. 479An 480.Cm E 481conversion uses the letter 482.Cm E 483(rather than 484.Cm e ) 485to introduce the exponent. 486The exponent always contains at least two digits; if the value is zero, 487the exponent is 00. 488.It Cm f 489The 490.Em double 491argument is rounded and converted to decimal notation in the style 492.Sm off 493.Pf [-]ddd Cm \&. No ddd , 494.Sm on 495where the number of digits after the decimal-point character 496is equal to the precision specification. 497If the precision is missing, it is taken as 6; if the precision is 498explicitly zero, no decimal-point character appears. 499If a decimal point appears, at least one digit appears before it. 500.It Cm gG 501The 502.Em double 503argument is converted in style 504.Cm f 505or 506.Cm e 507(or 508.Cm E 509for 510.Cm G 511conversions). 512The precision specifies the number of significant digits. 513If the precision is missing, 6 digits are given; if the precision is zero, 514it is treated as 1. 515Style 516.Cm e 517is used if the exponent from its conversion is less than -4 or greater than 518or equal to the precision. 519Trailing zeros are removed from the fractional part of the result; a 520decimal point appears only if it is followed by at least one digit. 521.It Cm c 522The 523.Em int 524argument is converted to an 525.Em unsigned char , 526and the resulting character is written. 527.It Cm s 528The 529.Dq Em char * 530argument is expected to be a pointer to an array of character type (pointer 531to a string). 532Characters from the array are written up to (but not including) 533a terminating 534.Dv NUL 535character; 536if a precision is specified, no more than the number specified are 537written. 538If a precision is given, no null character 539need be present; if the precision is not specified, or is greater than 540the size of the array, the array must contain a terminating 541.Dv NUL 542character. 543.It Cm p 544The 545.Dq Em void * 546pointer argument is printed in hexadecimal (as if by 547.Ql %#x 548or 549.Ql %#lx ) . 550.It Cm n 551The number of characters written so far is stored into the 552integer indicated by the 553.Dq Em int * 554(or variant) pointer argument. 555No argument is converted. 556.It Cm % 557A 558.Ql % 559is written. 560No argument is converted. 561The complete conversion specification 562is 563.Ql %% . 564.El 565.Pp 566The decimal point 567character is defined in the program's locale (category 568.Dv LC_NUMERIC ) . 569.Pp 570In no case does a non-existent or small field width cause truncation of 571a field; if the result of a conversion is wider than the field width, the 572field is expanded to contain the conversion result. 573.Pp 574.Sh EXAMPLES 575To print a date and time in the form `Sunday, July 3, 10:02', 576where 577.Em weekday 578and 579.Em month 580are pointers to strings: 581.Bd -literal -offset indent 582#include <stdio.h> 583fprintf(stdout, "%s, %s %d, %.2d:%.2d\en", 584 weekday, month, day, hour, min); 585.Ed 586.Pp 587To print \*(Pi 588to five decimal places: 589.Bd -literal -offset indent 590#include <math.h> 591#include <stdio.h> 592fprintf(stdout, "pi = %.5f\en", 4 * atan(1.0)); 593.Ed 594.Pp 595To allocate a 128 byte string and print into it: 596.Bd -literal -offset indent 597#include <stdio.h> 598#include <stdlib.h> 599#include <stdarg.h> 600char *newfmt(const char *fmt, ...) 601{ 602 char *p; 603 va_list ap; 604 if ((p = malloc(128)) == NULL) 605 return (NULL); 606 va_start(ap, fmt); 607 (void) vsnprintf(p, 128, fmt, ap); 608 va_end(ap); 609 return (p); 610} 611.Ed 612.Sh SEE ALSO 613.Xr printf 1 , 614.Xr scanf 3 615.Sh STANDARDS 616The 617.Fn fprintf , 618.Fn printf , 619.Fn sprintf , 620.Fn vprintf , 621.Fn vfprintf , 622and 623.Fn vsprintf 624functions 625conform to 626.St -isoC . 627.Sh HISTORY 628The functions 629.Fn asprintf 630and 631.Fn vasprintf 632first appeared in the GNU C library. 633These were implemented by 634.An Peter Wemm Aq peter@FreeBSD.org 635in 636.Fx 2.2 , 637but were later replaced with a different implementation 638from 639.An Todd C. Miller Aq Todd.Miller@courtesan.com 640for 641.Ox 2.3 . 642.Sh BUGS 643The conversion formats 644.Cm \&%D , 645.Cm \&%O , 646and 647.Cm %U 648are not standard and 649are provided only for backward compatibility. 650The effect of padding the 651.Cm %p 652format with zeros (either by the 653.Sq Cm 0 654flag or by specifying a precision), and the benign effect (i.e., none) 655of the 656.Sq Cm # 657flag on 658.Cm %n 659and 660.Cm %p 661conversions, as well as other 662nonsensical combinations such as 663.Cm %Ld , 664are not standard; such combinations 665should be avoided. 666.Pp 667Because 668.Fn sprintf 669and 670.Fn vsprintf 671assume an infinitely long string, 672callers must be careful not to overflow the actual space; 673this is often hard to assure. 674For safety, programmers should use the 675.Fn snprintf 676interface instead. 677Unfortunately, this interface is not portable. 678.Pp 679Never pass a string with user-supplied data as a format without using 680.Ql %s . 681An attacker can put format specifiers in the string to mangle your stack, 682leading to a possible security hole. 683This holds true even if the string was built using a function like 684.Fn snprintf , 685as the resulting string may still contain user-supplied conversion specifiers 686for later interpolation by 687.Fn printf . 688.Pp 689Always use the proper secure idiom: 690.Pp 691.Dl snprintf(buffer, sizeof(buffer), "%s", string); 692