1.\" Copyright (c) 1989, 1990, 1993 2.\" The Regents of the University of California. All rights reserved. 3.\" 4.\" This code is derived from software contributed to Berkeley by 5.\" the Institute of Electrical and Electronics Engineers, Inc. 6.\" 7.\" Redistribution and use in source and binary forms, with or without 8.\" modification, are permitted provided that the following conditions 9.\" are met: 10.\" 1. Redistributions of source code must retain the above copyright 11.\" notice, this list of conditions and the following disclaimer. 12.\" 2. Redistributions in binary form must reproduce the above copyright 13.\" notice, this list of conditions and the following disclaimer in the 14.\" documentation and/or other materials provided with the distribution. 15.\" 3. All advertising materials mentioning features or use of this software 16.\" must display the following acknowledgement: 17.\" This product includes software developed by the University of 18.\" California, Berkeley and its contributors. 19.\" 4. Neither the name of the University nor the names of its contributors 20.\" may be used to endorse or promote products derived from this software 21.\" without specific prior written permission. 22.\" 23.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33.\" SUCH DAMAGE. 34.\" 35.\" @(#)printf.1 8.1 (Berkeley) 6/6/93 36.\" $FreeBSD$ 37.\" 38.Dd June 6, 1993 39.Dt PRINTF 1 40.Os 41.Sh NAME 42.Nm printf 43.Nd formatted output 44.Sh SYNOPSIS 45.Nm 46.Ar format Op Ar arguments ... 47.Sh DESCRIPTION 48.Nm Printf 49formats and prints its arguments, after the first, under control 50of the 51.Ar format . 52The 53.Ar format 54is a character string which contains three types of objects: plain characters, 55which are simply copied to standard output, character escape sequences which 56are converted and copied to the standard output, and format specifications, 57each of which causes printing of the next successive 58.Ar argument . 59.Pp 60The 61.Ar arguments 62after the first are treated as strings if the corresponding format is 63either 64.Cm c 65or 66.Cm s ; 67otherwise it is evaluated as a C constant, with the following extensions: 68.Pp 69.Bl -bullet -offset indent -compact 70.It 71A leading plus or minus sign is allowed. 72.It 73If the leading character is a single or double quote, or not a digit, 74plus, or minus sign, the value is the ASCII code of the next character. 75.El 76.Pp 77The format string is reused as often as necessary to satisfy the 78.Ar arguments . 79Any extra format specifications are evaluated with zero or the null 80string. 81.Pp 82Character escape sequences are in backslash notation as defined in the 83.St -ansiC . 84The characters and their meanings 85are as follows: 86.Pp 87.Bl -tag -width Ds -offset indent -compact 88.It Cm \ea 89Write a <bell> character. 90.It Cm \eb 91Write a <backspace> character. 92.It Cm \ef 93Write a <form-feed> character. 94.It Cm \en 95Write a <new-line> character. 96.It Cm \er 97Write a <carriage return> character. 98.It Cm \et 99Write a <tab> character. 100.It Cm \ev 101Write a <vertical tab> character. 102.It Cm \e\' 103Write a <single quote> character. 104.It Cm \e\e 105Write a backslash character. 106.It Cm \e Ns Ar num 107Write an 8-bit character whose 108.Tn ASCII 109value is the 1-, 2-, or 3-digit 110octal number 111.Ar num . 112.El 113.Pp 114Each format specification is introduced by the percent character 115(``%''). 116The remainder of the format specification includes, 117in the following order: 118.Bl -tag -width Ds 119.It "Zero or more of the following flags:" 120.Bl -tag -width Ds 121.It Cm # 122A `#' character 123specifying that the value should be printed in an ``alternate form''. 124For 125.Cm c , 126.Cm d , 127and 128.Cm s , 129formats, this option has no effect. For the 130.Cm o 131formats the precision of the number is increased to force the first 132character of the output string to a zero. For the 133.Cm x 134.Pq Cm X 135format, a non-zero result has the string 136.Li 0x 137.Pq Li 0X 138prepended to it. For 139.Cm e , 140.Cm E , 141.Cm f , 142.Cm g , 143and 144.Cm G , 145formats, the result will always contain a decimal point, even if no 146digits follow the point (normally, a decimal point only appears in the 147results of those formats if a digit follows the decimal point). For 148.Cm g 149and 150.Cm G 151formats, trailing zeros are not removed from the result as they 152would otherwise be; 153.It Cm \&\- 154A minus sign `\-' which specifies 155.Em left adjustment 156of the output in the indicated field; 157.It Cm \&+ 158A `+' character specifying that there should always be 159a sign placed before the number when using signed formats. 160.It Sq \&\ \& 161A space specifying that a blank should be left before a positive number 162for a signed format. A `+' overrides a space if both are used; 163.It Cm \&0 164A zero `0' character indicating that zero-padding should be used 165rather than blank-padding. A `\-' overrides a `0' if both are used; 166.El 167.It "Field Width:" 168An optional digit string specifying a 169.Em field width ; 170if the output string has fewer characters than the field width it will 171be blank-padded on the left (or right, if the left-adjustment indicator 172has been given) to make up the field width (note that a leading zero 173is a flag, but an embedded zero is part of a field width); 174.It Precision: 175An optional period, 176.Sq Cm \&.\& , 177followed by an optional digit string giving a 178.Em precision 179which specifies the number of digits to appear after the decimal point, 180for 181.Cm e 182and 183.Cm f 184formats, or the maximum number of characters to be printed 185from a string; if the digit string is missing, the precision is treated 186as zero; 187.It Format: 188A character which indicates the type of format to use (one of 189.Cm diouxXfwEgGcs ) . 190.El 191.Pp 192A field width or precision may be 193.Sq Cm \&* 194instead of a digit string. 195In this case an 196.Ar argument 197supplies the field width or precision. 198.Pp 199The format characters and their meanings are: 200.Bl -tag -width Fl 201.It Cm diouXx 202The 203.Ar argument 204is printed as a signed decimal (d or i), unsigned octal, unsigned decimal, 205or unsigned hexadecimal (X or x), respectively. 206.It Cm f 207The 208.Ar argument 209is printed in the style `[\-]ddd.ddd' where the number of d's 210after the decimal point is equal to the precision specification for 211the argument. 212If the precision is missing, 6 digits are given; if the precision 213is explicitly 0, no digits and no decimal point are printed. 214.It Cm eE 215The 216.Ar argument 217is printed in the style 218.Cm e 219.Sm off 220.Sq Op - Ar d.ddd No \(+- Ar dd 221.Sm on 222where there 223is one digit before the decimal point and the number after is equal to 224the precision specification for the argument; when the precision is 225missing, 6 digits are produced. 226An upper-case E is used for an `E' format. 227.It Cm gG 228The 229.Ar argument 230is printed in style 231.Cm f 232or in style 233.Cm e 234.Pq Cm E 235whichever gives full precision in minimum space. 236.It Cm c 237The first character of 238.Ar argument 239is printed. 240.It Cm s 241Characters from the string 242.Ar argument 243are printed until the end is reached or until the number of characters 244indicated by the precision specification is reached; however if the 245precision is 0 or missing, all characters in the string are printed. 246.It Cm \&% 247Print a `%'; no argument is used. 248.El 249.Pp 250The decimal point 251character is defined in the program's locale (category 252.Dv LC_NUMERIC ) . 253.Pp 254In no case does a non-existent or small field width cause truncation of 255a field; padding takes place only if the specified field width exceeds 256the actual width. 257.Sh DIAGNOSTICS 258.Ex -std 259.Sh SEE ALSO 260.Xr printf 3 261.Sh HISTORY 262The 263.Nm 264command appeared in 265.Bx 4.3 Reno . 266It is modeled 267after the standard library function, 268.Xr printf 3 . 269.Sh BUGS 270Since the floating point numbers are translated from 271.Tn ASCII 272to floating-point and 273then back again, floating-point precision may be lost. 274.Pp 275.Tn ANSI 276hexadecimal character constants were deliberately not provided. 277.Pp 278The escape sequence \e000 is the string terminator. When present in the 279.Ar format , 280the 281.Ar format 282will be truncated at the \e000 character. 283