About most of the parameters of Printf --------- How important is it to learn a foreign language! ! !

xiaoxiao2021-03-06  49

#include

/ ************************************************** ************************************************************ /// // author: flyingleaf //// role: type of printf simple test, mainly to help those sick mm View Printf parameter ///// Date: 2004-12-27 //// Test tool: vc7.0 /// *********************** ********************************************************* /

INT _TMAIN (int Argc, _tchar * argv []) {float d = 3.14; printf ("d =%. 10e / n", d); // The 6 digits output here are spam, not accurate

CHAR CH [20]; STRCPY (CH, "123456780123"); int N = 2, m = 10; ///*.*, the front * defined is the total width, the definition of the rear is the number of outputs . // If the back is small than the front, use the space to make up the m bit on the left. Printf ("% *. * S / N", m, n, ch); // output "12" Printf ("% *. * S / N", N, M, CH); // output here " 1234567890 "Add, if Strlen (CH)

INT Y = 456; // The # 8d here, keeping the width, if not 8 bits, just tap // if there is more than 8 digits, there are several digits. Printf ("% # 8D / N% # 8X / N% # 8O / N", Y, Y, Y); Printf ("% # 3d / n% # 3x / n% # 3o / n", Y, Y , y); Printf ("% # 1d / n% # 1x / n% # 1O / n", y, y, y);

// Here. 8D is not stranger? It is not 8 digits when you use 0 to make up //, if more than 8 digits have several printf ("%. 8D / n%) .8x / n% .8o / n ", y, y, y);

// The * D is estimated to be a bit unfamiliar, but it can be seen as a # 6d, the effect is the same. Printf ("% * D / n", 6, y);

// The number in the 6D here is meaningful: First, there is a number in front of the output number, two, not enough 6-digit PRINTF ("% 6d / n", y); // The meaning of the% 6d above is basically the same, but if the number of Y is 1 is not 6 large, it is used in 0, but the number is not more than //6. The specific effect can be running to see PrINTF ("% 006d / n", y); Printf ("% 0006D / N", Y); // Supplement, if the number of bits 6 is small, only output NS and Y itself // If there is no number, it is use 0 to add enough 6 digits, for example: Printf ("% 06d / n", y); // output "000456" // Here-number is right side In order to clearly, we still use HH as the end. Printf ("% - 6dhh / n", y); // Of course, if the 2 is not the number of bits here, then output Y, then output hh // here - the number is just the right side of the space. Meaning Printf ("% - 2DHH / ​​N", Y);

// An example of using Printf is int LEN = 0; // Here% of the meaning of the string of the string before the% N is used to the Len: // below is 8 = Strlen ("HH") Strlen ("123456"); Printf ("HH% S% N / N", "123456", & LEN); Printf ("LEN =% D / N", LEN);

// I didn't intend to write him but bring it. If there is no decimal, no point #g is not less! Printf ("%. 0FHH / N% #. 0FHH / N% GHH / N% # GHH / N", 3.0, 3.0, 3.0, 3.0);

// I don't understand. The "C Trap and Defect" said that the output 7 empty shell and then output% is output. How did I try to one%. // Who is trying to tell me: csflyingleaf@163.com Thank you for Printf ("% *% / n", 8);

// Ok, I feel a lot, if you are not enough, give me a text message. Return 0;}

All output:

d = 3.1400001049e 000 121234567801 12hh1234678hh 456 0x1c8 07104560x1c807104560x1c8071000000456000001c800000710 456 456 00456 00456000456456 hh456hhhh123456len = 83hh3.hh3hh3.00000hh% Press any key to continue;

Posted on 2004-12-27 23:50 Dance reading (532)

Comments (6)

edit

Collect

href = "http://blog.vckbase.com/flyingleaf/services/pingback.aspx" Rel = "pingback" />

comment

#

Re: About most of the parameters of Printf

2004-12-28 02:40

SEC

Output Conversion Syntax

This section provides details about the precise syntax of conversion specifications that can appear in a printf template string.Characters in the template string that are not part of a conversion specification are printed as-is to the output stream. Multibyte character sequences (see Character Set HANDLING) Are Permitted in a template string.

The Conversion Specifications in a Printf Template String Have The General Form:

% [param-no $] flags width [. Precision] TYPE Conversion

For example, in the conversion specifier% -10.8ld, the - is a flag, 10 specifies the field width, the precision is 8, the letter l is a type modifier, and d specifies the conversion style (This particular type specifier says. To Print A Long Int Argument in Decimal Notation, with a minimum of 8 Digits Left-Justified in a field at Least 10 Characters Wide.)

In More Detail, Output Conversion Specifications Consist of An Initial% Character Followed in Sequence By:

? An optional specification of the parameter used for this format. Normally the parameters to the printf function are assigned to the formats in the order of appearance in the format string. But in some situations (such as message translation) this is not desirable and this Extension allows an expecit parameter to be specified.

The param-no part of the format must be an integer in the range of 1 to the maximum number of arguments present to the function call. Some implementations limit this number to a certainly upper bound. The exact limit can be retrieved by the following constant .

Macro: nl_argmax

The value of ARGMAX is the maximum value allowed for the specification of an positional parameter in a printf call. The actual value in effect at runtime can be retrieved by using sysconf using the _SC_NL_ARGMAX parameter see Sysconf Definition.Some system have a quite low limit such AS 9 for System V Systems. The GNU C Library Has No Real Limit.

If any of the formats HAS a specification for the parameter position all of the. Otherwise the behavior is undefined.

? Zero or more lag characters? That Modify The Normal Behavior of The Conversion Specification.

?? An optional decimal integer specifying the inimum field width If the normal conversion produces fewer characters than this, the field is padded with spaces to the specified width This is a minimum value;. If the normal conversion produces more characters than this, the field is not truncated. Normally, the output is right-justified within the field. You can also specify a field width of *. This means that the next argument in the argument list (before the actual value to be printed) is used as the Field width. The value must be an int........................

? An optional recision? To specify the number of digits to be written for the numeric conversions. If the precision is specified, it consists of a period (.) Followed optionally by a decimal integer (which defaults to zero if omitted). You can also specify a precision of *. This means that the next argument in the argument list (before the actual value to be printed) is used as the precision. The value must be an int, and is ignored if it is negative. If you specify * for both the field width and precision, the field width argument precedes the precision argument. Other C library versions may not recognize this syntax.?An optional ype modifier character? which is used to specify the data type of the corresponding argument if IT Differs from the default type. (for Example, The Integer Conversions Assumeth) Assumeth, but you can, ing, or l for other integer type.)

? A Character That Specifier The Conversion To Be Applied.

The exact options that are permitted and how they are interpreted vary between the different conversion specifiers. See the descriptions of the individual conversions for information about the particular options that they use.

With the -Wformat option, the GNU C compiler checks calls to printf and related functions. It examines the format string and verifies that the correct number and types of arguments are supplied. There is also a GNU C syntax to tell the compiler that a function You Write Uses a Printf-Style Format String. See Declaring Attributes of functions, for more information.

#

Re: About most of the parameters of Printf

2004-12-28 02:40

In one way

Table of Output Conversions

Here Is a Table Summarizing What All The Different Conversions Do:

% D,% I

Print An Integer As a Signed Decimal Number. See Integer Conversions, for Details.% D and% I Are Synynymous for Output, But Are Different When Used with Scanf for Input (See Table of Input Conversions).% O

Print An INTEGER AS AN UNSIGNED Octal Number. See Integer Conversions, for Details.

% U

Print An INTEGER AS AN UNSIGNED DECIMAL NUMBER. See Integer Conversions, for Details.

% x,% x

Print An Integer AS An Unsigned Hexadecimal Number.% X Usess Lower-Case Letters and% x Uses Upper-case. See Integer Conversions, for Details.

% F

Print A floating-point number in normal (fixed-point) Notation. See floating-point conversions, for details.

% E,% E

Print A floating-point Number in Exponential NOTATION.% E Usess limited. See Floating-Point Conversions, for Details.

% g,% g

Print A Floating-Point Number in Either Normal or Exponential NotTiTs Magnitude.% G Usess Lower-case letters and% g Uses Upper-case. See floating-point conversions, for details.

% a,% a

Print A Floating-Point Number in a Hexadecimal Fractional Notation Which The Exponent To Base 2 Represented in Decimal Digits.% A Usess Lower-Case Letters and

% A Uses Upper-case. See floating-point conversions, for details.

% C

Print A Single Character. See Other Output Conversions.

% C

....................

% s

Print A String. See Other Output Conversions.

% S

This is an alias for% ls Which is supported for Compatibility with the UNIX Standard.

% P

Print The Value of a Pointer. See Other Output Conversions.

% N

Get the Number of Characters Printed So Far. See other output conversions. Note That Conversion Specification NEVER Produces Any Output.% M

.,...........................

%%

Print A Literal% Character. See Other Output Conversions.

If the syntax of a conversion specification is invalid, unpredictable things will happen, so do not do this. If there are not enough function arguments provided to supply values ​​for all the conversion specifications in the template string, or if the arguments are not Of The Correct Types, The Results Arene Arguments Than Conversion Specifications, The Extra Argument Values ​​Arealog.

#

Re: About most of the parameters of Printf

2004-12-28 02:40

Integer

INTEGER Conversions

This Section Describes The Options for the% D,% I,% O,% U,% X, AND% X Conversion Specifications. These Conversions Print Integers in Various Formats.

The% d and% i conversion specifications both print an int argument as a signed decimal number; while% o,% u, and% x print the argument as an unsigned octal, decimal, or hexadecimal number (respectively) The% X conversion. Specification is Just Like% x Except this ing buys the characters of abcdef as digits instead of abcdef.

The Following Flags Are Meaningful:

-

Left-Justify The Result In The Field (Instead of the Normal Right-Justification).

For the Signed% D and% I Conversions, Print A Plus Signiff.

For the signed% d and% i conversions, if the result does not start with a plus or minus sign, prefix it with a space character instead. Since the flag ensures that the result includes a sign, this flag is ignored if you Supply Both of them. #

For THE% O Conversion, this Forces the Leading Digit To BE 0, As IF by Increasing The Precision. For% X OR% X, this prefixes a leading 0x or 0x (respective) to the result. This doesn't do anything useful For the% d,% i, or% u conversions. Using this flag produnce Output Which can be pased by the start (see Parsing of integers) and scanf with the% i conversion (see Numeric Input Conversions).

'

Separate The Digits Into Groups As Specified by The Locale Specified for the LC_NUMERIC CATEGORY; See General Numeric. This flag is a gnu extension.

0

.

If a precision is supplied, it specifies the minimum number of digits to appear; leading zeros are produced if necessary If you do not specify a precision, the number is printed with as many digits as it needs If you convert a value of.. Zero with an expected precision of zero, The No Characters at all areproduces.

WITHOUT A TYPE MODIFIER, THE CORRESPONDING ARGUMENT IS TREATED AS An Int (for The Signed Conversions% I and% D) Or Unsigned Int (for the unsigned conversions% O,

% U,% x, and% X) Recall that since printf and friends are variadic, any char and short arguments are automatically converted to int by the default argument promotions For arguments of other integer types, you can use these modifiers..:

HH

Specifies that the argument is a signed char or unsigned char, as appropriate. A char argument is converted to an int or unsigned int by the default argument promotions anyway, but the h modifier says to convert it back to a char again.This modifier was Introducesd in ISO C99.

hide

Specifies that the argument is a short int or unsigned short int, as appropriate. A short argument is converted to an int or unsigned int by the default argument promotions anyway, but the h modifier says to convert it back to a short again.

j

Specifies That The Argument is a INTMAX_T OR UINTMAX_T, AS Appropriate.

This Modifier WAS Introducesd in ISO C99.

l

Specifies That The Argument is a long int or unsigned long int, as appropriate. Two L Characters Is Like The L Modifier, Below.

If Used with% C OR% s The Corresponding Parameter Is Considered As A Wide Character or Wide Character String Respectively. This Use of L Was Introducesd In Amendment 1 To ISO C90.

L

LL

Qi

Specifies That The Argument IS A long long int. On Systems That Don't support extra-long integers, this is the same as long int.

The q modifier is another name for the Same Thing, Which Coms from 4.4 BSD; a long long int is some amountimes caled a "quad" int.

t

Specifies That the argument is a ptrdiff_t.

This Modifier WAS Introducesd in ISO C99.

z

Z

Specifies That the argument is a size_t.

Z Was Introducesd in ISO C99. Z is a gnu extension precating this addition and sales.

Here is an example. Using the template string:

"|% 5D |% -5D |% 5D |% - 5D |% 5D |% 05D |% 5.0D |% 5.2D |% D | / N"

To Print Numbers Using The Different Options for the% D Conversion Gives Results Like:

| 0 | 0 | 0 | 0 | 0 | 00000 | | 00 | 0 || 1 | 1 | 1 | 1 | 1 | 00001 | 1 | 01 | 1 |

| -1 | -1 | -1 | -1 | -1 | -0001 | -1 | -01 | -1 |

| 100000 | 100000 | 100000 | 100000 | 100000 | 100000 | 100000 | 100000 | 100000 |

In particular, NOTICE What HAPPENS in The Last Case Where The Number is Too Large to Fit in The Minimum Field Width Specified.

Here Are Some More Examples Showing How Unsigned Integers Print Under Various Format Options, Using The Template String:

"|% 5U |% 5o |% 5X |% 5X |% # 5o |% # 5X |% # 5X |% # 10.8x | / n"

| 0 | 0 | 0 | 0 | 0 | 0 | 0 聽 00000000 |

| 1 | 1 | 1 | 1 | 01 | 0x1 | 0x1 | 0x00000001 |

| 100000 | 303240 | 186A0 | 186A0 | 0303240 | 0x186A0 | 0x186A0 | 0x000186A0 |

#

Re: About most of the parameters of Printf

2004-12-28 02:41

Floating point number

Floating-points conversions

THIS Section Discusses The Conversion Specifications for floating-point number: the% f,% e,% e,% g, and% g conversions.

THE% f conversion print notation, producing output of the form [-] ddd.ddd, where the number of digits following the decimal point is control.

The% e conversion prints its argument in exponential notation, producing output of the form [-] d.ddde [ | -]. Dd Again, the number of digits following the decimal point is controlled by the precision The exponent always contains at. Least Two Digits. The% E Conversion IS Similar But The Exponent is Marked with The Letter E INSTEAD OF E.

The% g and% g conve% e or% e (respective) if the exponent would be less Than -4 or greater trecision; Otherwise the used the% f style. A Precision of 0, is taken as 1. is Trailing zeros are removed from the fractional portion of the result and a decimal-point character appears only if it is followed by a digit.The% a and% A conversions are meant for representing floating-point numbers exactly in textual form so that they can be exchanged as texts between different programs and / or machines The numbers are represented is the form [-] 0xh.hhhp [ | -].. dd At the left of the decimal-point character exactly one digit is print This character is only 0 if the number is denormalized Otherwise the value is unspecified;.. it is implementation dependent how many bits are used The number of hexadecimal digits on the right side of the decimal-point character is equal. To the precision. if the precision is zero it is determined to be la lad enough to provide an exact representation of the number (or it is large enough to distinguish two adjacent values ​​if the FLT_RADIX is not a power of 2, see Floating Point Parameters). For the% a conversion lower-case characters are used to represent the hexadecimal number and the prefix and exponent sign are printed as 0x and p respectively. Otherwise upper-case characters are used and 0X and P are used for the representation of prefix and exponent string. The exponent to the base of two is printed as a decimal Number Using At Least One Digit Butt Most As Many Digits As Necessary To Represent The Value Exactly.

If the value to be printed represents infinity or a nan, the output is [-] inf or% g and it is [-] inf or nan respectivenessiff The Conversion IS% a,% E, OR% G.The Following Flags Can Be Used To Modify The Behavior:

-

LEFT-JUSTIFY THE RESULT IN THE FIELD. NORMALLY THE RES RIGHT-JUSTIFIED.

Always include a plus or minus sign in the result.

If The Result Doesn't Start With a Plus or Minus Sign, Prefix It With a Space Instead. Since The Flag Ensures That The Result Includes a Sign, this flag is ignored if you supply @ @

#

Specifies that the result should always include a decimal point, even if no digits follow it. For the% g and% G conversions, this also forces trailing zeros after the decimal point to be left in place where they would otherwise be removed.

'

Separate The result integer part of the result ints of the interest............................

0

.

The precision specifies how many digits follow the decimal-point character for the% f,% e, and% E conversions. For these conversions, the default precision is 6. If the precision is explicitly 0, this suppresses the decimal point character entirely. for the% g and% G conversions, the precision specifies how many significant digits to print. Significant digits are the first digit before the decimal point, and all the digits after it. If the precision is 0 or not specified for% g or% G, it is treated like a value of 1. If the value being printed can not be expressed accurately in the specified number of digits, the value is rounded to the nearest number that fits.Without a type modifier, the floating-point conversions use an Argument of Type Double. (by the default argument ", any float arguments area automatically converted to double.) The Following Type Modifier is supported:

L

An Uppercase L Specifies That The Argument Is A Long Double.

Here Are Some Examples Showing How Numbers Print Using The Various Floating-Point Conversions. All of the number string:

"|% 13.4A |% 13.4F |% 13.4e |% 13.4g | / N"

Here is the output:

| 0x0.0000p 0 | 0.0000 | 0.0000E 00 | 0 |

| 0x1.0000p-1 | 0.5000 | 5.0000E-01 | 0.5 |

| 0x1.0000p 0 | 1.0000 | 1.0000E 00 | 1 |

| -0x1.0000p 0 | -1.0000 | -1.0000E 00 | -1 |

| 0x1.9000p 6 | 100.0000 | 1.0000E 02 | 100 |

| 0x1.f400p 9 | 1000.0000 | 1.0000E 03 | 1000 |

| 0x1.3880p 13 | 10000.0000 | 1.0000E 04 | 1e 04 |

| 0x1.81c8p 13 | 12345.0000 | 1.2345E 04 | 1.234E 04 |

| 0x1.86A0p 16 | 100000.0000 | 1.0000E 05 | 1e 05 |

| 0x1.e240p 16 | 123456.0000 | 1.2346e 05 | 1.235e 05 |

Notice how the% g converness drops trailing zeros. #

Re: About most of the parameters of Printf

2004-12-28 02:41

other

Other Output Conversions

This Section Describes Miscellaneous Conversions for Printf.

. The% c conversion prints a single character In case there is no l modifier the int argument is first converted to an unsigned char Then, if used in a wide stream function, the character is converted into the corresponding wide character The -.. Flag Can Be Used to Specify Left-Justification In The Field, But no Precision or Type Modifier CAN Be Given. For example:

Printf ("% C% C% C% C% C", 'h', 'e', ​​'L', 'L', 'o');

PRINTS HELLO.

If there is a l modifier present the argument is expected to be of type wint_t. If used in a multibyte function the wide character is converted into a multibyte character before being added to the output. In this case more than one output byte can be produced.

The% s conversion prints a string. If no l modifier is present the corresponding argument must be of type char * (or const char *). If used in a wide stream function the string is first converted in a wide character string. A precision can be specified to indicate the maximum number of characters to write; otherwise characters in the string up to but not including the terminating null character are written to the output stream The - flag can be used to specify left-justification in the field, but. No Other Flags Or Type Modifiers Are Defined for this Conversion. for example:

Printf ("% 3S%-6s", "no", "where");

PRINTS NOWHERE.

IF the is a l modifier present the argument is expected to be bit wchar_t (or const wchar_t *).

If you accidentally pass a null pointer as the argument for a% s conversion, the GNU library prints it as (null). We think this is more useful than crashing. But it's not good practice to pass a null argument intentionally.The% m Conversion Prints The String Corresponding to The Error Code in errno. See Error Messages. Thus:

FPrintf (stderr, "can't open`% s':% m / n ", filename);

IS Equivalent TO:

FPrintf (stderr, "can't open`% s':% s / n ", filename, strerror (errno));

THE% M Conversion is a gnu c library extension.

The% P Conversion Prints a Pointer Value. The Corresponding Argument Must Be of Type Void *. In Practice, You CAN Use any type of pointer.

In The GNU System, Non-Null Pointers Are Printed As UNSIGNED INTEGERS, AS IF a% # x conversion were used. Null Pointers Print As (NIL).

For example:

Printf ("% p", "testing");

PRINTS 0X FOLLOWED BY A Hexadecimal Number - The Address of The String Constant "Testing". It Does Not Print The Word Testing.

You Can Supply The - Flag with the% P Conversion To Specify Left-Justification, But no Other Flags, Precision, or Type Modifiers Are Defined.

The% n conversion is unlike any of the other output conversions. It uses an argument which must be a pointer to an int, but instead of printing anything it stores the number of characters printed so far by this call at that location. The h and l Type Modifiers Are Permitted to Specify That The Argument IS OF TYPE SHORT INT * Or Long Int * instead of int *, but no flags, field width, or precision is permitted.

For example,

Int nchar;

Printf ("% D% s% N / N", 3, "bears", & nchar);

PRINTS:

3 bears

And sets nchar to 7, because 3 bears is seven characters.

The %% Conversion Prints a literal% Character. This Conversion Doesn't Use An Argument, And no Flags, Field Width, Precision, or Type Modifiers Are Permitted.

#

Re: About most of the parameters of Printf

2004-12-28 02:40

SEC

Output Conversion Syntax

This Section Provides Details About The Precise Syntax of Conversion Specifications That Can Appear In a Printf Template String.

Characters in the template string that are not part of a conversion specification are printed as-is to the output stream. Multibyte character sequences (see Character Set Handling) are permitted in a template string.

The Conversion Specifications in a Printf Template String Have The General Form:

% [param-no $] flags width [. Precision] TYPE Conversion

For example, in the conversion specifier% -10.8ld, the - is a flag, 10 specifies the field width, the precision is 8, the letter l is a type modifier, and d specifies the conversion style (This particular type specifier says. To Print A Long Int Argument in Decimal Notation, with a minimum of 8 Digits Left-Justified in a field at Least 10 Characters Wide.)

In More Detail, Output Conversion Specifications Consist of An Initial% Character Followed in Sequence By:

? An optional specification of the parameter used for this format. Normally the parameters to the printf function are assigned to the formats in the order of appearance in the format string. But in some situations (such as message translation) this is not desirable and this Extension allows an expecit parameter to be specified.

The param-no part of the format must be an integer in the range of 1 to the maximum number of arguments present to the function call. Some implementations limit this number to a certainly upper bound. The exact limit can be retrieved by the following constant .Macro: NL_ARGMAX

The value of ARGMAX is the maximum value allowed for the specification of an positional parameter in a printf call. The actual value in effect at runtime can be retrieved by using sysconf using the _SC_NL_ARGMAX parameter see Sysconf Definition.

Some System Have a Quite Low Limit Such As 9 for System V Systems. The GNU C Library Has No Real Limit.

If any of the formats HAS a specification for the parameter position all of the. Otherwise the behavior is undefined.

? Zero or more lag characters? That Modify The Normal Behavior of The Conversion Specification.

?? An optional decimal integer specifying the inimum field width If the normal conversion produces fewer characters than this, the field is padded with spaces to the specified width This is a minimum value;. If the normal conversion produces more characters than this, the field is not truncated. Normally, the output is right-justified within the field. You can also specify a field width of *. This means that the next argument in the argument list (before the actual value to be printed) is used as the Field width. The value must be an int........................

? An optional recision? To specify the number of digits to be written for the numeric conversions. If the precision is specified, it consists of a period (.) Followed optionally by a decimal integer (which defaults to zero if omitted). You can also specify a precision of *. This means that the next argument in the argument list (before the actual value to be printed) is used as the precision. The value must be an int, and is ignored if it is negative. If you specify * for both the field width and precision, the field width argument precedes the precision argument. Other C library versions may not recognize this syntax.?An optional ype modifier character? which is used to specify the data type of the corresponding argument if IT Differs from the default type. (for Example, The Integer Conversions Assumeth) Assumeth, but you can, ing, or l for other integer type.)

? A Character That Specifier The Conversion To Be Applied.

The exact options that are permitted and how they are interpreted vary between the different conversion specifiers. See the descriptions of the individual conversions for information about the particular options that they use.

With the -Wformat option, the GNU C compiler checks calls to printf and related functions. It examines the format string and verifies that the correct number and types of arguments are supplied. There is also a GNU C syntax to tell the compiler that a function You Write Uses a Printf-Style Format String. See Declaring Attributes of functions, for more information.

#

Re: About most of the parameters of Printf

2004-12-28 02:40

In one way

Table of Output Conversions

Here Is a Table Summarizing What All The Different Conversions Do:

% D,% I

Print An Integer As a Signed Decimal Number. See Integer Conversions, for Details.% D and% I Are Synynymous for Output, But Are Different When Used with Scanf for Input (See Table of Input Conversions).% O

Print An INTEGER AS AN UNSIGNED Octal Number. See Integer Conversions, for Details.

% U

Print An INTEGER AS AN UNSIGNED DECIMAL NUMBER. See Integer Conversions, for Details.

% x,% x

Print An Integer AS An Unsigned Hexadecimal Number.% X Usess Lower-Case Letters and% x Uses Upper-case. See Integer Conversions, for Details.

% F

Print A floating-point number in normal (fixed-point) Notation. See floating-point conversions, for details.

% E,% E

Print A floating-point Number in Exponential NOTATION.% E Usess limited. See Floating-Point Conversions, for Details.

% g,% g

Print A Floating-Point Number in Either Normal or Exponential NotTiTs Magnitude.% G Usess Lower-case letters and% g Uses Upper-case. See floating-point conversions, for details.

% a,% a

Print A Floating-Point Number in a Hexadecimal Fractional Notation Which The Exponent To Base 2 Represented in Decimal Digits.% A Usess Lower-Case Letters and

% A Uses Upper-case. See floating-point conversions, for details.

% C

Print A Single Character. See Other Output Conversions.

% C

....................

% s

Print A String. See Other Output Conversions.

% S

This is an alias for% ls Which is supported for Compatibility with the UNIX Standard.

% P

Print The Value of a Pointer. See Other Output Conversions.

% N

Get the Number of Characters Printed So Far. See other output conversions. Note That Conversion Specification NEVER Produces Any Output.% M

.,...........................

%%

Print A Literal% Character. See Other Output Conversions.

If the syntax of a conversion specification is invalid, unpredictable things will happen, so do not do this. If there are not enough function arguments provided to supply values ​​for all the conversion specifications in the template string, or if the arguments are not Of The Correct Types, The Results Arene Arguments Than Conversion Specifications, The Extra Argument Values ​​Arealog.

#

Re: About most of the parameters of Printf

2004-12-28 02:40

Integer

INTEGER Conversions

This Section Describes The Options for the% D,% I,% O,% U,% X, AND% X Conversion Specifications. These Conversions Print Integers in Various Formats.

The% d and% i conversion specifications both print an int argument as a signed decimal number; while% o,% u, and% x print the argument as an unsigned octal, decimal, or hexadecimal number (respectively) The% X conversion. Specification is Just Like% x Except this ing buys the characters of abcdef as digits instead of abcdef.

The Following Flags Are Meaningful:

-

Left-Justify The Result In The Field (Instead of the Normal Right-Justification).

For the Signed% D and% I Conversions, Print A Plus Signiff.

For the signed% d and% i conversions, if the result does not start with a plus or minus sign, prefix it with a space character instead. Since the flag ensures that the result includes a sign, this flag is ignored if you Supply Both of them. #

For THE% O Conversion, this Forces the Leading Digit To BE 0, As IF by Increasing The Precision. For% X OR% X, this prefixes a leading 0x or 0x (respective) to the result. This doesn't do anything useful For the% d,% i, or% u conversions. Using this flag produnce Output Which can be pased by the start (see Parsing of integers) and scanf with the% i conversion (see Numeric Input Conversions).

'

Separate The Digits Into Groups As Specified by The Locale Specified for the LC_NUMERIC CATEGORY; See General Numeric. This flag is a gnu extension.

0

.

If a precision is supplied, it specifies the minimum number of digits to appear; leading zeros are produced if necessary If you do not specify a precision, the number is printed with as many digits as it needs If you convert a value of.. Zero with an expected precision of zero, The No Characters at all areproduces.

WITHOUT A TYPE MODIFIER, THE CORRESPONDING ARGUMENT IS TREATED AS An Int (for The Signed Conversions% I and% D) Or Unsigned Int (for the unsigned conversions% O,

% U,% x, and% X) Recall that since printf and friends are variadic, any char and short arguments are automatically converted to int by the default argument promotions For arguments of other integer types, you can use these modifiers..:

HH

Specifies that the argument is a signed char or unsigned char, as appropriate. A char argument is converted to an int or unsigned int by the default argument promotions anyway, but the h modifier says to convert it back to a char again.This modifier was Introducesd in ISO C99.

hide

Specifies that the argument is a short int or unsigned short int, as appropriate. A short argument is converted to an int or unsigned int by the default argument promotions anyway, but the h modifier says to convert it back to a short again.

j

Specifies That The Argument is a INTMAX_T OR UINTMAX_T, AS Appropriate.

This Modifier WAS Introducesd in ISO C99.

l

Specifies That The Argument is a long int or unsigned long int, as appropriate. Two L Characters Is Like The L Modifier, Below.

If Used with% C OR% s The Corresponding Parameter Is Considered As A Wide Character or Wide Character String Respectively. This Use of L Was Introducesd In Amendment 1 To ISO C90.

L

LL

Qi

Specifies That The Argument IS A long long int. On Systems That Don't support extra-long integers, this is the same as long int.

The q modifier is another name for the Same Thing, Which Coms from 4.4 BSD; a long long int is some amountimes caled a "quad" int.

t

Specifies That the argument is a ptrdiff_t.

This Modifier WAS Introducesd in ISO C99.

z

Z

Specifies That the argument is a size_t.

Z Was Introducesd in ISO C99. Z is a gnu extension precating this addition and sales.

Here is an example. Using the template string:

"|% 5D |% -5D |% 5D |% - 5D |% 5D |% 05D |% 5.0D |% 5.2D |% D | / N"

To Print Numbers Using The Different Options for the% D Conversion Gives Results Like:

| 0 | 0 | 0 | 0 | 0 | 00000 | | 00 | 0 || 1 | 1 | 1 | 1 | 1 | 00001 | 1 | 01 | 1 |

| -1 | -1 | -1 | -1 | -1 | -0001 | -1 | -01 | -1 |

| 100000 | 100000 | 100000 | 100000 | 100000 | 100000 | 100000 | 100000 | 100000 |

In particular, NOTICE What HAPPENS in The Last Case Where The Number is Too Large to Fit in The Minimum Field Width Specified.

Here Are Some More Examples Showing How Unsigned Integers Print Under Various Format Options, Using The Template String:

"|% 5U |% 5o |% 5X |% 5X |% # 5o |% # 5X |% # 5X |% # 10.8x | / n"

| 0 | 0 | 0 | 0 | 0 | 0 | 0 聽 00000000 |

| 1 | 1 | 1 | 1 | 01 | 0x1 | 0x1 | 0x00000001 |

| 100000 | 303240 | 186A0 | 186A0 | 0303240 | 0x186A0 | 0x186A0 | 0x000186A0 |

#

Re: About most of the parameters of Printf

2004-12-28 02:41

Floating point number

Floating-points conversions

THIS Section Discusses The Conversion Specifications for floating-point number: the% f,% e,% e,% g, and% g conversions.

THE% f conversion print notation, producing output of the form [-] ddd.ddd, where the number of digits following the decimal point is control.

The% e conversion prints its argument in exponential notation, producing output of the form [-] d.ddde [ | -]. Dd Again, the number of digits following the decimal point is controlled by the precision The exponent always contains at. Least Two Digits. The% E Conversion IS Similar But The Exponent is Marked with The Letter E INSTEAD OF E.

The% g and% g conve% e or% e (respective) if the exponent would be less Than -4 or greater trecision; Otherwise the used the% f style. A Precision of 0, is taken as 1. is Trailing zeros are removed from the fractional portion of the result and a decimal-point character appears only if it is followed by a digit.The% a and% A conversions are meant for representing floating-point numbers exactly in textual form so that they can be exchanged as texts between different programs and / or machines The numbers are represented is the form [-] 0xh.hhhp [ | -].. dd At the left of the decimal-point character exactly one digit is print This character is only 0 if the number is denormalized Otherwise the value is unspecified;.. it is implementation dependent how many bits are used The number of hexadecimal digits on the right side of the decimal-point character is equal. To the precision. if the precision is zero it is determined to be la lad enough to provide an exact representation of the number (or it is large enough to distinguish two adjacent values ​​if the FLT_RADIX is not a power of 2, see Floating Point Parameters). For the% a conversion lower-case characters are used to represent the hexadecimal number and the prefix and exponent sign are printed as 0x and p respectively. Otherwise upper-case characters are used and 0X and P are used for the representation of prefix and exponent string. The exponent to the base of two is printed as a decimal Number Using At Least One Digit Butt Most As Many Digits As Necessary To Represent The Value Exactly.

If the value to be printed represents infinity or a nan, the output is [-] inf or% g and it is [-] inf or nan respectivenessiff The Conversion IS% a,% E, OR% G.The Following Flags Can Be Used To Modify The Behavior:

-

LEFT-JUSTIFY THE RESULT IN THE FIELD. NORMALLY THE RES RIGHT-JUSTIFIED.

Always include a plus or minus sign in the result.

If The Result Doesn't Start With a Plus or Minus Sign, Prefix It With a Space Instead. Since The Flag Ensures That The Result Includes a Sign, this flag is ignored if you supply @ @

#

Specifies that the result should always include a decimal point, even if no digits follow it. For the% g and% G conversions, this also forces trailing zeros after the decimal point to be left in place where they would otherwise be removed.

'

Separate The result integer part of the result ints of the interest............................

0

.

The precision specifies how many digits follow the decimal-point character for the% f,% e, and% E conversions. For these conversions, the default precision is 6. If the precision is explicitly 0, this suppresses the decimal point character entirely. for the% g and% G conversions, the precision specifies how many significant digits to print. Significant digits are the first digit before the decimal point, and all the digits after it. If the precision is 0 or not specified for% g or% G, it is treated like a value of 1. If the value being printed can not be expressed accurately in the specified number of digits, the value is rounded to the nearest number that fits.Without a type modifier, the floating-point conversions use an Argument of Type Double. (by the default argument ", any float arguments area automatically converted to double.) The Following Type Modifier is supported:

L

An Uppercase L Specifies That The Argument Is A Long Double.

Here Are Some Examples Showing How Numbers Print Using The Various Floating-Point Conversions. All of the number string:

"|% 13.4A |% 13.4F |% 13.4e |% 13.4g | / N"

Here is the output:

| 0x0.0000p 0 | 0.0000 | 0.0000E 00 | 0 |

| 0x1.0000p-1 | 0.5000 | 5.0000E-01 | 0.5 |

| 0x1.0000p 0 | 1.0000 | 1.0000E 00 | 1 |

| -0x1.0000p 0 | -1.0000 | -1.0000E 00 | -1 |

| 0x1.9000p 6 | 100.0000 | 1.0000E 02 | 100 |

| 0x1.f400p 9 | 1000.0000 | 1.0000E 03 | 1000 |

| 0x1.3880p 13 | 10000.0000 | 1.0000E 04 | 1e 04 |

| 0x1.81c8p 13 | 12345.0000 | 1.2345E 04 | 1.234E 04 |

| 0x1.86A0p 16 | 100000.0000 | 1.0000E 05 | 1e 05 |

| 0x1.e240p 16 | 123456.0000 | 1.2346e 05 | 1.235e 05 |

Notice how the% g converness drops trailing zeros. #

Re: About most of the parameters of Printf

2004-12-28 02:41

other

Other Output Conversions

This Section Describes Miscellaneous Conversions for Printf.

. The% c conversion prints a single character In case there is no l modifier the int argument is first converted to an unsigned char Then, if used in a wide stream function, the character is converted into the corresponding wide character The -.. Flag Can Be Used to Specify Left-Justification In The Field, But no Precision or Type Modifier CAN Be Given. For example:

Printf ("% C% C% C% C% C", 'h', 'e', ​​'L', 'L', 'o');

PRINTS HELLO.

If there is a l modifier present the argument is expected to be of type wint_t. If used in a multibyte function the wide character is converted into a multibyte character before being added to the output. In this case more than one output byte can be produced.

The% s conversion prints a string. If no l modifier is present the corresponding argument must be of type char * (or const char *). If used in a wide stream function the string is first converted in a wide character string. A precision can be specified to indicate the maximum number of characters to write; otherwise characters in the string up to but not including the terminating null character are written to the output stream The - flag can be used to specify left-justification in the field, but. No Other Flags Or Type Modifiers Are Defined for this Conversion. for example:

Printf ("% 3S%-6s", "no", "where");

PRINTS NOWHERE.

IF the is a l modifier present the argument is expected to be bit wchar_t (or const wchar_t *).

If you accidentally pass a null pointer as the argument for a% s conversion, the GNU library prints it as (null). We think this is more useful than crashing. But it's not good practice to pass a null argument intentionally.The% m Conversion Prints The String Corresponding to The Error Code in errno. See Error Messages. Thus:

FPrintf (stderr, "can't open`% s':% m / n ", filename);

IS Equivalent TO:

FPrintf (stderr, "can't open`% s':% s / n ", filename, strerror (errno));

THE% M Conversion is a gnu c library extension.

The% P Conversion Prints a Pointer Value. The Corresponding Argument Must Be of Type Void *. In Practice, You CAN Use any type of pointer.

In The GNU System, Non-Null Pointers Are Printed As UNSIGNED INTEGERS, AS IF a% # x conversion were used. Null Pointers Print As (NIL).

For example:

Printf ("% p", "testing");

PRINTS 0X FOLLOWED BY A Hexadecimal Number - The Address of The String Constant "Testing". It Does Not Print The Word Testing.

You Can Supply The - Flag with the% P Conversion To Specify Left-Justification, But no Other Flags, Precision, or Type Modifiers Are Defined.

The% n conversion is unlike any of the other output conversions. It uses an argument which must be a pointer to an int, but instead of printing anything it stores the number of characters printed so far by this call at that location. The h and l Type Modifiers Are Permitted to Specify That The Argument IS OF TYPE SHORT INT * Or Long Int * instead of int *, but no flags, field width, or precision is permitted.

For example,

Int nchar;

Printf ("% D% s% N / N", 3, "bears", & nchar);

PRINTS:

3 bears

And sets nchar to 7, because 3 bears is seven characters.

The %% Conversion Prints a literal% Character. This Conversion Doesn't Use An Argument, And no Flags, Field Width, Precision, or Type Modifiers Are Permitted.

转载请注明原文地址:https://www.9cbs.com/read-40692.html

New Post(0)