143083Scael.\" Copyright (c) 1989, 1990 The Regents of the University of California. 236726Sbostic.\" All rights reserved. 336726Sbostic.\" 450202Sbostic.\" This code is derived from software contributed to Berkeley by 550202Sbostic.\" the Institute of Electrical and Electronics Engineers, Inc. 650202Sbostic.\" 7*50524Scael.\" %sccs.include.redist.roff% 836726Sbostic.\" 9*50524Scael.\" @(#)printf.1 5.11 (Berkeley) 07/24/91 1036726Sbostic.\" 1143083Scael.Dd 1243083Scael.Dt PRINTF 1 13*50524Scael.Os 1443083Scael.Sh NAME 1543083Scael.Nm printf 1643083Scael.Nd formatted output 1743083Scael.Sh SYNOPSIS 1843083Scael.Nm printf format 19*50524Scael.Op arguments ... 2043083Scael.Sh DESCRIPTION 2143083Scael.Nm Printf 2236726Sbosticformats and prints its arguments, after the first, under control 2336726Sbosticof the 2443083Scael.Ar format . 2536726SbosticThe 2643083Scael.Ar format 2736726Sbosticis a character string which contains three types of objects: plain characters, 2836726Sbosticwhich are simply copied to standard output, character escape sequences which 2936726Sbosticare converted and copied to the standard output, and format specifications, 3036726Sbosticeach of which causes printing of the next successive 3143083Scael.Ar argument . 3243083Scael.Pp 3336726SbosticThe 3443083Scael.Ar arguments 3536726Sbosticafter the first are treated as strings if the corresponding format is 3636726Sbosticeither 3743083Scael.Cm c 3836726Sbosticor 39*50524Scael.Cm s ; 4036726Sbosticotherwise it is evaluated as a C constant, with the following extensions: 4143083Scael.Pp 42*50524Scael.Bl -bullet -offset indent -compact 43*50524Scael.It 4436726SbosticA leading plus or minus sign is allowed. 45*50524Scael.It 4636726SbosticIf the leading character is a single or double quote, or not a digit, 4736726Sbosticplus, or minus sign, the value is the ASCII code of the next character. 48*50524Scael.El 4943083Scael.Pp 5036726SbosticThe format string is reused as often as necessary to satisfy the 5143083Scael.Ar arguments . 5236726SbosticAny extra format specifications are evaluated with zero or the null 5336726Sbosticstring. 5443083Scael.Pp 5536726SbosticCharacter escape sequences are in backslash notation as defined in the 56*50524Scaeldraft proposed 57*50524Scael.Tn ANSI C 58*50524ScaelStandard 59*50524Scael.Tn X3J11 . 60*50524ScaelThe characters and their meanings 6136726Sbosticare as follows: 62*50524Scael.Bl -tag -width Ds -offset indent 63*50524Scael.It Cm \ea 6436727SbosticWrite a <bell> character. 65*50524Scael.It Cm \eb 6636726SbosticWrite a <backspace> character. 67*50524Scael.It Cm \ef 6836726SbosticWrite a <form-feed> character. 69*50524Scael.It Cm \en 7036726SbosticWrite a <new-line> character. 71*50524Scael.It Cm \er 7236726SbosticWrite a <carriage return> character. 73*50524Scael.It Cm \et 7436726SbosticWrite a <tab> character. 75*50524Scael.It Cm \ev 7636726SbosticWrite a <vertical tab> character. 77*50524Scael.It Cm \e\' 7836727SbosticWrite a <single quote> character. 79*50524Scael.It Cm \e\e 8036726SbosticWrite a backslash character. 81*50524Scael.It Cm \e Ns Ar num 82*50524ScaelWrite an 8-bit character whose 83*50524Scael.Tn ASCII 84*50524Scaelvalue is the 1-, 2-, or 3-digit 8536726Sbosticoctal number 86*50524Scael.Ar num . 87*50524Scael.El 8843083Scael.Pp 8936726SbosticEach format specification is introduced by the percent character 9036726Sbostic(``%''). 91*50524ScaelThe remainder of the format specification includes, 92*50524Scaelin the following order: 93*50524Scael.Bl -tag -width Ds 94*50524Scael.It "Zero or more of the following flags:" 95*50524Scael.Bl -tag -width Ds 96*50524Scael.It Cm # 9743083ScaelA `#' character 9836726Sbosticspecifying that the value should be printed in an ``alternate form''. 9943083ScaelFor 10043083Scael.Cm c , 10143083Scael.Cm d , 10236726Sbosticand 10343083Scael.Cm s , 10436726Sbosticformats, this option has no effect. For the 10543083Scael.Cm o 10636726Sbosticformats the precision of the number is increased to force the first 10736726Sbosticcharacter of the output string to a zero. For the 10843083Scael.Cm x 10943083Scael.Pq Cm X 11036726Sbosticformat, a non-zero result has the string 11143083Scael.Li 0x 11243083Scael.Pq Li 0X 11343083Scaelprepended to it. For 11443083Scael.Cm e , 11543083Scael.Cm E , 11643083Scael.Cm f , 11743083Scael.Cm g , 11836726Sbosticand 11943083Scael.Cm G , 12036726Sbosticformats, the result will always contain a decimal point, even if no 12136726Sbosticdigits follow the point (normally, a decimal point only appears in the 12236726Sbosticresults of those formats if a digit follows the decimal point). For 12343083Scael.Cm g 12436726Sbosticand 12543083Scael.Cm G 12636726Sbosticformats, trailing zeros are not removed from the result as they 12736726Sbosticwould otherwise be; 128*50524Scael.It Cm \&\- 12943083ScaelA minus sign `\-' which specifies 13043083Scael.Em left adjustment 13136726Sbosticof the output in the indicated field; 132*50524Scael.It Cm \&+ 13343083ScaelA `+' character specifying that there should always be 13436726Sbostica sign placed before the number when using signed formats. 135*50524Scael.It Sq \&\ \& 13643083ScaelA space specifying that a blank should be left before a positive number 13736726Sbosticfor a signed format. A `+' overrides a space if both are used; 138*50524Scael.It Cm \&0 13943083ScaelA zero `0' character indicating that zero-padding should be used 14036726Sbosticrather than blank-padding. A `\-' overrides a `0' if both are used; 141*50524Scael.El 142*50524Scael.It "Field Width:" 14343083ScaelAn optional digit string specifying a 14443083Scael.Em field width ; 14536726Sbosticif the output string has fewer characters than the field width it will 14636726Sbosticbe blank-padded on the left (or right, if the left-adjustment indicator 14736726Sbostichas been given) to make up the field width (note that a leading zero 14836726Sbosticis a flag, but an embedded zero is part of a field width); 149*50524Scael.It Precision: 15043083ScaelAn optional period, 15143083Scael.Sq Cm \&.\& , 15243083Scaelfollowed by an optional digit string giving a 15343083Scael.Em precision 15436726Sbosticwhich specifies the number of digits to appear after the decimal point, 15543083Scaelfor 15643083Scael.Cm e 15743083Scaeland 15843083Scael.Cm f 15943083Scaelformats, or the maximum number of characters to be printed 16036726Sbosticfrom a string; if the digit string is missing, the precision is treated 16136726Sbosticas zero; 162*50524Scael.It Format: 16343083ScaelA character which indicates the type of format to use (one of 16443083Scael.Cm diouxXfwEgGcs ) . 165*50524Scael.El 16643083Scael.Pp 16743083ScaelA field width or precision may be 16843083Scael.Sq Cm \&* 16943083Scaelinstead of a digit string. 17036726SbosticIn this case an 17143083Scael.Ar argument 17236726Sbosticsupplies the field width or precision. 17343083Scael.Pp 17436726SbosticThe format characters and their meanings are: 175*50524Scael.Bl -tag -width Fl 176*50524Scael.It Cm diouXx 17736726SbosticThe 17843083Scael.Ar argument 17936729Sbosticis printed as a signed decimal (d or i), unsigned decimal, unsigned octal, 18036729Sbosticor unsigned hexadecimal (X or x), respectively. 181*50524Scael.It Cm f 18236726SbosticThe 18343083Scael.Ar argument 18443083Scaelis printed in the style `[\-]ddd.ddd' where the number of d's 18536726Sbosticafter the decimal point is equal to the precision specification for 18636726Sbosticthe argument. 18744957ScaelIf the precision is missing, 6 digits are given; if the precision 18844957Scaelis explicitly 0, no digits and no decimal point are printed. 189*50524Scael.It Cm eE 19036726SbosticThe 19143083Scael.Ar argument 19243083Scaelis printed in the style 19343083Scael.Cm e 194*50524Scael.`[-]d.ddd Ns \(+-dd\' 19543083Scaelwhere there 19636726Sbosticis one digit before the decimal point and the number after is equal to 19736726Sbosticthe precision specification for the argument; when the precision is 19844957Scaelmissing, 6 digits are produced. 19936726SbosticAn upper-case E is used for an `E' format. 200*50524Scael.It Cm gG 20136726SbosticThe 20243083Scael.Ar argument 20336726Sbosticis printed in style 20443083Scael.Cm f 20536726Sbosticor in style 20643083Scael.Cm e 20743083Scael.Pq Cm E 20836726Sbosticwhichever gives full precision in minimum space. 209*50524Scael.It Cm c 21036726SbosticThe first character of 21143083Scael.Ar argument 21236726Sbosticis printed. 213*50524Scael.It Cm s 21436726SbosticCharacters from the string 21543083Scael.Ar argument 21636726Sbosticare printed until the end is reached or until the number of characters 21736726Sbosticindicated by the precision specification is reached; however if the 21836726Sbosticprecision is 0 or missing, all characters in the string are printed. 219*50524Scael.It Cm \&% 22036726SbosticPrint a `%'; no argument is used. 221*50524Scael.El 22243083Scael.Pp 22336726SbosticIn no case does a non-existent or small field width cause truncation of 22436726Sbostica field; padding takes place only if the specified field width exceeds 22536726Sbosticthe actual width. 226*50524Scael.Sh RETURN VALUES 22743083Scael.Nm Printf 22836811Sbosticexits 0 on success, 1 on failure. 22943083Scael.Sh SEE ALSO 23043083Scael.Xr printf 3 23143083Scael.Sh HISTORY 232*50524ScaelThe 233*50524Scael.Nm printf 234*50524Scaelcommand appeared in 235*50524Scael.Bx 4.3 Reno . 236*50524ScaelIt is modeled 23744957Scaelafter the standard library function, 23844957Scael.Xr printf 3 . 23943083Scael.Sh BUGS 240*50524ScaelSince the floating point numbers are translated from 241*50524Scael.Tn ASCII 242*50524Scaelto floating-point and 24336731Sbosticthen back again, floating-point precision may be lost. 24443083Scael.Pp 245*50524Scael.Tn ANSI 246*50524Scaelhexadecimal character constants were deliberately not provided. 247