Printf () Output format control

Source: Internet
Author: User

int printf (const char *format,[argument]);

Format parameter output, defined in the format:

  %[FLAGS][WIDTH][.PERC] [f| N|h|l]type

Specify the data output mode, as follows:

  1.type meanings are as follows:

integers          %d integer parameter will be converted to a signed decimal digit         The parameters of the  %u integer are converted to an unsigned decimal number          %o Integer parameters are converted to an unsigned octal number         %x Integer parameters are converted to an unsigned hexadecimal number and are represented in lowercase abcdef          %x integer is converted to an unsigned hexadecimal number, and the floating-point number is represented in uppercase abcdef

    %f double The parameters of the type are converted to decimal digits and rounded up to six digits below the decimal point.         %e the parameters of the double type are printed in exponential form, There is a number before the decimal point, six digits after the decimal point, and in the exponent part is indicated by the lowercase e.         %e the same as%E, the only difference is that the exponential portion will be in uppercase E To express.         %g the parameters of the double type are automatically selected to%f or%e Format to print, the standard is determined by the number of values to be printed and the amount of significant digits that are set.         %g is the same as%G, and the only difference is that it is selected when printing in exponential form %E format.

the characters and the parameters of the string%c integer are converted to unsigned char-typed. %s The argument to the string is output verbatim until the null character appears %p if the argument is a "void *" pointer, it is displayed in hexadecimal format, and the near pointer is expressed as: the Xxxx,far pointer is represented as: xxxx:yyyy

Percent sign itself 

%n The connected parameter should be a pointer, which holds the number of characters

  2.flags specified output format, values and meanings are as follows:

No right alignment, left padding 0 and spaces

-left-justified, right padding space

+ Add symbol + or-in front of number

A space only displays symbols for negative numbers

# no effect when Type=c,s,d,i,u

type=o,x,x, add ' 0 ', ' 0x ', ' 0X ' before the value

type=e,e,f, always use the decimal point

when Type=g,g, the decimal point is always displayed in addition to the value 0

The 3.width is used to control the width of the displayed values, and the values and meanings are as follows

N (n=1,2,3 ...) width is at least n bits, not enough to fill with spaces

0n (n=1,2,3 ...) width of at least n bits, not enough left to fill with 0 *

lattice Type list, the next parameter or width

The 4..prec is used to control the number of digits after the decimal point, and the values and meanings are as follows :

No default precision Display

0 when type=d,i,o,u,x, no effect

type=e,e,f, the decimal point is not displayed

N (n=1,2,3 ...) the maximum number of digits represented when type=e,e,f

type= Other, indicates the maximum width of the display. *

The next argument or width in the format list

5.f| N|h|l Indicates whether the pointer is a far pointer or whether the integer is a long integer

F far pointer

N near Pointer

H short integer or single-precision floating-point number

L Long integer or double precision floating-point number

-------------------------------------------------------------------------------------------

1. General format

printf (format control, output table column)

For example: printf ("i=%d,ch=%c\n", i,ch);

Description

(1) "Format Control" is a string enclosed in double apostrophes, also known as a "conversion control string," which consists of two kinds of information:

① Format Description: Consists of a "%" and a format character, which is the function of converting the output data to the specified format output.

② ordinary characters, which are characters that need to be output as-is.

(2) "Output table column" is some data that needs to be output, it can be an expression

(3) The general form of the printf function can be expressed as

printf (parameter 1, Parameter 2, ..., parameter n)

The function is to output the parameter 2~ parameter N in the format given by parameter 1

2. Format characters (9 types)

(1) d (or i) format symbol. Use to output decimal integers in the following ways:

①%d, output by the actual length of the integer data.

②%md,m the width of the specified output field. If the number of bits in the data is less than M, then the left is a space, and if it is greater than M, the actual number of digits is output.

③%ld (%mld also), output long integer data.

For example: Long a=123456;

printf ("%ld", a);

(2) o format character, which outputs integers as octal numbers. Format:%o,%mo,%lo,%mlo is available.

(3) An X (or x) format character that outputs an integer in hexadecimal form. Format:%X,%MX,%LX,%MLX is available.

(4) U format character, used to output unsigned type data, that is, unsigned number, in the form of a decimal number output. Format:%u,%mu,%lu is available.

See also: li4-3.c

(5) C format character, which is used to output a character. Format:%C,%MC is available.

(6) S-format character, which is used to output a string. Format:%S,%MS,%-MS,%M.NS,%-M.NS is available.

See also: li4-5.c

(7) The F format character, which is used to output real numbers (including single and double precision) and output in decimal form. Format:%F,%M.NF,%-M.NF is available.

Note: The number of valid digits for single-precision real numbers is typically 7 bits, and the double-precision is 16-bit.

See also: LI4-6.C

Li4-7.c

Li4-8.c

(8) The E (or e) format character, which outputs the real number in exponential form. Format:%e,%m.ne,%-m.ne is available.

(9) the G (or G) format character, which is used to output a real number, is automatically selected in f format or e format (one of the smaller widths when the output is selected), depending on the size of the value.

3. Description

(1) In addition to X, E, G (denoted in uppercase letters), other format characters must be in lowercase letters;

(2) The "format control" string can contain escape characters;

(3) If you want to output the character "%", you should indicate in the "format control" string a continuous two%, such as:

printf ("%f%%", 1.0/3);

(4) Format character table see below

Table 4.1 printf Format characters

Format character description

D,i output integers in signed decimal form (positive not output symbols)

o output integers unsigned as octal (does not output leader 0)

x,x output integers in hexadecimal unsigned form (not output leader 0x), output hexadecimal number a~f with x in lowercase, and when x, output in uppercase letters

u output integers in unsigned decimal form

C Output as a character, only one character is output

S output string

F output single, double, and implicitly output 6 decimal digits in decimal form

E,e outputting real numbers as an exponent

G,G selects a format with a shorter output width in the%f or%e format and does not output meaningless 0

Table 4.2 printf Additional format Description characters

Character

Description

Letter L

For long integers, can be added in front of the format symbol D, o, x, u

M (represents a positive integer)

Minimum data width

N (represents a positive integer)

For real numbers, the output n decimal places, and the number of characters intercepted for a string.

-

The number or character of the output is left-leaning within the field

------------------------------------------------------------------------------------

Use

Write formatted output.

Grammar

printf Format [Argument ...]

Describe

The printf command transforms, formats, and writes Argument parameters to standard output. The Argument parameter is formatted by the format parameter control. The formatted output line cannot exceed the Line_max byte length.

The following environment variables affect the execution of the printf command:

LANG determines the locale used for locale cataloging when lc_all and the corresponding environment variables (beginning with lc_) do not specify a locale.

Lc_all determines the locale used to overwrite the catalog values for any locale set by LANG or any other LC_ environment variable.

LC_CTYPE determines the locale in which the text byte data order is interpreted as a character, for example, a single byte parameter that corresponds to multibyte characters.

Lc_messages determines the language to use for writing messages.

Lc_numeric determines the locale of the digital format orchestration. This environment variable affects the format of numbers written using the E, E, F, G, and G conversion characters.

The Format parameter is a string that contains three types of objects:

* Unformatted characters are copied to the output stream.

* Conversion specification, each specification causes 0 or more items to be retrieved in the value parameter list.

* The following escape sequences. When copied to the output stream, these sequences cause their related actions to be displayed on devices that have this capability:

\ \ counter Slash

\a Warning

\b Backspace

\f Page Change

\ nthe line break

\ r Enter

\ t Jump lattice

\v Vertical Jump Lattice

\DDD DDD is a 1, 2, or 3-bit octal number. These escape sequences are displayed as bytes with numeric values specified by the octal number.

The Argument parameter is a list of one or more strings that are written to standard output under the control of the Format parameter.

The Format parameter is often reused if necessary to satisfy the Argument parameter. Any additional C or S conversion specifications will be evaluated as if an empty string Argument is provided, and other additional conversion specifications will be evaluated as if they provided 0 Argument. Here the Format parameter does not contain conversion specifications only the Argument parameter appears, and the result is indeterminate.

The conversion specification in each Format parameter has the following syntax in order:

1.% (Percent semicolon).

2.0 or more options to modify the meaning of the conversion specification. The option characters and their meanings are:

-the result of the conversion is left-aligned in the field.

+ symbol conversion results are often started with symbols (+ or-).

Spaces if the first character of the symbol conversion is not a symbol, the result prefix will be a space. If spaces and + option characters are displayed, the empty cells entry character is ignored.

# This option specifies that the value is converted to an alternate format. For C, D, I, u, and S conversions, the option has no effect. For the O conversion, the first number that increases the precision to force the result is a, 0 (0). For x and X conversions, non-0 results have 0x or 0X prefixes, respectively. For E, E, F, G, and G conversions, the result usually contains a base number character, even if there is no number after the base number character. For G and G conversions, the end 0 is not removed as usual.

0 for the D, I, O, u, x, E, E, F, G, and G conversions, the leading 0 (followed by the symbol or base) is used to fill the field width and will not be filled with spaces. If the 0 (0) and-(minus) options are displayed, the 0 (0) option is ignored. For D, I, O, U, x, and X conversions, if you specify precision, the 0 (0) option is ignored.

Note:

Other conversions that do not define their behavior.

3. Optional decimal number string specifying the width of the minimum field. If the converted value character is less than the field width, the field is populated from left to right by the specified field width. If the left adjustment option is specified, the field is populated on the right. If the conversion result is wider than the field width, the field is expanded to include the converted results. No truncation occurs. However, small precision can cause truncation to occur on the right.

4. Optional accuracy. Accuracy is one. (dot) followed by a decimal digit string. If the accuracy is not given, press 0 (0) to treat. Precision specified:

* Minimum number of digits for D, O, I, u, x, or X conversion.

* The minimum number of digits to display after the base number of the E and F conversions.

* The maximum number of significant digits for the G conversion.

* The maximum number of print bytes in a string in the S conversion.

5. A character indicating the type of conversion to apply, for example:

% is not converted. Prints a percent (percent semicolon).

D, I accept an integer value and convert it to a signed decimal notation notation. The precision specifies the minimum number of digits to display. If the value is converted and can be represented with fewer digits, the leading 0 extension is used. The default precision is 1. The result of a zero-precision 0 value conversion is an empty string. Specifies the field width with 0 as the leading character, causing the field width value to be populated with leading 0.

o accepts an integer value and converts it to a signed octal notation notation. The precision specifies the minimum number of digits to display. If the value is converted and can be represented with fewer digits, the leading 0 extension is used. The default precision is 1. The result of a zero-precision 0 value conversion is an empty string. Specifies the field width with 0 as the leading character, causing the field width value to be populated with leading 0. The field width is not represented by an octal value.

U accepts an integer value and converts it to an unsigned decimal notation notation. The precision specifies the minimum number of digits to display. If the value is converted and can be represented with fewer digits, the leading 0 extension is used. The default precision is 1. The result of a zero-precision 0 value conversion is an empty string. Specifies the field width with 0 as the leading character, causing the field width value to be populated with leading 0.

X, x accepts an integer value and converts it to a hexadecimal notation notation. The letter abcdef is used for x conversion, and the letter abcdef is used for x conversion. The precision specifies the minimum number of digits to display. If the value is converted and can be represented with fewer digits, the leading 0 extension is used. The default precision is 1. The result of a zero-precision 0 value conversion is an empty string. Specifies the field width with 0 as the leading character, causing the field width value to be populated with leading 0.

F accepts a floating-point or double-precision value and converts it to a decimal notation notation in the form [-] ddd.ddd. The number of digits after the base number character (shown here as a decimal point) equals the specified precision. The lc_numeric locale catalog determines the base number characters used in this format. If you do not specify a precision, the output is six digits. If the precision is 0 (0), the base number character is not displayed.

E, E accepts a floating-point or double-precision value and converts it to an exponential representation of the form [-] d.dde{+|-}dd. There is a number in front of the base number character (shown here as a decimal point), and the number of digits after the base number character equals the specified precision. The lc_numeric locale catalog determines the base number characters used in this format. If you do not specify a precision, the output is six digits. If the precision is 0 (0), the base number character is not displayed. The e conversion character generates a number with E instead of e before the exponent. An index usually contains at least two digits. However, if you want to print an exponential value greater than two digits, you need to print an additional exponential number if necessary.

G, G accepts floating-point and double values and converts the style to F or e conversion characters (or E in case of G conversion), specifying the number of valid digits with precision. Tail 0 will be removed from the result. The base digit character is displayed only when it is followed by a number. The style used depends on the value of the conversion. The style g is used only when the index result of the conversion is less than-4, or greater than or equal to precision.

C accepts a value as a string and prints the first character in a string.

s accepts a value as a string and prints the characters in the string until the string ends or the number of characters indicated by the precision is reached. If you do not specify a precision, all characters are printed until the first null character appears.

The B accepts the value as a string and may contain a backslash escape sequence. Prints the bytes from the converted string until the end of the string or the number of bytes indicated by the precision specification. If you do not specify a precision, all bytes are printed until the first null character appears.

The following backslash escape sequences are supported:

* The backslash escape sequence listed earlier is under the Format parameter description. These escape sequences are converted to the single character they represent.

* \c (backslash c) sequence, which does not display and causes the printf command to ignore all remaining string parameters, all remaining string arguments, and all additional characters in the string parameter in the Format parameter.

Exit status

The command returns the following exit values:

0 completed successfully.

>0 an error occurred.

Example

1. Enter the following command:

printf "%5d%4d\n" 1 21 321 4321 54321

Produces the following output:

1 21

3214321

54321 0

Use the Format parameter three times to print all the given strings. 0 (0) is provided by the printf command to meet the final%4D conversion specifications.

2. Enter the following command

printf "%c%c\n" 78 79

Produces the following output:

7 7

File

The/usr/bin/printf contains the printf command.

----------------------------------------Supplement:

Http://blog.csdn.net/TrueLie/archive/2007/07/15/1692942.aspx

Transferred from: http://blog.sina.com.cn/s/blog_48964cb10100lg1v.html

Printf () Output format control (RPM)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.