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