1.\" $NetBSD: printf.3,v 1.7 1998/02/05 18:49:29 perry 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 vprintf , 49.Nm vfprintf, 50.Nm vsprintf , 51.Nm vsnprintf 52.Nd formatted output conversion 53.Sh LIBRARY 54.Lb libc 55.Sh SYNOPSIS 56.Fd #include <stdio.h> 57.Ft int 58.Fn printf "const char *format" ... 59.Ft int 60.Fn fprintf "FILE *stream" "const char *format" ... 61.Ft int 62.Fn sprintf "char *str" "const char *format" ... 63.Ft int 64.Fn snprintf "char *str" "size_t size" "const char *format" ... 65.\" .Ft int 66.\" .Fn smprintf "const char *format" ... 67.Fd #include <stdarg.h> 68.Ft int 69.Fn vprintf "const char *format" "va_list ap" 70.Ft int 71.Fn vfprintf "FILE *stream" "const char *format" "va_list ap" 72.Ft int 73.Fn vsprintf "char *str" "char *format" "va_list ap" 74.Ft int 75.Fn vsnprintf "char *str" "size_t size" "const char *format" "va_list ap" 76.\" .Ft int 77.\" .Fn vsmprintf "const char *format" "va_list ap" 78.Sh DESCRIPTION 79The 80.Fn printf 81family of functions produces output according to a 82.Fa format 83as described below. 84.Fn Printf 85and 86.Fn vprintf 87write output to 88.Em stdout, 89the standard output stream; 90.Fn fprintf 91and 92.Fn vfprintf 93write output to the given output 94.Fa stream ; 95.Fn sprintf , 96.Fn snprintf , 97.Fn vsprintf , 98and 99.Fn vsnprintf 100write to the character string 101.Fa str . 102.\" .IR str ; 103.\" and 104.\" .I smprintf 105.\" and 106.\" .I vsmprintf 107.\" dynamically allocate a new string with 108.\" .IR malloc . 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.\" Except for 116.\" .I smprintf 117.\" and 118.\" .IR vsmprintf , 119.\" all of these functions return 120These functions return 121the number of characters printed 122(not including the trailing 123.Ql \e0 124used to end output to strings). 125.\" .I Smprintf 126.\" and 127.\" .I vsmprintf 128.\" return a pointer to a string of an appropriate length; 129.\" this pointer should be passed to 130.\" .I free 131.\" to release the associated storage 132.\" when it is no longer needed. 133.\" If sufficient space is not avaliable, 134.\" .I smprintf 135.\" and 136.\" .I vsmprintf 137.\" will return 138.\" .SM 139.\" .BR 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.Fn Sprintf 155and 156.Fn vsprintf 157effectively assume an infinite 158.Fa size . 159.Pp 160The format string is composed of zero or more directives: 161ordinary 162.\" multibyte 163characters (not 164.Cm % ) , 165which are copied unchanged to the output stream; 166and conversion specifications, each of which results 167in fetching zero or more subsequent arguments. 168Each conversion specification is introduced by 169the character 170.Cm % . 171The arguments must correspond properly (after type promotion) 172with the conversion specifier. 173After the 174.Cm % , 175the following appear in sequence: 176.Bl -bullet 177.It 178Zero or more of the following flags: 179.Bl -hyphen 180.It 181A 182.Cm # 183character 184specifying that the value should be converted to an ``alternative form''. 185For 186.Cm c , 187.Cm d , 188.Cm i , 189.Cm n , 190.Cm p , 191.Cm s , 192and 193.Cm u , 194conversions, this option has no effect. 195For 196.Cm o 197conversions, the precision of the number is increased to force the first 198character of the output string to a zero (except if a zero value is printed 199with an explicit precision of zero). 200For 201.Cm x 202and 203.Cm X 204conversions, a non-zero result has the string 205.Ql 0x 206(or 207.Ql 0X 208for 209.Cm X 210conversions) prepended to it. 211For 212.Cm e , 213.Cm E , 214.Cm f , 215.Cm g , 216and 217.Cm G , 218conversions, the result will always contain a decimal point, even if no 219digits follow it (normally, a decimal point appears in the results of 220those conversions only if a digit follows). 221For 222.Cm g 223and 224.Cm G 225conversions, trailing zeros are not removed from the result as they 226would otherwise be. 227.It 228A zero 229.Sq Cm \&0 230character specifying zero padding. 231For all conversions except 232.Cm n , 233the converted value is padded on the left with zeros rather than blanks. 234If a precision is given with a numeric conversion 235.Pf ( Mc d , 236.Cm i , 237.Cm o , 238.Cm u , 239.Cm i , 240.Cm x , 241and 242.Cm X ) , 243the 244.Sq Cm \&0 245flag is ignored. 246.It 247A negative field width flag 248.Sq Cm \- 249indicates the converted value is to be left adjusted on the field boundary. 250Except for 251.Cm n 252conversions, the converted value is padded on the right with blanks, 253rather than on the left with blanks or zeros. 254A 255.Sq Cm \- 256overrides a 257.Sq Cm \&0 258if both are given. 259.It 260A space, specifying that a blank should be left before a positive number 261produced by a signed conversion 262.Pf ( Cm d , 263.Cm e , 264.Cm E , 265.Cm f , 266.Cm g , 267.Cm G , 268or 269.Cm i ) . 270.It 271A 272.Sq Cm + 273character specifying that a sign always be placed before a 274number produced by a signed conversion. 275A 276.Sq Cm + 277overrides a space if both are used. 278.El 279.It 280An optional decimal digit string specifying a minimum field width. 281If the converted value has fewer characters than the field width, it will 282be padded with spaces on the left (or right, if the left-adjustment 283flag has been given) to fill out 284the field width. 285.It 286An optional precision, in the form of a period 287.Sq Cm \&. 288followed by an 289optional digit string. If the digit string is omitted, the precision 290is taken as zero. This gives the minimum number of digits to appear for 291.Cm d , 292.Cm i , 293.Cm o , 294.Cm u , 295.Cm x , 296and 297.Cm X 298conversions, the number of digits to appear after the decimal-point for 299.Cm e , 300.Cm E , 301and 302.Cm f 303conversions, the maximum number of significant digits for 304.Cm g 305and 306.Cm G 307conversions, or the maximum number of characters to be printed from a 308string for 309.Cm s 310conversions. 311.It 312The optional character 313.Cm h , 314specifying that a following 315.Cm d , 316.Cm i , 317.Cm o , 318.Cm u , 319.Cm x , 320or 321.Cm X 322conversion corresponds to a 323.Em short int 324or 325.Em unsigned short int 326argument, or that a following 327.Cm n 328conversion corresponds to a pointer to a 329.Em short int 330argument. 331.It 332The optional character 333.Cm l 334(ell) specifying that a following 335.Cm d , 336.Cm i , 337.Cm o , 338.Cm u , 339.Cm x , 340or 341.Cm X 342conversion applies to a pointer to a 343.Em long int 344or 345.Em unsigned long int 346argument, or that a following 347.Cm n 348conversion corresponds to a pointer to a 349.Em long int 350argument. 351.It 352The optional character 353.Cm q , 354specifying that a following 355.Cm d , 356.Cm i , 357.Cm o , 358.Cm u , 359.Cm x , 360or 361.Cm X 362conversion corresponds to a 363.Em quad int 364or 365.Em unsigned quad int 366argument, or that a following 367.Cm n 368conversion corresponds to a pointer to a 369.Em quad int 370argument. 371.It 372The character 373.Cm L 374specifying that a following 375.Cm e , 376.Cm E , 377.Cm f , 378.Cm g , 379or 380.Cm G 381conversion corresponds to a 382.Em long double 383argument (but note that long double values are not currently supported 384by the 385.Tn VAX 386and 387.Tn Tahoe 388compilers). 389.It 390A character that specifies the type of conversion to be applied. 391.El 392.Pp 393A field width or precision, or both, may be indicated by 394an asterisk 395.Ql * 396instead of a 397digit string. 398In this case, an 399.Em int 400argument supplies the field width or precision. 401A negative field width is treated as a left adjustment flag followed by a 402positive field width; a negative precision is treated as though it were 403missing. 404.Pp 405The conversion specifiers and their meanings are: 406.Bl -tag -width "diouxX" 407.It Cm diouxX 408The 409.Em int 410(or appropriate variant) argument is converted to signed decimal 411.Pf ( Cm d 412and 413.Cm i ) , 414unsigned octal 415.Pq Cm o , 416unsigned decimal 417.Pq Cm u , 418or unsigned hexadecimal 419.Pf ( Cm x 420and 421.Cm X ) 422notation. The letters 423.Cm abcdef 424are used for 425.Cm x 426conversions; the letters 427.Cm ABCDEF 428are used for 429.Cm X 430conversions. 431The precision, if any, gives the minimum number of digits that must 432appear; if the converted value requires fewer digits, it is padded on 433the left with zeros. 434.It Cm DOU 435The 436.Em long int 437argument is converted to signed decimal, unsigned octal, or unsigned 438decimal, as if the format had been 439.Cm ld , 440.Cm lo , 441or 442.Cm lu 443respectively. 444These conversion characters are deprecated, and will eventually disappear. 445.It Cm eE 446The 447.Em double 448argument is rounded and converted in the style 449.Sm off 450.Pf [\-]d Cm \&. No ddd Cm e No \\*(Pmdd 451.Sm on 452where there is one digit before the 453decimal-point character 454and the number of digits after it is equal to the precision; 455if the precision is missing, 456it is taken as 6; if the precision is 457zero, no decimal-point character appears. 458An 459.Cm E 460conversion uses the letter 461.Cm E 462(rather than 463.Cm e ) 464to introduce the exponent. 465The exponent always contains at least two digits; if the value is zero, 466the exponent is 00. 467.It Cm f 468The 469.Em double 470argument is rounded and converted to decimal notation in the style 471.Sm off 472.Pf [-]ddd Cm \&. No ddd , 473.Sm on 474where the number of digits after the decimal-point character 475is equal to the precision specification. 476If the precision is missing, it is taken as 6; if the precision is 477explicitly zero, no decimal-point character appears. 478If a decimal point appears, at least one digit appears before it. 479.It Cm g 480The 481.Em double 482argument is converted in style 483.Cm f 484or 485.Cm e 486(or 487.Cm E 488for 489.Cm G 490conversions). 491The precision specifies the number of significant digits. 492If the precision is missing, 6 digits are given; if the precision is zero, 493it is treated as 1. 494Style 495.Cm e 496is used if the exponent from its conversion is less than -4 or greater than 497or equal to the precision. 498Trailing zeros are removed from the fractional part of the result; a 499decimal point appears only if it is followed by at least one digit. 500.It Cm c 501The 502.Em int 503argument is converted to an 504.Em unsigned char , 505and the resulting character is written. 506.It Cm s 507The 508.Dq Em char * 509argument is expected to be a pointer to an array of character type (pointer 510to a string). 511Characters from the array are written up to (but not including) 512a terminating 513.Dv NUL 514character; 515if a precision is specified, no more than the number specified are 516written. 517If a precision is given, no null character 518need be present; if the precision is not specified, or is greater than 519the size of the array, the array must contain a terminating 520.Dv NUL 521character. 522.It Cm p 523The 524.Dq Em void * 525pointer argument is printed in hexadecimal (as if by 526.Ql %#x 527or 528.Ql %#lx ) . 529.It Cm n 530The number of characters written so far is stored into the 531integer indicated by the 532.Dq Em int * 533(or variant) pointer argument. 534No argument is converted. 535.It Cm % 536A 537.Ql % 538is written. No argument is converted. The complete conversion specification 539is 540.Ql %% . 541.El 542.Pp 543In no case does a non-existent or small field width cause truncation of 544a field; if the result of a conversion is wider than the field width, the 545field is expanded to contain the conversion result. 546.Pp 547.Sh EXAMPLES 548.br 549To print a date and time in the form `Sunday, July 3, 10:02', 550where 551.Em weekday 552and 553.Em month 554are pointers to strings: 555.Bd -literal -offset indent 556#include <stdio.h> 557fprintf(stdout, "%s, %s %d, %.2d:%.2d\en", 558 weekday, month, day, hour, min); 559.Ed 560.Pp 561To print \*(Pi 562to five decimal places: 563.Bd -literal -offset indent 564#include <math.h> 565#include <stdio.h> 566fprintf(stdout, "pi = %.5f\en", 4 * atan(1.0)); 567.Ed 568.Pp 569To allocate a 128 byte string and print into it: 570.Bd -literal -offset indent 571#include <stdio.h> 572#include <stdlib.h> 573#include <stdarg.h> 574char *newfmt(const char *fmt, ...) 575{ 576 char *p; 577 va_list ap; 578 if ((p = malloc(128)) == NULL) 579 return (NULL); 580 va_start(ap, fmt); 581 (void) vsnprintf(p, 128, fmt, ap); 582 va_end(ap); 583 return (p); 584} 585.Ed 586.Sh SEE ALSO 587.Xr printf 1 , 588.Xr scanf 3 589.Sh STANDARDS 590The 591.Fn fprintf , 592.Fn printf , 593.Fn sprintf , 594.Fn vprintf , 595.Fn vfprintf , 596and 597.Fn vsprintf 598functions 599conform to 600.St -ansiC . 601.Sh HISTORY 602The functions 603.Fn snprintf 604and 605.Fn vsnprintf 606are new to this release. 607.Sh BUGS 608The conversion formats 609.Cm \&%D , 610.Cm \&%O , 611and 612.Cm %U 613are not standard and 614are provided only for backward compatibility. 615The effect of padding the 616.Cm %p 617format with zeros (either by the 618.Sq Cm 0 619flag or by specifying a precision), and the benign effect (i.e., none) 620of the 621.Sq Cm # 622flag on 623.Cm %n 624and 625.Cm %p 626conversions, as well as other 627nonsensical combinations such as 628.Cm %Ld , 629are not standard; such combinations 630should be avoided. 631.Pp 632Because 633.Fn sprintf 634and 635.Fn vsprintf 636assume an infinitely long string, 637callers must be careful not to overflow the actual space; 638this is often impossible to assure. 639For safety, programmers should use the 640.Fn snprintf 641interface instead. 642Unfortunately, this interface is not portable. 643