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 5*50296Sbostic.\" Chris Torek and the American National Standards Committee X3, 6*50296Sbostic.\" on Information Processing Systems. 7*50296Sbostic.\" 846094Sbostic.\" %sccs.include.redist.man% 946094Sbostic.\" 10*50296Sbostic.\" @(#)printf.3 6.13 (Berkeley) 06/29/91 1146094Sbostic.\" 1248351Scael.Dd 1348351Scael.Dt PRINTF 3 1448351Scael.Os 1548351Scael.Sh NAME 1648351Scael.Nm printf , 1748351Scael.Nm fprintf , 1848351Scael.Nm sprintf , 1948351Scael.Nm snprintf , 2048351Scael.Nm vprintf , 2148351Scael.Nm vfprintf, 2248351Scael.Nm vsprintf , 2348351Scael.Nm vsnprintf 2448351Scael.Nd formatted output conversion 2548351Scael.Sh SYNOPSIS 2648351Scael.Fd #include <stdio.h> 2748351Scael.Ft int 2848351Scael.Fn printf "const char *format" ... 2948351Scael.Ft int 3048351Scael.Fn fprintf "FILE *stream" "const char *format" ... 3148351Scael.Ft int 3248351Scael.Fn sprintf "char *str" "const char *format" ... 3348351Scael.Ft int 3448351Scael.Fn snprintf "char *str" "size_t size" "const char *format" ... 3548351Scael.\" .Ft int 3648351Scael.\" .Fn smprintf "const char *format" ... 3748723Sdonn.Fd #include <stdarg.h> 3848351Scael.Ft int 3948351Scael.Fn vprintf "const char *format" "va_list ap" 4048351Scael.Ft int 4148351Scael.Fn vfprintf "FILE *stream" "const char *format" "va_list ap" 4248351Scael.Ft int 4348351Scael.Fn vsprintf "char *str" "char *format" "va_list ap" 4448351Scael.Ft int 4548351Scael.Fn vsnprintf "char *str" "size_t size" "const char *format" "va_list ap" 4648351Scael.\" .Ft int 4748351Scael.\" .Fn vsmprintf "const char *format" "va_list ap" 4848351Scael.Sh DESCRIPTION 4946094SbosticThe 5048351Scael.Fn printf 5146094Sbosticfamily of functions produces output according to a 5248351Scael.Fa format 5346094Sbosticas described below. 5448351Scael.Fn Printf 5537423Sbosticand 5648351Scael.Fn vprintf 5748351Scaelwrite output to 5848351Scael.Em stdout, 5948351Scaelthe standard output stream; 6048351Scael.Fn fprintf 6137423Sbosticand 6248351Scael.Fn vfprintf 6348351Scaelwrite output to the given output 6448351Scael.Fa stream ; 6548351Scael.Fn sprintf , 6648351Scael.Fn snprintf , 6748351Scael.Fn vsprintf , 6837423Sbosticand 6948351Scael.Fn vsnprintf 7046094Sbosticwrite to the character string 7148351Scael.Fa str . 7246094Sbostic.\" .IR str ; 7346094Sbostic.\" and 7446094Sbostic.\" .I smprintf 7546094Sbostic.\" and 7646094Sbostic.\" .I vsmprintf 7746094Sbostic.\" dynamically allocate a new string with 7846094Sbostic.\" .IR malloc . 7948351ScaelThese functions write the output under the control of a 8048351Scael.Fa format 8148351Scaelstring that specifies how subsequent arguments 8248351Scael(or arguments accessed via the variable-length argument facilities of 8348723Sdonn.Xr stdarg 3 ) 8448351Scaelare converted for output. 8546094Sbostic.\" Except for 8646094Sbostic.\" .I smprintf 8746094Sbostic.\" and 8846094Sbostic.\" .IR vsmprintf , 8946094Sbostic.\" all of these functions return 9048351ScaelThese functions return 9146094Sbosticthe number of characters printed 9248351Scael(not including the trailing 9348351Scael.Ql \e0 9448351Scaelused to end output to strings). 9546094Sbostic.\" .I Smprintf 9646094Sbostic.\" and 9746094Sbostic.\" .I vsmprintf 9846094Sbostic.\" return a pointer to a string of an appropriate length; 9946094Sbostic.\" this pointer should be passed to 10046094Sbostic.\" .I free 10146094Sbostic.\" to release the associated storage 10246094Sbostic.\" when it is no longer needed. 10346094Sbostic.\" If sufficient space is not avaliable, 10446094Sbostic.\" .I smprintf 10546094Sbostic.\" and 10646094Sbostic.\" .I vsmprintf 10746094Sbostic.\" will return 10846094Sbostic.\" .SM 10948351Scael.\" .BR 11048351Scael.Fn Snprintf 11137423Sbosticand 11248351Scael.Fn vsnprintf 11346094Sbosticwill write at most 11448351Scael.Fa size Ns \-1 11546094Sbosticof the characters printed into the output string 11646094Sbostic(the 11748351Scael.Fa size Ns 'th 11848351Scaelcharacter then gets the terminating 11948351Scael.Ql \e0 ) ; 12046094Sbosticif the return value is greater than or equal to the 12148351Scael.Fa size 12246094Sbosticargument, the string was too short 12346094Sbosticand some of the printed characters were discarded. 12448351Scael.Fn Sprintf 12534643Sbosticand 12648351Scael.Fn vsprintf 12746094Sbosticeffectively assume an infinte 12848351Scael.Fa size . 12948351Scael.Pp 13048351ScaelThe format string is composed of zero or more directives: 13148351Scaelordinary 13248351Scael.\" multibyte 13348351Scaelcharacters (not 13448351Scael.Cm % ) , 13548351Scaelwhich are copied unchanged to the output stream; 13648351Scaeland conversion specifications, each of which results 13748351Scaelin fetching zero or more subsequent arguments. 13848351ScaelEach conversion specification is introduced by 13948351Scaelthe character 14048351Scael.Cm % . 14148351ScaelThe arguments must correspond properly (after type promotion) 14246094Sbosticwith the conversion specifier. 14348351ScaelAfter the 14448351Scael.Cm % , 14548351Scaelthe following appear in sequence: 14648351Scael.Bl -bullet 14748351Scael.It 14834643SbosticZero or more of the following flags: 14948351Scael.Bl -hyphen -offset indent 15048351Scael.It 15148351Scaela 15248351Scael.Cm # 15348351Scaelcharacter 15420359Smckusickspecifying that the value should be converted to an ``alternate form''. 15520359SmckusickFor 15648351Scael.Cm c , 15748351Scael.Cm d , 15848351Scael.Cm i , 15948351Scael.Cm n , 16048351Scael.Cm p , 16148351Scael.Cm s , 16220359Smckusickand 16348351Scael.Cm u , 16437423Sbosticconversions, this option has no effect. 16537423SbosticFor 16648351Scael.Cm o 16720359Smckusickconversions, the precision of the number is increased to force the first 16837423Sbosticcharacter of the output string to a zero (except if a zero value is printed 16937423Sbosticwith an explicit precision of zero). 17037423SbosticFor 17148351Scael.Cm x 17237423Sbosticand 17348351Scael.Cm X 17437423Sbosticconversions, a non-zero result has the string 17548351Scael.Ql 0x 17637423Sbostic(or 17748351Scael.Ql 0X 17837423Sbosticfor 17948351Scael.Cm X 18037423Sbosticconversions) prepended to it. 18137423SbosticFor 18248351Scael.Cm e , 18348351Scael.Cm E , 18448351Scael.Cm f , 18548351Scael.Cm g , 18620359Smckusickand 18748351Scael.Cm G , 18820359Smckusickconversions, the result will always contain a decimal point, even if no 18937423Sbosticdigits follow it (normally, a decimal point appears in the results of 19037423Sbosticthose conversions only if a digit follows). 19137423SbosticFor 19248351Scael.Cm g 19320359Smckusickand 19448351Scael.Cm G 19520359Smckusickconversions, trailing zeros are not removed from the result as they 19637423Sbosticwould otherwise be. 19748351Scael.It 19848351ScaelA zero 19948351Scael.Sq Cm \&0 20048351Scaelcharacter specifying zero padding. 20137423SbosticFor all conversions except 20248351Scael.Cm n , 20337423Sbosticthe converted value is padded on the left with zeros rather than blanks. 20446094SbosticIf a precision is given with a numeric conversion 20548351Scael.Pf ( Mc d , 20648351Scael.Cm i , 20748351Scael.Cm o , 20848351Scael.Cm u , 20948351Scael.Cm i , 21048351Scael.Cm x , 21137423Sbosticand 21248351Scael.Cm X ) , 21348351Scaelthe 21448351Scael.Sq Cm \&0 21548351Scaelflag is ignored. 21648351Scael.It 21748351ScaelA negative field width flag 21848351Scael.Sq Cm \- 21948351Scaelindicates the converted value is to be left adjusted on the field boundary. 22037423SbosticExcept for 22148351Scael.Cm n 22237423Sbosticconversions, the converted value is padded on the right with blanks, 22337423Sbosticrather than on the left with blanks or zeros. 22448351ScaelA 22548351Scael.Sq Cm \- 22648351Scaeloverrides a 22748351Scael.Sq Cm \&0 22848351Scaelif both are given. 22948351Scael.It 23037423SbosticA space, specifying that a blank should be left before a positive number 23146094Sbosticproduced by a signed conversion 23248351Scael.Pf ( Cm d , 23348351Scael.Cm e , 23448351Scael.Cm E , 23548351Scael.Cm f , 23648351Scael.Cm g , 23748351Scael.Cm G , 23837423Sbosticor 23948351Scael.Cm i ) . 24048351Scael.It 24148351Scaela 24248351Scael.Sq Cm + 24348351Scaelcharacter specifying that a sign always be placed before a 24437423Sbosticnumber produced by a signed conversion. 24548351ScaelA 24648351Scael.Sq Cm + 24748351Scaeloverrides a space if both are used. 24848351Scael.El 24948351Scael.It 25048351ScaelAn optional decimal digit string specifying a minimum field width. 25137423SbosticIf the converted value has fewer characters than the field width, it will 25248351Scaelbe padded with spaces on the left (or right, if the left-adjustment 25348351Scaelflag has been given) to fill out 25448351Scaelthe field width. 25548351Scael.It 25648351ScaelAn optional precision, in the form of a period 25748351Scael.Sq Cm \&. 25848351Scaelfollowed by an 25937423Sbosticoptional digit string. If the digit string is omitted, the precision 26037423Sbosticis taken as zero. This gives the minimum number of digits to appear for 26148351Scael.Cm d , 26248351Scael.Cm i , 26348351Scael.Cm o , 26448351Scael.Cm u , 26548351Scael.Cm x , 26637423Sbosticand 26748351Scael.Cm X 26848351Scaelconversions, the number of digits to appear after the decimal-point for 26948351Scael.Cm e , 27048351Scael.Cm E , 27137423Sbosticand 27248351Scael.Cm f 27337423Sbosticconversions, the maximum number of significant digits for 27448351Scael.Cm g 27537423Sbosticand 27648351Scael.Cm G 27737423Sbosticconversions, or the maximum number of characters to be printed from a 27837423Sbosticstring for 27948351Scael.Cm s 28037423Sbosticconversions. 28148351Scael.It 28248351ScaelThe optional character 28348351Scael.Cm h , 28420358Smckusickspecifying that a following 28548351Scael.Cm d , 28648351Scael.Cm i , 28748351Scael.Cm o , 28848351Scael.Cm u , 28948351Scael.Cm x , 29020358Smckusickor 29148351Scael.Cm X 29237423Sbosticconversion corresponds to a 29348351Scael.Em short int 29437423Sbosticor 29548351Scael.Em unsigned short int 29637423Sbosticargument, or that a following 29748351Scael.Cm n 29837423Sbosticconversion corresponds to a pointer to a 29948351Scael.Em short int 30037423Sbosticargument. 30148351Scael.It 30248351ScaelThe optional character 30348351Scael.Cm l 30437423Sbostic(ell) specifying that a following 30548351Scael.Cm d , 30648351Scael.Cm i , 30748351Scael.Cm o , 30848351Scael.Cm u , 30948351Scael.Cm x , 31034643Sbosticor 31148351Scael.Cm X 31248351Scaelconversion applies to a pointer to a 31348351Scael.Em long int 31437423Sbosticor 31548351Scael.Em unsigned long int 31637423Sbosticargument, or that a following 31748351Scael.Cm n 31837423Sbosticconversion corresponds to a pointer to a 31948351Scael.Em long int 32037423Sbosticargument. 32148351Scael.It 32237423SbosticThe character 32348351Scael.Cm L 32437423Sbosticspecifying that a following 32548351Scael.Cm e , 32648351Scael.Cm E , 32748351Scael.Cm f , 32848351Scael.Cm g , 32937423Sbosticor 33048351Scael.Cm G 33137423Sbosticconversion corresponds to a 33248351Scael.Em long double 33337423Sbosticargument (but note that long double values are not currently supported 33448351Scaelby the 33548351Scael.Tn VAX 33648351Scaeland 33748351Scael.Tn Tahoe 33848351Scaelcompilers). 33948351Scael.It 34048351ScaelA character that specifies the type of conversion to be applied. 34148351Scael.El 34248351Scael.Pp 34348351ScaelA field width or precision, or both, may be indicated by 34448351Scaelan asterisk 34548351Scael.Ql * 34648351Scaelinstead of a 34737423Sbosticdigit string. 34848351ScaelIn this case, an 34948351Scael.Em int 35048351Scaelargument supplies the field width or precision. 35137423SbosticA negative field width is treated as a left adjustment flag followed by a 35237423Sbosticpositive field width; a negative precision is treated as though it were 35337423Sbosticmissing. 35448351Scael.Pp 35548351ScaelThe conversion specifiers and their meanings are: 35648351Scael.Bl -tag -width "diouxX" 35748351Scael.It Cm diouxX 35837423SbosticThe 35948351Scael.Em int 36037423Sbostic(or appropriate variant) argument is converted to signed decimal 36148351Scael.Pf ( Cm d 36248351Scaeland 36348351Scael.Cm i ) , 36437423Sbosticunsigned octal 36548351Scael.Pq Cm o , 36637423Sbosticunsigned decimal 36748351Scael.Pq Cm u , 36837423Sbosticor unsigned hexadecimal 36948351Scael.Pf ( Cm x 37048351Scaeland 37148351Scael.Cm X ) 37248351Scaelnotation. The letters 37348351Scael.Cm abcdef 37437423Sbosticare used for 37548351Scael.Cm x 37637423Sbosticconversions; the letters 37748351Scael.Cm ABCDEF 37837423Sbosticare used for 37948351Scael.m X 38037423Sbosticconversions. 38137423SbosticThe precision, if any, gives the minimum number of digits that must 38237423Sbosticappear; if the converted value requires fewer digits, it is padded on 38337423Sbosticthe left with zeros. 38448351Scael.It Cm DOU 38537423SbosticThe 38648351Scael.Em long int 38737423Sbosticargument is converted to signed decimal, unsigned octal, or unsigned 38837423Sbosticdecimal, as if the format had been 38948351Scael.Cm ld , 39048351Scael.Cm lo , 39137423Sbosticor 39248351Scael.Cm lu 39337423Sbosticrespectively. 39437423SbosticThese conversion characters are deprecated, and will eventually disappear. 39548351Scael.It Cm eE 39637423SbosticThe 39748351Scael.Em double 39837423Sbosticargument is rounded and converted in the style 39948351Scael.Sm off 40048351Scael.Pf [\-]d Cm \&. No ddd Cm e No \\*(Pmdd 40148351Scael.Sm on 40248351Scaelwhere there is one digit before the 40348351Scaeldecimal-point character 40448351Scaeland the number of digits after it is equal to the precision; 40548351Scaelif the precision is missing, 40648351Scaelit is taken as 6; if the precision is 40748351Scaelzero, no decimal-point character appears. 40837423SbosticAn 40948351Scael.Cm E 41037423Sbosticconversion uses the letter 41148351Scael.Cm E 41237423Sbostic(rather than 41348351Scael.Cm e ) 41437423Sbosticto introduce the exponent. 41537423SbosticThe exponent always contains at least two digits; if the value is zero, 41637423Sbosticthe exponent is 00. 41748351Scael.It Cm f 41837423SbosticThe 41948351Scael.Em double 42037423Sbosticargument is rounded and converted to decimal notation in the style 42148351Scael.Sm off 42248351Scael.Pf [-]ddd Cm \&. No ddd , 42348351Scael.Sm on 42448351Scaelwhere the number of digits after the decimal-point character 42548351Scaelis equal to the precision specification. 42648351ScaelIf the precision is missing, it is taken as 6; if the precision is 42748351Scaelexplicitly zero, no decimal-point character appears. 42837423SbosticIf a decimal point appears, at least one digit appears before it. 42948351Scael.It Cm g 43037423SbosticThe 43148351Scael.Em double 43248351Scaelargument is converted in style 43348351Scael.Cm f 43437423Sbosticor 43548351Scael.Cm e 43637423Sbostic(or 43748351Scael.Cm E 43837423Sbosticfor 43948351Scael.Cm G 44037423Sbosticconversions). 44137423SbosticThe precision specifies the number of significant digits. 44237423SbosticIf the precision is missing, 6 digits are given; if the precision is zero, 44337423Sbosticit is treated as 1. 44437423SbosticStyle 44548351Scael.Cm e 44637423Sbosticis used if the exponent from its conversion is less than -4 or greater than 44737423Sbosticor equal to the precision. 44837423SbosticTrailing zeros are removed from the fractional part of the result; a 44937423Sbosticdecimal point appears only if it is followed by at least one digit. 45048351Scael.It Cm c 45137423SbosticThe 45248351Scael.Em int 45337423Sbosticargument is converted to an 45448351Scael.Em unsigned char , 45548351Scaeland the resulting character is written. 45648351Scael.It Cm s 45737423SbosticThe 45848351Scael.Dq Em char * 45948351Scaelargument is expected to be a pointer to an array of character type (pointer 46048351Scaelto a string). 46148351ScaelCharacters from the array are written up to (but not including) 46248351Scaela terminating 46348351Scael.Dv NUL 46448351Scaelcharacter; 46548351Scaelif a precision is specified, no more than the number specified are 46648351Scaelwritten. 46748351ScaelIf a precision is given, no null character 46848351Scaelneed be present; if the precision is not specified, or is greater than 46948351Scaelthe size of the array, the array must contain a terminating 47048351Scael.Dv NUL 47148351Scaelcharacter. 47248351Scael.It Cm p 47337423SbosticThe 47448351Scael.Dq Em void * 47548351Scaelpointer argument is printed in hexadecimal (as if by 47648351Scael.Ql %#x 47748351Scaelor 47848351Scael.Ql %#lx ) . 47948351Scael.It Cm n 48037423SbosticThe number of characters written so far is stored into the 48137423Sbosticinteger indicated by the 48248351Scael.Dq Em int * 48337423Sbostic(or variant) pointer argument. 48437423SbosticNo argument is converted. 48548351Scael.It Cm % 48648351ScaelA 48748351Scael.Ql % 48848351Scaelis written. No argument is converted. The complete conversion specification 48948351Scaelis 49048351Scael.Ql %% . 49148351Scael.El 49248351Scael.Pp 49337423SbosticIn no case does a non-existent or small field width cause truncation of 49437423Sbostica field; if the result of a conversion is wider than the field width, the 49548351Scaelfield is expanded to contain the conversion result. 49648351Scael.Pp 49748351Scael.Sh EXAMPLES 49820358Smckusick.br 49920358SmckusickTo print a date and time in the form `Sunday, July 3, 10:02', 50020358Smckusickwhere 50148351Scael.Em weekday 50220358Smckusickand 50348351Scael.Em month 50448351Scaelare pointers to strings: 50548351Scael.Bd -literal -offset indent 50648351Scael#include <stdio.h> 50748723Sdonnfprintf(stdout, "%s, %s %d, %.2d:%.2d\en", 50848351Scael weekday, month, day, hour, min); 50948351Scael.Ed 51048351Scael.Pp 51148351ScaelTo print \*(Pi 51248351Scaelto five decimal places: 51348351Scael.Bd -literal -offset indent 51448351Scael#include <math.h> 51548351Scael#include <stdio.h> 51648723Sdonnfprintf(stdout, "pi = %.5f\en", 4 * atan(1.0)); 51748351Scael.Ed 51848351Scael.Pp 51937423SbosticTo allocate a 128 byte string and print into it: 52048351Scael.Bd -literal -offset indent 52137423Sbostic#include <stdio.h> 52248723Sdonn#include <stdlib.h> 52348723Sdonn#include <stdarg.h> 52448723Sdonnchar *newfmt(const char *fmt, ...) 52537423Sbostic{ 52648723Sdonn char *p; 52748351Scael va_list ap; 52848351Scael if ((p = malloc(128)) == NULL) 52948351Scael return (NULL); 53048723Sdonn va_start(ap, fmt); 53148723Sdonn (void) vsnprintf(p, 128, fmt, ap); 53248351Scael va_end(ap); 53348351Scael return (p); 53437423Sbostic} 53548351Scael.Ed 53648351Scael.Sh SEE ALSO 53748351Scael.Xr printf 1 , 53848351Scael.Xr scanf 3 53948351Scael.Sh STANDARDS 54048723SdonnThe 54148351Scael.Fn fprintf , 54248351Scael.Fn printf , 54348351Scael.Fn sprintf , 54448351Scael.Fn vprintf , 54548351Scael.Fn vfprintf , 54648351Scaeland 54748723Sdonn.Fn vsprintf 54848351Scaelfunctions 54948351Scaelconform to 55048351Scael.St -ansiC . 55148723Sdonn.Sh HISTORY 55248723SdonnThe functions 55348723Sdonn.Fn snprintf 55448723Sdonnand 55548723Sdonn.Fn vsnprintf 55648723Sdonnare new to this release. 55748351Scael.Sh BUGS 55848351ScaelThe conversion formats 55948351Scael.Cm %D , 56048351Scael.Cm %O , 56148351Scaeland 56248351Scael.Cm %U 56348351Scaelare not standard and 56437423Sbosticare provided only for backward compatibility. 56548351ScaelThe effect of padding the 56648351Scael.Cm %p 56748351Scaelformat with zeros (either by the 56848351Scael.Sq Cm 0 56937423Sbosticflag or by specifying a precision), and the benign effect (i.e., none) 57048351Scaelof the 57148351Scael.Sq Cm # 57248351Scaelflag on 57348351Scael.Cm %n 57448351Scaeland 57548351Scael.Cm %p 57648351Scaelconversions, as well as other 57748351Scaelnonsensical combinations such as 57848351Scael.Cm %Ld , 57948351Scaelare not standard; such combinations 58037423Sbosticshould be avoided. 58148351Scael.Pp 58246094SbosticBecause 58348351Scael.Fn sprintf 58446094Sbosticand 58548351Scael.Fn vsprintf 58646094Sbosticassume an infinitely long string, 58746094Sbosticcallers must be careful not to overflow the actual space; 58846094Sbosticthis is often impossible to assure. 58946094SbosticFor safety, programmers should use the 59048351Scael.Fn snprintf 59146094Sbosticinterface instead. 59246094SbosticUnfortunately, this interface is not portable. 593