IEEE printf Specification

Source: Internet
Author: User

Apple's reference to direct reference to the http://pubs.opengroup.org/onlinepubs/009695399/functions/printf.html documentation to illustrate the use of formatting, this document is not Microsoft's msdn said clearly, look at the following msdn given the format description can explain everything.

% [Flags] [width] [. Perc] [f | n | H | L] Type

1. The meaning of type is as follows:

D signed 10-digit integer

I signed 10-digit integer

O signed octal integer

U unsigned 10-digit integer

X unsigned hexadecimal number, expressed in lowercase abcdef

X unsigned hexadecimal number, expressed in abcdef

F/F floating point number

E/E floating point in scientific format

G uses % F and % E to represent the smallest number of digits in the same g format as the floating point G, but expressed as an index.

C Single Character

S string

% Display percentage itself

P shows a pointer, and the near pointer is expressed as: xxxx

Far pointer: xxxx: yyyy

N connection parameters should be a pointer, where the number of written characters is stored

2. Flags specifies the output format. The values and meanings are as follows:

No right alignment. Fill in 0 and space on the left.

-Align left and fill in spaces on the right

+ Add the symbol + or-before the number-

A space is used to display symbols only for negative numbers.

# No effect on type = C, S, D, I, u

When type = O, X, X, add '0', "0x", "0x" before the value respectively"

Type = E, E, F, always use the decimal point

Type = G, G, the decimal point is always displayed except for the value 0

3. width is used to control the width of the displayed value. The values and meanings are as follows: n (n = 1, 2, 3 ...) the width must be at least N bits. If not, enter 0n (n =, 3...) with spaces ...) the width must be at least N bits. If not, the left side is filled with 0 x.

 

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

No display by default

0 when type = D, I, O, U, X, no effect

Type = E, E, F, do not display decimal point

N (n = 1, 2, 3...) the maximum number of decimal places when type = E, E, F

Type = others, indicating the maximum width displayed. * Format list

5. f | n | H | L indicates whether the pointer is a remote pointer or an integer is a long integer.

F remote pointer

N near pointer

H short integer or single-precision floating point number

L long integer or double-precision floating point number

Let's take a look at what the official website says. It is obviously not intuitive.

TheFormat Can contain either numbered argument conversion specifications (that is, "% N $" And "* M $" ), Or unnumbered argument conversion specifications (that is, % And * ), But not both. The only exception to this is that % Can be mixed with "% N $" Form. The results of mixing numbered and unnumbered argument specifications in Format String are undefined. When numbered argument specifications are used, specifying N Th argument requires that all the leading arguments, from the first to ( N-1 ) Th, are specified in the format string.

In format strings containing"%N$"Form of conversion specification, numbered arguments in the argument list can be referenced from the format string as required times as required.

In format strings containing%Form of conversion specification, each conversion specification uses the first unused argument in the argument list.

[CX] All Forms ofFprintf() Functions allow for the insertion of a language-dependent Radix character in the output string. The radix character is defined in the program's locale (CategoryLc_numeric ).In the POSIX locale, or in a locale where the radix character is not defined, the radix character shall default to a period ('.').

 

Each conversion specification is introduced by'%'Character [xsi] or by the Character Sequence"%N$", After which the following appear in sequence:

  • Zero or moreFlags(In any order), which modify the meaning of the conversion specification.

  • An optional minimumField width. If the converted value has fewer bytes than the field width, it shall be padded with spaces by default on the left; it shall be padded on the right if the left-adjustment flag ('-'), Described below, is given to the field width. The field width takes the form of an asterisk ('*'), Described below, or a decimal integer.

  • An optional Precision That gives the minimum number of digits to appear for D , I , O , U , X , And X Conversion specifiers; the number of digits to appear after the radix character for A ,A , E , E , F , And F Conversion specifiers; the maximum number of significant digits for G And G Conversion specifiers; or the maximum number of bytes to be printed from a string in S [Xsi] and S Conversion specifiers. The precision takes the form of a period ( '.' ) Followed either by an asterisk ( '*' ), Described below, or an optional decimal digit string, where a null digit string is treated as zero. If a precision appears with any other conversion specifier, the behavior is undefined.

  • An optional length modifier that specifies the size of the argument.

  • AConversion specifierCharacter that indicates the type of conversion to be applied.

A field width, or precision, or both, may be indicated by an asterisk ( '*' ). In this case an argument of Type Int Supplies the field width or precision. applications shall ensure that arguments specifying field width, or precision, or both appear in that order before the argument, if any, to be converted. A negative field width is taken as '-' Flag followed by a positive field width. A negative precision is taken as if the precision were omitted. [xsi] in format strings containing "% N $" Form of a conversion specification, a field width or precision may be indicated by the sequence "* M $" , Where M Is a decimal integer in the range [1, {nl_argmax}] Giving the position in the argument list (after Format Argument) of an integer argument containing the field width or precision, For example:

Nslog (@ "% 1 $ d05d: % 2 $. * 3 $ F: % 4 $. * 1 $ D", 2, 0.5555756, 6, 3 ); // 0000. 555576: 03

 

(@ "S % 02d", 3) // 03

(@ "S % 2D", 3) // _ 3

 

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.