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 July 3, 2004 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 48The 49.Nm 50utility formats and prints its arguments, after the first, under control 51of the 52.Ar format . 53The 54.Ar format 55is a character string which contains three types of objects: plain characters, 56which are simply copied to standard output, character escape sequences which 57are converted and copied to the standard output, and format specifications, 58each of which causes printing of the next successive 59.Ar argument . 60.Pp 61The 62.Ar arguments 63after the first are treated as strings if the corresponding format is 64either 65.Cm c , b 66or 67.Cm s ; 68otherwise it is evaluated as a C constant, with the following extensions: 69.Pp 70.Bl -bullet -offset indent -compact 71.It 72A leading plus or minus sign is allowed. 73.It 74If the leading character is a single or double quote, the value is the 75.Tn ASCII 76code of the next character. 77.El 78.Pp 79The format string is reused as often as necessary to satisfy the 80.Ar arguments . 81Any extra format specifications are evaluated with zero or the null 82string. 83.Pp 84Character escape sequences are in backslash notation as defined in the 85.St -ansiC , 86with extensions. 87The characters and their meanings 88are as follows: 89.Pp 90.Bl -tag -width Ds -offset indent -compact 91.It Cm \ea 92Write a <bell> character. 93.It Cm \eb 94Write a <backspace> character. 95.It Cm \ec 96Ignore remaining characters in this string. 97.It Cm \ef 98Write a <form-feed> character. 99.It Cm \en 100Write a <new-line> character. 101.It Cm \er 102Write a <carriage return> character. 103.It Cm \et 104Write a <tab> character. 105.It Cm \ev 106Write a <vertical tab> character. 107.It Cm \e\' 108Write a <single quote> character. 109.It Cm \e\e 110Write a backslash character. 111.It Cm \e Ns Ar num 112.It Cm \e0 Ns Ar num 113Write an 8-bit character whose 114.Tn ASCII 115value is the 1-, 2-, or 3-digit 116octal number 117.Ar num . 118.El 119.Pp 120Each format specification is introduced by the percent character 121(``%''). 122The remainder of the format specification includes, 123in the following order: 124.Bl -tag -width Ds 125.It "Zero or more of the following flags:" 126.Bl -tag -width Ds 127.It Cm # 128A `#' character 129specifying that the value should be printed in an ``alternate form''. 130For 131.Cm c , d , 132and 133.Cm s , 134formats, this option has no effect. 135For the 136.Cm o 137formats the precision of the number is increased to force the first 138character of the output string to a zero. 139For the 140.Cm x 141.Pq Cm X 142format, a non-zero result has the string 143.Li 0x 144.Pq Li 0X 145prepended to it. 146For 147.Cm e , E , f , g , 148and 149.Cm G , 150formats, the result will always contain a decimal point, even if no 151digits follow the point (normally, a decimal point only appears in the 152results of those formats if a digit follows the decimal point). 153For 154.Cm g 155and 156.Cm G 157formats, trailing zeros are not removed from the result as they 158would otherwise be; 159.It Cm \&\- 160A minus sign `\-' which specifies 161.Em left adjustment 162of the output in the indicated field; 163.It Cm \&+ 164A `+' character specifying that there should always be 165a sign placed before the number when using signed formats. 166.It Sq \&\ \& 167A space specifying that a blank should be left before a positive number 168for a signed format. 169A `+' overrides a space if both are used; 170.It Cm \&0 171A zero `0' character indicating that zero-padding should be used 172rather than blank-padding. 173A `\-' overrides a `0' if both are used; 174.El 175.It "Field Width:" 176An optional digit string specifying a 177.Em field width ; 178if the output string has fewer characters than the field width it will 179be blank-padded on the left (or right, if the left-adjustment indicator 180has been given) to make up the field width (note that a leading zero 181is a flag, but an embedded zero is part of a field width); 182.It Precision: 183An optional period, 184.Sq Cm \&.\& , 185followed by an optional digit string giving a 186.Em precision 187which specifies the number of digits to appear after the decimal point, 188for 189.Cm e 190and 191.Cm f 192formats, or the maximum number of characters to be printed 193from a string; if the digit string is missing, the precision is treated 194as zero; 195.It Format: 196A character which indicates the type of format to use (one of 197.Cm diouxXfFeEgGaAcsb ) . 198The uppercase formats differ from their lowercase counterparts only in 199that the output of the former is entirely in uppercase. 200.El 201.Pp 202A field width or precision may be 203.Sq Cm \&* 204instead of a digit string. 205In this case an 206.Ar argument 207supplies the field width or precision. 208.Pp 209The format characters and their meanings are: 210.Bl -tag -width Fl 211.It Cm diouXx 212The 213.Ar argument 214is printed as a signed decimal (d or i), unsigned octal, unsigned decimal, 215or unsigned hexadecimal (X or x), respectively. 216.It Cm fF 217The 218.Ar argument 219is printed in the style `[\-]ddd.ddd' where the number of d's 220after the decimal point is equal to the precision specification for 221the argument. 222If the precision is missing, 6 digits are given; if the precision 223is explicitly 0, no digits and no decimal point are printed. 224The values \*[If] and \*[Na] are printed as 225.Ql inf 226and 227.Ql nan , 228respectively. 229.It Cm eE 230The 231.Ar argument 232is printed in the style 233.Cm e 234.Sm off 235.Sq Op - Ar d.ddd No \(+- Ar dd 236.Sm on 237where there 238is one digit before the decimal point and the number after is equal to 239the precision specification for the argument; when the precision is 240missing, 6 digits are produced. 241The values \*[If] and \*[Na] are printed as 242.Ql inf 243and 244.Ql nan , 245respectively. 246.It Cm gG 247The 248.Ar argument 249is printed in style 250.Cm f 251.Pq Cm F 252or in style 253.Cm e 254.Pq Cm E 255whichever gives full precision in minimum space. 256.It Cm aA 257The 258.Ar argument 259is printed in style 260.Sm off 261.Sq Op - Ar h.hhh No \(+- Li p Ar d 262.Sm on 263where there is one digit before the hexadecimal point and the number 264after is equal to the precision specification for the argument; 265when the precision is missing, enough digits are produced to convey 266the argument's exact double-precision floating-point representation. 267The values \*[If] and \*[Na] are printed as 268.Ql inf 269and 270.Ql nan , 271respectively. 272.It Cm c 273The first character of 274.Ar argument 275is printed. 276.It Cm s 277Characters from the string 278.Ar argument 279are printed until the end is reached or until the number of characters 280indicated by the precision specification is reached; however if the 281precision is 0 or missing, all characters in the string are printed. 282.It Cm b 283As for 284.Cm s , 285but interpret character escapes in backslash notation in the string 286.Ar argument . 287.It Cm \&% 288Print a `%'; no argument is used. 289.El 290.Pp 291The decimal point 292character is defined in the program's locale (category 293.Dv LC_NUMERIC ) . 294.Pp 295In no case does a non-existent or small field width cause truncation of 296a field; padding takes place only if the specified field width exceeds 297the actual width. 298.Sh DIAGNOSTICS 299.Ex -std 300.Sh COMPATIBILITY 301The traditional 302.Bx 303behavior of converting arguments of numeric formats not beginning 304with a digit to the 305.Tn ASCII 306code of the first character is not supported. 307.Sh SEE ALSO 308.Xr echo 1 , 309.Xr printf 3 310.Sh STANDARDS 311The 312.Nm 313command is expected to be compatible with the 314.St -p1003.2 315specification. 316.Sh HISTORY 317The 318.Nm 319command appeared in 320.Bx 4.3 Reno . 321It is modeled 322after the standard library function, 323.Xr printf 3 . 324.Sh BUGS 325Since the floating point numbers are translated from 326.Tn ASCII 327to floating-point and 328then back again, floating-point precision may be lost. 329.Pp 330.Tn ANSI 331hexadecimal character constants were deliberately not provided. 332.Pp 333The escape sequence \e000 is the string terminator. 334When present in the 335.Ar format , 336the 337.Ar format 338will be truncated at the \e000 character. 339.Pp 340Multibyte characters are not recognized in format strings (this is only 341a problem if 342.Ql % 343can appear inside a multibyte character). 344