148351Scael.\" 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*48723Sdonn.\" @(#)printf.3 6.12 (Berkeley) 04/26/91 946094Sbostic.\" 1048351Scael.Dd 1148351Scael.Dt PRINTF 3 1248351Scael.Os 1348351Scael.Sh NAME 1448351Scael.Nm printf , 1548351Scael.Nm fprintf , 1648351Scael.Nm sprintf , 1748351Scael.Nm snprintf , 1848351Scael.Nm vprintf , 1948351Scael.Nm vfprintf, 2048351Scael.Nm vsprintf , 2148351Scael.Nm vsnprintf 2248351Scael.Nd formatted output conversion 2348351Scael.Sh SYNOPSIS 2448351Scael.Fd #include <stdio.h> 2548351Scael.Ft int 2648351Scael.Fn printf "const char *format" ... 2748351Scael.Ft int 2848351Scael.Fn fprintf "FILE *stream" "const char *format" ... 2948351Scael.Ft int 3048351Scael.Fn sprintf "char *str" "const char *format" ... 3148351Scael.Ft int 3248351Scael.Fn snprintf "char *str" "size_t size" "const char *format" ... 3348351Scael.\" .Ft int 3448351Scael.\" .Fn smprintf "const char *format" ... 35*48723Sdonn.Fd #include <stdarg.h> 3648351Scael.Ft int 3748351Scael.Fn vprintf "const char *format" "va_list ap" 3848351Scael.Ft int 3948351Scael.Fn vfprintf "FILE *stream" "const char *format" "va_list ap" 4048351Scael.Ft int 4148351Scael.Fn vsprintf "char *str" "char *format" "va_list ap" 4248351Scael.Ft int 4348351Scael.Fn vsnprintf "char *str" "size_t size" "const char *format" "va_list ap" 4448351Scael.\" .Ft int 4548351Scael.\" .Fn vsmprintf "const char *format" "va_list ap" 4648351Scael.Sh DESCRIPTION 4746094SbosticThe 4848351Scael.Fn printf 4946094Sbosticfamily of functions produces output according to a 5048351Scael.Fa format 5146094Sbosticas described below. 5248351Scael.Fn Printf 5337423Sbosticand 5448351Scael.Fn vprintf 5548351Scaelwrite output to 5648351Scael.Em stdout, 5748351Scaelthe standard output stream; 5848351Scael.Fn fprintf 5937423Sbosticand 6048351Scael.Fn vfprintf 6148351Scaelwrite output to the given output 6248351Scael.Fa stream ; 6348351Scael.Fn sprintf , 6448351Scael.Fn snprintf , 6548351Scael.Fn vsprintf , 6637423Sbosticand 6748351Scael.Fn vsnprintf 6846094Sbosticwrite to the character string 6948351Scael.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 . 7748351ScaelThese functions write the output under the control of a 7848351Scael.Fa format 7948351Scaelstring that specifies how subsequent arguments 8048351Scael(or arguments accessed via the variable-length argument facilities of 81*48723Sdonn.Xr stdarg 3 ) 8248351Scaelare converted for output. 8346094Sbostic.\" Except for 8446094Sbostic.\" .I smprintf 8546094Sbostic.\" and 8646094Sbostic.\" .IR vsmprintf , 8746094Sbostic.\" all of these functions return 8848351ScaelThese functions return 8946094Sbosticthe number of characters printed 9048351Scael(not including the trailing 9148351Scael.Ql \e0 9248351Scaelused 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 10748351Scael.\" .BR 10848351Scael.Fn Snprintf 10937423Sbosticand 11048351Scael.Fn vsnprintf 11146094Sbosticwill write at most 11248351Scael.Fa size Ns \-1 11346094Sbosticof the characters printed into the output string 11446094Sbostic(the 11548351Scael.Fa size Ns 'th 11648351Scaelcharacter then gets the terminating 11748351Scael.Ql \e0 ) ; 11846094Sbosticif the return value is greater than or equal to the 11948351Scael.Fa size 12046094Sbosticargument, the string was too short 12146094Sbosticand some of the printed characters were discarded. 12248351Scael.Fn Sprintf 12334643Sbosticand 12448351Scael.Fn vsprintf 12546094Sbosticeffectively assume an infinte 12648351Scael.Fa size . 12748351Scael.Pp 12848351ScaelThe format string is composed of zero or more directives: 12948351Scaelordinary 13048351Scael.\" multibyte 13148351Scaelcharacters (not 13248351Scael.Cm % ) , 13348351Scaelwhich are copied unchanged to the output stream; 13448351Scaeland conversion specifications, each of which results 13548351Scaelin fetching zero or more subsequent arguments. 13648351ScaelEach conversion specification is introduced by 13748351Scaelthe character 13848351Scael.Cm % . 13948351ScaelThe arguments must correspond properly (after type promotion) 14046094Sbosticwith the conversion specifier. 14148351ScaelAfter the 14248351Scael.Cm % , 14348351Scaelthe following appear in sequence: 14448351Scael.Bl -bullet 14548351Scael.It 14634643SbosticZero or more of the following flags: 14748351Scael.Bl -hyphen -offset indent 14848351Scael.It 14948351Scaela 15048351Scael.Cm # 15148351Scaelcharacter 15220359Smckusickspecifying that the value should be converted to an ``alternate form''. 15320359SmckusickFor 15448351Scael.Cm c , 15548351Scael.Cm d , 15648351Scael.Cm i , 15748351Scael.Cm n , 15848351Scael.Cm p , 15948351Scael.Cm s , 16020359Smckusickand 16148351Scael.Cm u , 16237423Sbosticconversions, this option has no effect. 16337423SbosticFor 16448351Scael.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 16948351Scael.Cm x 17037423Sbosticand 17148351Scael.Cm X 17237423Sbosticconversions, a non-zero result has the string 17348351Scael.Ql 0x 17437423Sbostic(or 17548351Scael.Ql 0X 17637423Sbosticfor 17748351Scael.Cm X 17837423Sbosticconversions) prepended to it. 17937423SbosticFor 18048351Scael.Cm e , 18148351Scael.Cm E , 18248351Scael.Cm f , 18348351Scael.Cm g , 18420359Smckusickand 18548351Scael.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 19048351Scael.Cm g 19120359Smckusickand 19248351Scael.Cm G 19320359Smckusickconversions, trailing zeros are not removed from the result as they 19437423Sbosticwould otherwise be. 19548351Scael.It 19648351ScaelA zero 19748351Scael.Sq Cm \&0 19848351Scaelcharacter specifying zero padding. 19937423SbosticFor all conversions except 20048351Scael.Cm n , 20137423Sbosticthe converted value is padded on the left with zeros rather than blanks. 20246094SbosticIf a precision is given with a numeric conversion 20348351Scael.Pf ( Mc d , 20448351Scael.Cm i , 20548351Scael.Cm o , 20648351Scael.Cm u , 20748351Scael.Cm i , 20848351Scael.Cm x , 20937423Sbosticand 21048351Scael.Cm X ) , 21148351Scaelthe 21248351Scael.Sq Cm \&0 21348351Scaelflag is ignored. 21448351Scael.It 21548351ScaelA negative field width flag 21648351Scael.Sq Cm \- 21748351Scaelindicates the converted value is to be left adjusted on the field boundary. 21837423SbosticExcept for 21948351Scael.Cm n 22037423Sbosticconversions, the converted value is padded on the right with blanks, 22137423Sbosticrather than on the left with blanks or zeros. 22248351ScaelA 22348351Scael.Sq Cm \- 22448351Scaeloverrides a 22548351Scael.Sq Cm \&0 22648351Scaelif both are given. 22748351Scael.It 22837423SbosticA space, specifying that a blank should be left before a positive number 22946094Sbosticproduced by a signed conversion 23048351Scael.Pf ( Cm d , 23148351Scael.Cm e , 23248351Scael.Cm E , 23348351Scael.Cm f , 23448351Scael.Cm g , 23548351Scael.Cm G , 23637423Sbosticor 23748351Scael.Cm i ) . 23848351Scael.It 23948351Scaela 24048351Scael.Sq Cm + 24148351Scaelcharacter specifying that a sign always be placed before a 24237423Sbosticnumber produced by a signed conversion. 24348351ScaelA 24448351Scael.Sq Cm + 24548351Scaeloverrides a space if both are used. 24648351Scael.El 24748351Scael.It 24848351ScaelAn optional decimal digit string specifying a minimum field width. 24937423SbosticIf the converted value has fewer characters than the field width, it will 25048351Scaelbe padded with spaces on the left (or right, if the left-adjustment 25148351Scaelflag has been given) to fill out 25248351Scaelthe field width. 25348351Scael.It 25448351ScaelAn optional precision, in the form of a period 25548351Scael.Sq Cm \&. 25648351Scaelfollowed 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 25948351Scael.Cm d , 26048351Scael.Cm i , 26148351Scael.Cm o , 26248351Scael.Cm u , 26348351Scael.Cm x , 26437423Sbosticand 26548351Scael.Cm X 26648351Scaelconversions, the number of digits to appear after the decimal-point for 26748351Scael.Cm e , 26848351Scael.Cm E , 26937423Sbosticand 27048351Scael.Cm f 27137423Sbosticconversions, the maximum number of significant digits for 27248351Scael.Cm g 27337423Sbosticand 27448351Scael.Cm G 27537423Sbosticconversions, or the maximum number of characters to be printed from a 27637423Sbosticstring for 27748351Scael.Cm s 27837423Sbosticconversions. 27948351Scael.It 28048351ScaelThe optional character 28148351Scael.Cm h , 28220358Smckusickspecifying that a following 28348351Scael.Cm d , 28448351Scael.Cm i , 28548351Scael.Cm o , 28648351Scael.Cm u , 28748351Scael.Cm x , 28820358Smckusickor 28948351Scael.Cm X 29037423Sbosticconversion corresponds to a 29148351Scael.Em short int 29237423Sbosticor 29348351Scael.Em unsigned short int 29437423Sbosticargument, or that a following 29548351Scael.Cm n 29637423Sbosticconversion corresponds to a pointer to a 29748351Scael.Em short int 29837423Sbosticargument. 29948351Scael.It 30048351ScaelThe optional character 30148351Scael.Cm l 30237423Sbostic(ell) specifying that a following 30348351Scael.Cm d , 30448351Scael.Cm i , 30548351Scael.Cm o , 30648351Scael.Cm u , 30748351Scael.Cm x , 30834643Sbosticor 30948351Scael.Cm X 31048351Scaelconversion applies to a pointer to a 31148351Scael.Em long int 31237423Sbosticor 31348351Scael.Em unsigned long int 31437423Sbosticargument, or that a following 31548351Scael.Cm n 31637423Sbosticconversion corresponds to a pointer to a 31748351Scael.Em long int 31837423Sbosticargument. 31948351Scael.It 32037423SbosticThe character 32148351Scael.Cm L 32237423Sbosticspecifying that a following 32348351Scael.Cm e , 32448351Scael.Cm E , 32548351Scael.Cm f , 32648351Scael.Cm g , 32737423Sbosticor 32848351Scael.Cm G 32937423Sbosticconversion corresponds to a 33048351Scael.Em long double 33137423Sbosticargument (but note that long double values are not currently supported 33248351Scaelby the 33348351Scael.Tn VAX 33448351Scaeland 33548351Scael.Tn Tahoe 33648351Scaelcompilers). 33748351Scael.It 33848351ScaelA character that specifies the type of conversion to be applied. 33948351Scael.El 34048351Scael.Pp 34148351ScaelA field width or precision, or both, may be indicated by 34248351Scaelan asterisk 34348351Scael.Ql * 34448351Scaelinstead of a 34537423Sbosticdigit string. 34648351ScaelIn this case, an 34748351Scael.Em int 34848351Scaelargument 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. 35248351Scael.Pp 35348351ScaelThe conversion specifiers and their meanings are: 35448351Scael.Bl -tag -width "diouxX" 35548351Scael.It Cm diouxX 35637423SbosticThe 35748351Scael.Em int 35837423Sbostic(or appropriate variant) argument is converted to signed decimal 35948351Scael.Pf ( Cm d 36048351Scaeland 36148351Scael.Cm i ) , 36237423Sbosticunsigned octal 36348351Scael.Pq Cm o , 36437423Sbosticunsigned decimal 36548351Scael.Pq Cm u , 36637423Sbosticor unsigned hexadecimal 36748351Scael.Pf ( Cm x 36848351Scaeland 36948351Scael.Cm X ) 37048351Scaelnotation. The letters 37148351Scael.Cm abcdef 37237423Sbosticare used for 37348351Scael.Cm x 37437423Sbosticconversions; the letters 37548351Scael.Cm ABCDEF 37637423Sbosticare used for 37748351Scael.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. 38248351Scael.It Cm DOU 38337423SbosticThe 38448351Scael.Em long int 38537423Sbosticargument is converted to signed decimal, unsigned octal, or unsigned 38637423Sbosticdecimal, as if the format had been 38748351Scael.Cm ld , 38848351Scael.Cm lo , 38937423Sbosticor 39048351Scael.Cm lu 39137423Sbosticrespectively. 39237423SbosticThese conversion characters are deprecated, and will eventually disappear. 39348351Scael.It Cm eE 39437423SbosticThe 39548351Scael.Em double 39637423Sbosticargument is rounded and converted in the style 39748351Scael.Sm off 39848351Scael.Pf [\-]d Cm \&. No ddd Cm e No \\*(Pmdd 39948351Scael.Sm on 40048351Scaelwhere there is one digit before the 40148351Scaeldecimal-point character 40248351Scaeland the number of digits after it is equal to the precision; 40348351Scaelif the precision is missing, 40448351Scaelit is taken as 6; if the precision is 40548351Scaelzero, no decimal-point character appears. 40637423SbosticAn 40748351Scael.Cm E 40837423Sbosticconversion uses the letter 40948351Scael.Cm E 41037423Sbostic(rather than 41148351Scael.Cm e ) 41237423Sbosticto introduce the exponent. 41337423SbosticThe exponent always contains at least two digits; if the value is zero, 41437423Sbosticthe exponent is 00. 41548351Scael.It Cm f 41637423SbosticThe 41748351Scael.Em double 41837423Sbosticargument is rounded and converted to decimal notation in the style 41948351Scael.Sm off 42048351Scael.Pf [-]ddd Cm \&. No ddd , 42148351Scael.Sm on 42248351Scaelwhere the number of digits after the decimal-point character 42348351Scaelis equal to the precision specification. 42448351ScaelIf the precision is missing, it is taken as 6; if the precision is 42548351Scaelexplicitly zero, no decimal-point character appears. 42637423SbosticIf a decimal point appears, at least one digit appears before it. 42748351Scael.It Cm g 42837423SbosticThe 42948351Scael.Em double 43048351Scaelargument is converted in style 43148351Scael.Cm f 43237423Sbosticor 43348351Scael.Cm e 43437423Sbostic(or 43548351Scael.Cm E 43637423Sbosticfor 43748351Scael.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 44348351Scael.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. 44848351Scael.It Cm c 44937423SbosticThe 45048351Scael.Em int 45137423Sbosticargument is converted to an 45248351Scael.Em unsigned char , 45348351Scaeland the resulting character is written. 45448351Scael.It Cm s 45537423SbosticThe 45648351Scael.Dq Em char * 45748351Scaelargument is expected to be a pointer to an array of character type (pointer 45848351Scaelto a string). 45948351ScaelCharacters from the array are written up to (but not including) 46048351Scaela terminating 46148351Scael.Dv NUL 46248351Scaelcharacter; 46348351Scaelif a precision is specified, no more than the number specified are 46448351Scaelwritten. 46548351ScaelIf a precision is given, no null character 46648351Scaelneed be present; if the precision is not specified, or is greater than 46748351Scaelthe size of the array, the array must contain a terminating 46848351Scael.Dv NUL 46948351Scaelcharacter. 47048351Scael.It Cm p 47137423SbosticThe 47248351Scael.Dq Em void * 47348351Scaelpointer argument is printed in hexadecimal (as if by 47448351Scael.Ql %#x 47548351Scaelor 47648351Scael.Ql %#lx ) . 47748351Scael.It Cm n 47837423SbosticThe number of characters written so far is stored into the 47937423Sbosticinteger indicated by the 48048351Scael.Dq Em int * 48137423Sbostic(or variant) pointer argument. 48237423SbosticNo argument is converted. 48348351Scael.It Cm % 48448351ScaelA 48548351Scael.Ql % 48648351Scaelis written. No argument is converted. The complete conversion specification 48748351Scaelis 48848351Scael.Ql %% . 48948351Scael.El 49048351Scael.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 49348351Scaelfield is expanded to contain the conversion result. 49448351Scael.Pp 49548351Scael.Sh EXAMPLES 49620358Smckusick.br 49720358SmckusickTo print a date and time in the form `Sunday, July 3, 10:02', 49820358Smckusickwhere 49948351Scael.Em weekday 50020358Smckusickand 50148351Scael.Em month 50248351Scaelare pointers to strings: 50348351Scael.Bd -literal -offset indent 50448351Scael#include <stdio.h> 505*48723Sdonnfprintf(stdout, "%s, %s %d, %.2d:%.2d\en", 50648351Scael weekday, month, day, hour, min); 50748351Scael.Ed 50848351Scael.Pp 50948351ScaelTo print \*(Pi 51048351Scaelto five decimal places: 51148351Scael.Bd -literal -offset indent 51248351Scael#include <math.h> 51348351Scael#include <stdio.h> 514*48723Sdonnfprintf(stdout, "pi = %.5f\en", 4 * atan(1.0)); 51548351Scael.Ed 51648351Scael.Pp 51737423SbosticTo allocate a 128 byte string and print into it: 51848351Scael.Bd -literal -offset indent 51937423Sbostic#include <stdio.h> 520*48723Sdonn#include <stdlib.h> 521*48723Sdonn#include <stdarg.h> 522*48723Sdonnchar *newfmt(const char *fmt, ...) 52337423Sbostic{ 524*48723Sdonn char *p; 52548351Scael va_list ap; 52648351Scael if ((p = malloc(128)) == NULL) 52748351Scael return (NULL); 528*48723Sdonn va_start(ap, fmt); 529*48723Sdonn (void) vsnprintf(p, 128, fmt, ap); 53048351Scael va_end(ap); 53148351Scael return (p); 53237423Sbostic} 53348351Scael.Ed 53448351Scael.Sh SEE ALSO 53548351Scael.Xr printf 1 , 53648351Scael.Xr scanf 3 53748351Scael.Sh STANDARDS 538*48723SdonnThe 53948351Scael.Fn fprintf , 54048351Scael.Fn printf , 54148351Scael.Fn sprintf , 54248351Scael.Fn vprintf , 54348351Scael.Fn vfprintf , 54448351Scaeland 545*48723Sdonn.Fn vsprintf 54648351Scaelfunctions 54748351Scaelconform to 54848351Scael.St -ansiC . 549*48723Sdonn.Sh HISTORY 550*48723SdonnThe functions 551*48723Sdonn.Fn snprintf 552*48723Sdonnand 553*48723Sdonn.Fn vsnprintf 554*48723Sdonnare new to this release. 55548351Scael.Sh BUGS 55648351ScaelThe conversion formats 55748351Scael.Cm %D , 55848351Scael.Cm %O , 55948351Scaeland 56048351Scael.Cm %U 56148351Scaelare not standard and 56237423Sbosticare provided only for backward compatibility. 56348351ScaelThe effect of padding the 56448351Scael.Cm %p 56548351Scaelformat with zeros (either by the 56648351Scael.Sq Cm 0 56737423Sbosticflag or by specifying a precision), and the benign effect (i.e., none) 56848351Scaelof the 56948351Scael.Sq Cm # 57048351Scaelflag on 57148351Scael.Cm %n 57248351Scaeland 57348351Scael.Cm %p 57448351Scaelconversions, as well as other 57548351Scaelnonsensical combinations such as 57648351Scael.Cm %Ld , 57748351Scaelare not standard; such combinations 57837423Sbosticshould be avoided. 57948351Scael.Pp 58046094SbosticBecause 58148351Scael.Fn sprintf 58246094Sbosticand 58348351Scael.Fn vsprintf 58446094Sbosticassume an infinitely long string, 58546094Sbosticcallers must be careful not to overflow the actual space; 58646094Sbosticthis is often impossible to assure. 58746094SbosticFor safety, programmers should use the 58848351Scael.Fn snprintf 58946094Sbosticinterface instead. 59046094SbosticUnfortunately, this interface is not portable. 591