printf formatted output function __ function in C language

Source: Internet
Author: User
Tags locale
Usage

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

Format for the output of the format parameter, defined as:

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

Specify the mode of data output, as follows:

1.type meanings are as follows:

D signed 10 binary integers

I signed 10 binary integers

O Signed 8 binary integers

U unsigned 10-binary integer

x unsigned 16 digits, expressed in lowercase abcdef

X unsigned 16 digits, expressed in uppercase abcdef

f/f floating-point numbers

e/e floating-point numbers in scientific representation format

G uses the total number of digits in the%f and%e representations to represent the shortest representation of the floating-point G and G format, but is expressed as an exponential

C Single character

s string

% shows percent per se

P displays a pointer, near pointer as: XXXX

The far pointer is expressed as: xxxx:yyyy

The N-connected parameter should be a pointer to the number of characters

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

No right alignment, padding 0 and spaces on the left

-left-aligned, padding space on the right

+ Add symbol + or before number-

A space displays symbols only for negative numbers

# There's no effect when type=c,s,d,i,u

Type=o,x,x, add ' 0 ', ' 0x ', ' 0X ' before the value.

Always use a decimal point when type=e,e,f

When Type=g,g, the decimal point is always displayed except for a value of 0

The 3.width is used to control the width of the display values, with values and meanings as follows N (n=1,2,3 ...) at least n bits, not enough to fill with spaces

0n (n=1,2,3 ...) width is at least n bit, not enough left to fill the 0 * lattice

List, the next argument or width

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

No display by default precision

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

The decimal point is not displayed when Type=e,e,f

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

Type= Other, represents the maximum width of the display. *

In the format list, the next argument or width

5.f| N|h|l Indicates whether the pointer is a remote 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 by a double apostrophe, also known as a "conversion control string", which includes two kinds of information:

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

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

(2) "Output table column" is some data that needs to be output, 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 kinds)

(1) d (or i) format character. Used to output decimal integers, there are several uses:

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

②%md,m is the width of the specified output field. If the number of digits in the data is less than M, then the left is filled with spaces, and if greater than M, the actual digits are output.

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

For example: Long a=123456;

printf ("%ld", a);

(2) The O format character, which outputs an integer in octal number form. Format:%o,%mo,%lo,%mlo are available.

(3) The X (or x) format character that outputs an integer as a hexadecimal number. Format:%X,%MX,%LX,%MLX are available.

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

See also: li4-3.c/* unsigned data output * *

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

(6) s format character, used to output a string. Format:%s,%ms,%-ms,%m.ns,%-m.ns are available.

See also: li4-5.c/* String Output/*

(7) F format character, used to output real numbers (including single, double precision), in decimal form output. Format:%f,%m.nf,%-m.nf are available.

Note: The effective digits of the single precision real number are generally 7 digits, and the double precision is 16 bits.

See also: li4-6.c/* output single precision real number of significant digits * *

li4-7.c/* the number of valid digits for the output of double precision real numbers * *

li4-8.c/* Specifies the number of decimal places when outputting real numbers * *

(8) e (or e) format character, outputting the real number in exponential form. Format:%e,%m.ne,%-m.ne are available.

(9) the G (or G) format character, which is used to output real numbers, which automatically select the F format or the E format depending on the size of the value (choose a smaller width for the output).

3. Notes

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

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

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

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

(4) Format character chart see table below

Table 4.1 printf Format characters

Format character description

D,i output integer in signed decimal notation (positive not output symbol)

o An integer output in octal unsigned form (no output leader 0)

X,x an integer in hexadecimal unsigned (no output leader 0x), output hexadecimal a~f in lowercase when x, and output in uppercase letters with X

u output integers in unsigned decimal form

C output in character form, output only one character

S output string

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

E,e output real numbers in exponential form

G,G Select a format with a shorter output width in the%f or%e format without outputting meaningless 0

Table 4.2 Additional format specifier characters for printf

Character

Description

Letter L

For long integer integers, which can be added in front of the format character D, O, X, and U

M (represents a positive integer)

Minimum data width

N (represents a positive integer)

For a real number, an output n-bit decimal; A string that represents the amount of characters intercepted

-

The number or character of the output is left in the field by an example:

#include <stdio.h>
int main ()
{
printf ("hello,world/n");
while (1);
#include <stdio.h>
int main ()
{
int i = 1, j =2;
printf ("%d%d/n", i,j);
while (1);
}


printf Command

  Use

Write formatted output.

  Grammar

printf Format [Argument ...]

  Description

printf commands convert, format, and write Argument parameters to standard output. The Argument parameter is formatted by the format parameter control. The formatted output line cannot exceed the length of Line_max bytes.

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

LANG determines the locale in which the locale catalog is used when Lc_all and the corresponding environment variable (beginning with lc_) do not specify the language environment.

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

LC_CTYPE determines the locale in which the order of text byte data is interpreted as a character, for example, a single byte corresponding to the parameters of a multibyte character.

Lc_messages determines the language used to write messages.

Lc_numeric determines the locale in which the number format is organized. This environment variable affects the format of numbers written with 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 sequence. When copied to the output stream, these sequences cause their associated operations to appear on devices that have this feature:

Back slash

/A warning

/b BACKSPACE

/F Page Change

/N Line Wrap

/R Enter

/T Jump grid

/V Vertical Jumps

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

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 frequently reused when necessary to satisfy the Argument parameter. Evaluates any additional C or S conversion specification as if it provided an empty string Argument; Other additional conversion specifications will be evaluated as if they provided 0 Argument. The Format parameter here does not contain the Argument parameter only if the conversion specification appears, and the result is indeterminate.

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

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 transformation is left-aligned in the field.

+ Symbolic conversion results often begin with a symbol (+ or-).

Space if the first character of a symbol conversion is not a symbol, the result will be prefixed by a space. If both the space and the + option characters are displayed, the null cell character is ignored.

# This option specifies that the value is converted to an alternate format. For C, D, I, u, and S conversions, options do not work. For the O conversion, it increases the precision to force the result that the first number 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 the base number character, even though there are no digits after the base number character. For G and G conversions, the end 0 is not removed as usual.

0 for D, I, O, u, x, E, E, F, G, and G conversions, leading 0 (followed by the symbol or base) is used to fill the field width, and will not be padded 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, the 0 (0) option is ignored if the precision is specified.

Note:

Other transformations, which do not define their behavior.

3. Optional decimal digit 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 you specify a left adjustment option, the field is populated on the right. If the result of the conversion is wider than the field width, the field is extended to include the converted result. Truncation does not occur. However, small precision can result in truncation on the right.

4. Optional precision. Precision is one. (dots) followed by a decimal digit string. If no precision is given, press 0 (0) for treatment. Precision specified:

* d, O, I, u, x, or x the minimum number of digits to be displayed for conversion.

* E and F convert the base digit character after the minimum number of digits displayed.

* g The maximum number of significant digits converted.

The maximum number of bytes printed in the string in the * s conversion.

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

% does not convert. Print a% (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 by a smaller number of digits, a leading 0 extension is used. The default precision is 1. The result of a 0 value conversion with a precision of zero is an empty string. Specify the field width with 0 as the leading character, causing the field width value to be filled with a leading 0.

o accepts an integer value and converts it to a signed octal notation. The precision specifies the minimum number of digits to display. If the value is converted and can be represented by a smaller number of digits, a leading 0 extension is used. The default precision is 1. The result of a 0 value conversion with a precision of zero is an empty string. Specify the field width with 0 as the leading character, causing the field width value to be filled with a leading 0. The field width is not represented by a 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 by a smaller number of digits, a leading 0 extension is used. The default precision is 1. The result of a 0 value conversion with a precision of zero is an empty string. Specify the field width with 0 as the leading character, causing the field width value to be filled with a leading 0.

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

F accepts a floating-point or double-precision value and converts it to decimal notation, in the form [-] ddd.ddd. The number of digits in the base number character (shown here as a decimal point) equals the specified precision. Lc_numeric The language environment catalog determines the base number characters that are used in this format. If no precision is specified, six digits are output. 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 [-] d.dde{+|-}dd. There is a number (shown here as a decimal point) before the base number character, and the number of digits after the base number is equal to the specified precision. Lc_numeric The language environment catalog determines the base number characters that are used in this format. If no precision is specified, six digits are output. 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. Indices usually contain at least two digits. However, if you want to print an exponential value greater than two digits, you need to print additional exponent numbers if necessary.

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

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

s accepts the 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 no precision is specified, all characters are printed until the first null character appears.

b accepts the value as a string and may contain a backslash escape sequence. Prints bytes from the converted string until the end of the string or the number of bytes indicated by the precision specification. If no precision is specified, all bytes are printed until the first null character appears.

The following backslash escape sequences are supported:

* The backslash escape sequence listed earlier is described in the Format parameter. These escape sequences are converted to the individual characters they represent.

*/C (backslash C) sequence, which does not display and causes the printf command to ignore all remaining strings, all remaining string parameters, and all additional characters contained in the string parameter in the Format parameter.

Exit status

This command returns the following export values:

0 completed successfully.

>0 an error occurred.

  Sample

1. Enter the following command:

printf ("%5d%4d/n", 1213,43);

Produces the following output:

_1213_ _43

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

2. Enter the following command

printf ("%c%c/n", 78,79);

Produces the following output:

N_o

  file

/usr/bin/printf contains printf commands.

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.