1*48351Scael.\" Copyright (c) 1990, 1991 The Regents of the University of California. 246094Sbostic.\" All rights reserved. 320358Smckusick.\" 446094Sbostic.\" This code is derived from software contributed to Berkeley by 546094Sbostic.\" Chris Torek. 646094Sbostic.\" %sccs.include.redist.man% 746094Sbostic.\" 8*48351Scael.\" @(#)printf.3 6.11 (Berkeley) 04/19/91 946094Sbostic.\" 10*48351Scael.Dd 11*48351Scael.Dt PRINTF 3 12*48351Scael.Os 13*48351Scael.Sh NAME 14*48351Scael.Nm printf , 15*48351Scael.Nm fprintf , 16*48351Scael.Nm sprintf , 17*48351Scael.Nm snprintf , 18*48351Scael.Nm vprintf , 19*48351Scael.Nm vfprintf, 20*48351Scael.Nm vsprintf , 21*48351Scael.Nm vsnprintf 22*48351Scael.Nd formatted output conversion 23*48351Scael.Sh SYNOPSIS 24*48351Scael.Fd #include <stdio.h> 25*48351Scael.Ft int 26*48351Scael.Fn printf "const char *format" ... 27*48351Scael.Ft int 28*48351Scael.Fn fprintf "FILE *stream" "const char *format" ... 29*48351Scael.Ft int 30*48351Scael.Fn sprintf "char *str" "const char *format" ... 31*48351Scael.Ft int 32*48351Scael.Fn snprintf "char *str" "size_t size" "const char *format" ... 33*48351Scael.\" .Ft int 34*48351Scael.\" .Fn smprintf "const char *format" ... 35*48351Scael.Fd #include <varargs.h> 36*48351Scael.Ft int 37*48351Scael.Fn vprintf "const char *format" "va_list ap" 38*48351Scael.Ft int 39*48351Scael.Fn vfprintf "FILE *stream" "const char *format" "va_list ap" 40*48351Scael.Ft int 41*48351Scael.Fn vsprintf "char *str" "char *format" "va_list ap" 42*48351Scael.Ft int 43*48351Scael.Fn vsnprintf "char *str" "size_t size" "const char *format" "va_list ap" 44*48351Scael.\" .Ft int 45*48351Scael.\" .Fn vsmprintf "const char *format" "va_list ap" 46*48351Scael.Sh DESCRIPTION 4746094SbosticThe 48*48351Scael.Fn printf 4946094Sbosticfamily of functions produces output according to a 50*48351Scael.Fa format 5146094Sbosticas described below. 52*48351Scael.Fn Printf 5337423Sbosticand 54*48351Scael.Fn vprintf 55*48351Scaelwrite output to 56*48351Scael.Em stdout, 57*48351Scaelthe standard output stream; 58*48351Scael.Fn fprintf 5937423Sbosticand 60*48351Scael.Fn vfprintf 61*48351Scaelwrite output to the given output 62*48351Scael.Fa stream ; 63*48351Scael.Fn sprintf , 64*48351Scael.Fn snprintf , 65*48351Scael.Fn vsprintf , 6637423Sbosticand 67*48351Scael.Fn vsnprintf 6846094Sbosticwrite to the character string 69*48351Scael.Fa str . 7046094Sbostic.\" .IR str ; 7146094Sbostic.\" and 7246094Sbostic.\" .I smprintf 7346094Sbostic.\" and 7446094Sbostic.\" .I vsmprintf 7546094Sbostic.\" dynamically allocate a new string with 7646094Sbostic.\" .IR malloc . 77*48351ScaelThese functions write the output under the control of a 78*48351Scael.Fa format 79*48351Scaelstring that specifies how subsequent arguments 80*48351Scael(or arguments accessed via the variable-length argument facilities of 81*48351Scael.Xr varargs 3 ) 82*48351Scaelare converted for output. 8346094Sbostic.\" Except for 8446094Sbostic.\" .I smprintf 8546094Sbostic.\" and 8646094Sbostic.\" .IR vsmprintf , 8746094Sbostic.\" all of these functions return 88*48351ScaelThese functions return 8946094Sbosticthe number of characters printed 90*48351Scael(not including the trailing 91*48351Scael.Ql \e0 92*48351Scaelused to end output to strings). 9346094Sbostic.\" .I Smprintf 9446094Sbostic.\" and 9546094Sbostic.\" .I vsmprintf 9646094Sbostic.\" return a pointer to a string of an appropriate length; 9746094Sbostic.\" this pointer should be passed to 9846094Sbostic.\" .I free 9946094Sbostic.\" to release the associated storage 10046094Sbostic.\" when it is no longer needed. 10146094Sbostic.\" If sufficient space is not avaliable, 10246094Sbostic.\" .I smprintf 10346094Sbostic.\" and 10446094Sbostic.\" .I vsmprintf 10546094Sbostic.\" will return 10646094Sbostic.\" .SM 107*48351Scael.\" .BR 108*48351Scael.Fn Snprintf 10937423Sbosticand 110*48351Scael.Fn vsnprintf 11146094Sbosticwill write at most 112*48351Scael.Fa size Ns \-1 11346094Sbosticof the characters printed into the output string 11446094Sbostic(the 115*48351Scael.Fa size Ns 'th 116*48351Scaelcharacter then gets the terminating 117*48351Scael.Ql \e0 ) ; 11846094Sbosticif the return value is greater than or equal to the 119*48351Scael.Fa size 12046094Sbosticargument, the string was too short 12146094Sbosticand some of the printed characters were discarded. 122*48351Scael.Fn Sprintf 12334643Sbosticand 124*48351Scael.Fn vsprintf 12546094Sbosticeffectively assume an infinte 126*48351Scael.Fa size . 127*48351Scael.Pp 128*48351ScaelThe format string is composed of zero or more directives: 129*48351Scaelordinary 130*48351Scael.\" multibyte 131*48351Scaelcharacters (not 132*48351Scael.Cm % ) , 133*48351Scaelwhich are copied unchanged to the output stream; 134*48351Scaeland conversion specifications, each of which results 135*48351Scaelin fetching zero or more subsequent arguments. 136*48351ScaelEach conversion specification is introduced by 137*48351Scaelthe character 138*48351Scael.Cm % . 139*48351ScaelThe arguments must correspond properly (after type promotion) 14046094Sbosticwith the conversion specifier. 141*48351ScaelAfter the 142*48351Scael.Cm % , 143*48351Scaelthe following appear in sequence: 144*48351Scael.Bl -bullet 145*48351Scael.It 14634643SbosticZero or more of the following flags: 147*48351Scael.Bl -hyphen -offset indent 148*48351Scael.It 149*48351Scaela 150*48351Scael.Cm # 151*48351Scaelcharacter 15220359Smckusickspecifying that the value should be converted to an ``alternate form''. 15320359SmckusickFor 154*48351Scael.Cm c , 155*48351Scael.Cm d , 156*48351Scael.Cm i , 157*48351Scael.Cm n , 158*48351Scael.Cm p , 159*48351Scael.Cm s , 16020359Smckusickand 161*48351Scael.Cm u , 16237423Sbosticconversions, this option has no effect. 16337423SbosticFor 164*48351Scael.Cm o 16520359Smckusickconversions, the precision of the number is increased to force the first 16637423Sbosticcharacter of the output string to a zero (except if a zero value is printed 16737423Sbosticwith an explicit precision of zero). 16837423SbosticFor 169*48351Scael.Cm x 17037423Sbosticand 171*48351Scael.Cm X 17237423Sbosticconversions, a non-zero result has the string 173*48351Scael.Ql 0x 17437423Sbostic(or 175*48351Scael.Ql 0X 17637423Sbosticfor 177*48351Scael.Cm X 17837423Sbosticconversions) prepended to it. 17937423SbosticFor 180*48351Scael.Cm e , 181*48351Scael.Cm E , 182*48351Scael.Cm f , 183*48351Scael.Cm g , 18420359Smckusickand 185*48351Scael.Cm G , 18620359Smckusickconversions, the result will always contain a decimal point, even if no 18737423Sbosticdigits follow it (normally, a decimal point appears in the results of 18837423Sbosticthose conversions only if a digit follows). 18937423SbosticFor 190*48351Scael.Cm g 19120359Smckusickand 192*48351Scael.Cm G 19320359Smckusickconversions, trailing zeros are not removed from the result as they 19437423Sbosticwould otherwise be. 195*48351Scael.It 196*48351ScaelA zero 197*48351Scael.Sq Cm \&0 198*48351Scaelcharacter specifying zero padding. 19937423SbosticFor all conversions except 200*48351Scael.Cm n , 20137423Sbosticthe converted value is padded on the left with zeros rather than blanks. 20246094SbosticIf a precision is given with a numeric conversion 203*48351Scael.Pf ( Mc d , 204*48351Scael.Cm i , 205*48351Scael.Cm o , 206*48351Scael.Cm u , 207*48351Scael.Cm i , 208*48351Scael.Cm x , 20937423Sbosticand 210*48351Scael.Cm X ) , 211*48351Scaelthe 212*48351Scael.Sq Cm \&0 213*48351Scaelflag is ignored. 214*48351Scael.It 215*48351ScaelA negative field width flag 216*48351Scael.Sq Cm \- 217*48351Scaelindicates the converted value is to be left adjusted on the field boundary. 21837423SbosticExcept for 219*48351Scael.Cm n 22037423Sbosticconversions, the converted value is padded on the right with blanks, 22137423Sbosticrather than on the left with blanks or zeros. 222*48351ScaelA 223*48351Scael.Sq Cm \- 224*48351Scaeloverrides a 225*48351Scael.Sq Cm \&0 226*48351Scaelif both are given. 227*48351Scael.It 22837423SbosticA space, specifying that a blank should be left before a positive number 22946094Sbosticproduced by a signed conversion 230*48351Scael.Pf ( Cm d , 231*48351Scael.Cm e , 232*48351Scael.Cm E , 233*48351Scael.Cm f , 234*48351Scael.Cm g , 235*48351Scael.Cm G , 23637423Sbosticor 237*48351Scael.Cm i ) . 238*48351Scael.It 239*48351Scaela 240*48351Scael.Sq Cm + 241*48351Scaelcharacter specifying that a sign always be placed before a 24237423Sbosticnumber produced by a signed conversion. 243*48351ScaelA 244*48351Scael.Sq Cm + 245*48351Scaeloverrides a space if both are used. 246*48351Scael.El 247*48351Scael.It 248*48351ScaelAn optional decimal digit string specifying a minimum field width. 24937423SbosticIf the converted value has fewer characters than the field width, it will 250*48351Scaelbe padded with spaces on the left (or right, if the left-adjustment 251*48351Scaelflag has been given) to fill out 252*48351Scaelthe field width. 253*48351Scael.It 254*48351ScaelAn optional precision, in the form of a period 255*48351Scael.Sq Cm \&. 256*48351Scaelfollowed by an 25737423Sbosticoptional digit string. If the digit string is omitted, the precision 25837423Sbosticis taken as zero. This gives the minimum number of digits to appear for 259*48351Scael.Cm d , 260*48351Scael.Cm i , 261*48351Scael.Cm o , 262*48351Scael.Cm u , 263*48351Scael.Cm x , 26437423Sbosticand 265*48351Scael.Cm X 266*48351Scaelconversions, the number of digits to appear after the decimal-point for 267*48351Scael.Cm e , 268*48351Scael.Cm E , 26937423Sbosticand 270*48351Scael.Cm f 27137423Sbosticconversions, the maximum number of significant digits for 272*48351Scael.Cm g 27337423Sbosticand 274*48351Scael.Cm G 27537423Sbosticconversions, or the maximum number of characters to be printed from a 27637423Sbosticstring for 277*48351Scael.Cm s 27837423Sbosticconversions. 279*48351Scael.It 280*48351ScaelThe optional character 281*48351Scael.Cm h , 28220358Smckusickspecifying that a following 283*48351Scael.Cm d , 284*48351Scael.Cm i , 285*48351Scael.Cm o , 286*48351Scael.Cm u , 287*48351Scael.Cm x , 28820358Smckusickor 289*48351Scael.Cm X 29037423Sbosticconversion corresponds to a 291*48351Scael.Em short int 29237423Sbosticor 293*48351Scael.Em unsigned short int 29437423Sbosticargument, or that a following 295*48351Scael.Cm n 29637423Sbosticconversion corresponds to a pointer to a 297*48351Scael.Em short int 29837423Sbosticargument. 299*48351Scael.It 300*48351ScaelThe optional character 301*48351Scael.Cm l 30237423Sbostic(ell) specifying that a following 303*48351Scael.Cm d , 304*48351Scael.Cm i , 305*48351Scael.Cm o , 306*48351Scael.Cm u , 307*48351Scael.Cm x , 30834643Sbosticor 309*48351Scael.Cm X 310*48351Scaelconversion applies to a pointer to a 311*48351Scael.Em long int 31237423Sbosticor 313*48351Scael.Em unsigned long int 31437423Sbosticargument, or that a following 315*48351Scael.Cm n 31637423Sbosticconversion corresponds to a pointer to a 317*48351Scael.Em long int 31837423Sbosticargument. 319*48351Scael.It 32037423SbosticThe character 321*48351Scael.Cm L 32237423Sbosticspecifying that a following 323*48351Scael.Cm e , 324*48351Scael.Cm E , 325*48351Scael.Cm f , 326*48351Scael.Cm g , 32737423Sbosticor 328*48351Scael.Cm G 32937423Sbosticconversion corresponds to a 330*48351Scael.Em long double 33137423Sbosticargument (but note that long double values are not currently supported 332*48351Scaelby the 333*48351Scael.Tn VAX 334*48351Scaeland 335*48351Scael.Tn Tahoe 336*48351Scaelcompilers). 337*48351Scael.It 338*48351ScaelA character that specifies the type of conversion to be applied. 339*48351Scael.El 340*48351Scael.Pp 341*48351ScaelA field width or precision, or both, may be indicated by 342*48351Scaelan asterisk 343*48351Scael.Ql * 344*48351Scaelinstead of a 34537423Sbosticdigit string. 346*48351ScaelIn this case, an 347*48351Scael.Em int 348*48351Scaelargument supplies the field width or precision. 34937423SbosticA negative field width is treated as a left adjustment flag followed by a 35037423Sbosticpositive field width; a negative precision is treated as though it were 35137423Sbosticmissing. 352*48351Scael.Pp 353*48351ScaelThe conversion specifiers and their meanings are: 354*48351Scael.Bl -tag -width "diouxX" 355*48351Scael.It Cm diouxX 35637423SbosticThe 357*48351Scael.Em int 35837423Sbostic(or appropriate variant) argument is converted to signed decimal 359*48351Scael.Pf ( Cm d 360*48351Scaeland 361*48351Scael.Cm i ) , 36237423Sbosticunsigned octal 363*48351Scael.Pq Cm o , 36437423Sbosticunsigned decimal 365*48351Scael.Pq Cm u , 36637423Sbosticor unsigned hexadecimal 367*48351Scael.Pf ( Cm x 368*48351Scaeland 369*48351Scael.Cm X ) 370*48351Scaelnotation. The letters 371*48351Scael.Cm abcdef 37237423Sbosticare used for 373*48351Scael.Cm x 37437423Sbosticconversions; the letters 375*48351Scael.Cm ABCDEF 37637423Sbosticare used for 377*48351Scael.m X 37837423Sbosticconversions. 37937423SbosticThe precision, if any, gives the minimum number of digits that must 38037423Sbosticappear; if the converted value requires fewer digits, it is padded on 38137423Sbosticthe left with zeros. 382*48351Scael.It Cm DOU 38337423SbosticThe 384*48351Scael.Em long int 38537423Sbosticargument is converted to signed decimal, unsigned octal, or unsigned 38637423Sbosticdecimal, as if the format had been 387*48351Scael.Cm ld , 388*48351Scael.Cm lo , 38937423Sbosticor 390*48351Scael.Cm lu 39137423Sbosticrespectively. 39237423SbosticThese conversion characters are deprecated, and will eventually disappear. 393*48351Scael.It Cm eE 39437423SbosticThe 395*48351Scael.Em double 39637423Sbosticargument is rounded and converted in the style 397*48351Scael.Sm off 398*48351Scael.Pf [\-]d Cm \&. No ddd Cm e No \\*(Pmdd 399*48351Scael.Sm on 400*48351Scaelwhere there is one digit before the 401*48351Scaeldecimal-point character 402*48351Scaeland the number of digits after it is equal to the precision; 403*48351Scaelif the precision is missing, 404*48351Scaelit is taken as 6; if the precision is 405*48351Scaelzero, no decimal-point character appears. 40637423SbosticAn 407*48351Scael.Cm E 40837423Sbosticconversion uses the letter 409*48351Scael.Cm E 41037423Sbostic(rather than 411*48351Scael.Cm e ) 41237423Sbosticto introduce the exponent. 41337423SbosticThe exponent always contains at least two digits; if the value is zero, 41437423Sbosticthe exponent is 00. 415*48351Scael.It Cm f 41637423SbosticThe 417*48351Scael.Em double 41837423Sbosticargument is rounded and converted to decimal notation in the style 419*48351Scael.Sm off 420*48351Scael.Pf [-]ddd Cm \&. No ddd , 421*48351Scael.Sm on 422*48351Scaelwhere the number of digits after the decimal-point character 423*48351Scaelis equal to the precision specification. 424*48351ScaelIf the precision is missing, it is taken as 6; if the precision is 425*48351Scaelexplicitly zero, no decimal-point character appears. 42637423SbosticIf a decimal point appears, at least one digit appears before it. 427*48351Scael.It Cm g 42837423SbosticThe 429*48351Scael.Em double 430*48351Scaelargument is converted in style 431*48351Scael.Cm f 43237423Sbosticor 433*48351Scael.Cm e 43437423Sbostic(or 435*48351Scael.Cm E 43637423Sbosticfor 437*48351Scael.Cm G 43837423Sbosticconversions). 43937423SbosticThe precision specifies the number of significant digits. 44037423SbosticIf the precision is missing, 6 digits are given; if the precision is zero, 44137423Sbosticit is treated as 1. 44237423SbosticStyle 443*48351Scael.Cm e 44437423Sbosticis used if the exponent from its conversion is less than -4 or greater than 44537423Sbosticor equal to the precision. 44637423SbosticTrailing zeros are removed from the fractional part of the result; a 44737423Sbosticdecimal point appears only if it is followed by at least one digit. 448*48351Scael.It Cm c 44937423SbosticThe 450*48351Scael.Em int 45137423Sbosticargument is converted to an 452*48351Scael.Em unsigned char , 453*48351Scaeland the resulting character is written. 454*48351Scael.It Cm s 45537423SbosticThe 456*48351Scael.Dq Em char * 457*48351Scaelargument is expected to be a pointer to an array of character type (pointer 458*48351Scaelto a string). 459*48351ScaelCharacters from the array are written up to (but not including) 460*48351Scaela terminating 461*48351Scael.Dv NUL 462*48351Scaelcharacter; 463*48351Scaelif a precision is specified, no more than the number specified are 464*48351Scaelwritten. 465*48351ScaelIf a precision is given, no null character 466*48351Scaelneed be present; if the precision is not specified, or is greater than 467*48351Scaelthe size of the array, the array must contain a terminating 468*48351Scael.Dv NUL 469*48351Scaelcharacter. 470*48351Scael.It Cm p 47137423SbosticThe 472*48351Scael.Dq Em void * 473*48351Scaelpointer argument is printed in hexadecimal (as if by 474*48351Scael.Ql %#x 475*48351Scaelor 476*48351Scael.Ql %#lx ) . 477*48351Scael.It Cm n 47837423SbosticThe number of characters written so far is stored into the 47937423Sbosticinteger indicated by the 480*48351Scael.Dq Em int * 48137423Sbostic(or variant) pointer argument. 48237423SbosticNo argument is converted. 483*48351Scael.It Cm % 484*48351ScaelA 485*48351Scael.Ql % 486*48351Scaelis written. No argument is converted. The complete conversion specification 487*48351Scaelis 488*48351Scael.Ql %% . 489*48351Scael.El 490*48351Scael.Pp 49137423SbosticIn no case does a non-existent or small field width cause truncation of 49237423Sbostica field; if the result of a conversion is wider than the field width, the 493*48351Scaelfield is expanded to contain the conversion result. 494*48351Scael.Pp 495*48351Scael.Sh EXAMPLES 49620358Smckusick.br 49720358SmckusickTo print a date and time in the form `Sunday, July 3, 10:02', 49820358Smckusickwhere 499*48351Scael.Em weekday 50020358Smckusickand 501*48351Scael.Em month 502*48351Scaelare pointers to strings: 503*48351Scael.Bd -literal -offset indent 504*48351Scael#include <stdio.h> 505*48351Scaelfprintf(stdout,"%s, %s %d, %.2d:%.2d0", 506*48351Scael weekday, month, day, hour, min); 507*48351Scael.Ed 508*48351Scael.Pp 509*48351ScaelTo print \*(Pi 510*48351Scaelto five decimal places: 511*48351Scael.Bd -literal -offset indent 512*48351Scael#include <math.h> 513*48351Scael#include <stdio.h> 514*48351Scaelfprintf(stdout,"pi = %.5f0", 4 * atan(1.0)); 515*48351Scael.Ed 516*48351Scael.Pp 51737423SbosticTo allocate a 128 byte string and print into it: 518*48351Scael.Bd -literal -offset indent 51937423Sbostic#include <stdio.h> 52037423Sbostic#include <varargs.h> 52137423Sbosticchar *newfmt(va_alist) 522*48351Scael va_dcl 52337423Sbostic{ 524*48351Scael char *p, *malloc(), *fmt; 525*48351Scael va_list ap; 526*48351Scael if ((p = malloc(128)) == NULL) 527*48351Scael return (NULL); 528*48351Scael va_start(ap); 529*48351Scael fmt = va_arg(ap, char *); 530*48351Scael (void) vsprintf(p, fmt, ap); 531*48351Scael va_end(ap); 532*48351Scael return (p); 53337423Sbostic} 534*48351Scael.Ed 535*48351Scael.Sh SEE ALSO 536*48351Scael.Xr printf 1 , 537*48351Scael.Xr scanf 3 538*48351Scael.Sh STANDARDS 539*48351ScaelThe functions 540*48351Scael.Fn fprintf , 541*48351Scael.Fn printf , 542*48351Scael.Fn sprintf , 543*48351Scael.Fn snprintf , 544*48351Scael.Fn vprintf , 545*48351Scael.Fn vfprintf , 546*48351Scael.Fn vsprintf , 547*48351Scaeland 548*48351Scael.Fn vsnprintf 549*48351Scaelfunctions 550*48351Scaelconform to 551*48351Scael.St -ansiC . 552*48351Scael.Sh BUGS 553*48351ScaelThe conversion formats 554*48351Scael.Cm %D , 555*48351Scael.Cm %O , 556*48351Scaeland 557*48351Scael.Cm %U 558*48351Scaelare not standard and 55937423Sbosticare provided only for backward compatibility. 560*48351ScaelThe effect of padding the 561*48351Scael.Cm %p 562*48351Scaelformat with zeros (either by the 563*48351Scael.Sq Cm 0 56437423Sbosticflag or by specifying a precision), and the benign effect (i.e., none) 565*48351Scaelof the 566*48351Scael.Sq Cm # 567*48351Scaelflag on 568*48351Scael.Cm %n 569*48351Scaeland 570*48351Scael.Cm %p 571*48351Scaelconversions, as well as other 572*48351Scaelnonsensical combinations such as 573*48351Scael.Cm %Ld , 574*48351Scaelare not standard; such combinations 57537423Sbosticshould be avoided. 576*48351Scael.Pp 57746094SbosticBecause 578*48351Scael.Fn sprintf 57946094Sbosticand 580*48351Scael.Fn vsprintf 58146094Sbosticassume an infinitely long string, 58246094Sbosticcallers must be careful not to overflow the actual space; 58346094Sbosticthis is often impossible to assure. 58446094SbosticFor safety, programmers should use the 585*48351Scael.Fn snprintf 58646094Sbosticinterface instead. 58746094SbosticUnfortunately, this interface is not portable. 588