关于printf的大部分参数祥解---------学会一门外语是多么重要啊!!!

#include

/************************************************************************/
//                                                                    
//作者:flyingleaf
//
//作用:简单测试printf的类型,主要是帮mm查看那些变态的printf参数
//
//日期:2004-12-27
//
//试验工具:vc7.0
//
/************************************************************************/

int _tmain(int argc, _TCHAR* argv[])
{
 float d=3.14;
 printf("d=%.10e/n",d); //这里输出的6位以后的都是垃圾数据了,不准确

 char ch[20];
 strcpy(ch,"123456780123");
 int n = 2,m = 10;
 //*.* 呢,前边的*定义的是总的宽度,后边的定义的是输出的个数。
 //如果后边的比前边的小,则使用空格在左侧补够m位。
 printf("%*.*s/n",m,n,ch);//这里输出“        12”
 printf("%*.*s/n",n,m,ch);//这里输出“1234567890”
 //补充,如果strlen(ch) < m的话,就输出strlen位 例如:
 strcpy(ch, "1234678");
 //这里结尾使用个hh作为输出,是为了更好的显示出输出12345678后到底光标到了那里
 printf("%*.*shh/n",m,n,ch);//这里输出“        12hh”
 printf("%*.*shh/n",n,m,ch);//这里输出“12345678hh”

 int y = 456;
 //这里的#8d,保持宽度的,如果不够8位,就在左侧用空格补够
 //如果超过8位,则有几位就输出几位。
 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);

 //这里的.8d估计大家都不陌生了吧?就是不够8位的时候左侧使用0补够
 //同样,如果超过8位就有几位输出几位
 printf("%.8d/n%.8x/n%.8o/n", y,y,y);

 //这里的*d估计有的人有点陌生,其实可以看作是#6d,效果是一样的.
 printf("%*d/n",6,y);

 //这里的%+6d中的+号有俩意思:一、输出的数字前面有+号,二、不够6位左侧补空格
 printf("%+6d/n",y);
 //这连个和上面的%+6d的意思基本一样,但是如果y的位数+1没有6大,就用0补,但是个数是不超过
 //6前面的0的个数。具体效果可以运行下看看
 printf("%+006d/n",y);
 printf("%+0006d/n",y);
 //补充上面,如果6比y的位数小的话,只输出+号和y本身
 //如果没有+号的话,则是使用0补充够6位 例如:
 printf("%06d/n",y);//输出“000456”

    //这里的-号是右侧补空格的意思 为了明显起见,我们仍旧使用hh作为结尾。
 printf("%-6dhh/n",y);
 //当然,如果这里的2没有y的位数大的话,就直接输出y,然后输出hh
 //这里的-号仅仅是右侧补空格的意思
 printf("%-2dhh/n",y);

 //一个利用printf来输出的例子
 int len = 0;
 //这里%n的意思是将%n前的字符串的长度符给len:
 //下面的例子是8 = strlen("hh") + strlen("123456");
 printf("hh%s%n /n", "123456",&len);
 printf("len=%d/n", len);

 //本来不打算写他了 但是带上吧
 //简单说明吧:.0f是小数点后0位,不带点 #.-0f就是带点 但是也是0位
 //而%g则省略所以的无效的0 如果没有小数,则不带点 #g则一个0都不可以少!
 printf("%.0fhh/n%#.0fhh/n%ghh/n%#ghh/n", 3.0,3.0,3.0,3.0);


 //一个不明白的.这里《c陷阱与缺陷》中说输出7个空壳再输出%号。我试的怎么就一个%号。
 //谁试的结果不一样请告诉我下:
csflyingleaf@163.com 谢谢了
 printf("%*%/n", 8);

 //好了,觉得不少了,如果不够了再给我发短信吧。
    return 0;
}

所有的输出:

d=3.1400001049e+000
        12
1234567801
        12hh
1234678hh
     456
   0x1c8
    0710
456
0x1c8
0710
456
0x1c8
0710
00000456
000001c8
00000710
   456
  +456
+00456
+00456
000456
456   hh
456hh
hh123456
len=8
3hh
3.hh
3hh
3.00000hh
%
Press any key to continue;

posted on 2004-12-27 23:50 漫天飞舞 阅读(532) 评论(6)   编辑  收藏
href="http://blog.vckbase.com/flyingleaf/Services/Pingback.aspx" rel="pingback"/>

评论

#  re: 关于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 explicit 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 them in the format string shall have one. 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. If the value is negative, this means to set the - flag (see below) and to use the absolute value as the field width.

?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 assume a type of int, but you can specify h, l, or L for other integer types.)

?A character that specifies 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: 关于printf的大部分参数祥解 2004-12-28 02:40 顺便
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 synonymous 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 uses 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 uses lower-case letters and %E uses upper-case. See Floating-Point Conversions, for details.


%g, %G

Print a floating-point number in either normal or exponential notation, whichever is more appropriate for its magnitude. %g uses 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 uses lower-case letters and
%A uses upper-case. See Floating-Point Conversions, for details.


%c

Print a single character. See Other Output Conversions.


%C

This is an alias for %lc which is supported for compatibility with the Unix standard.


%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 this conversion specification never produces any output.


%m

Print the string corresponding to the value of errno. (This is a GNU extension.) See Other Output Conversions.


%%

Print a literal % character. See Other Output Conversions.

If the syntax of a conversion specification is invalid, unpredictable things will happen, so don't do this. If there aren't 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 are unpredictable. If you supply more arguments than conversion specifications, the extra argument values are simply ignored; this is sometimes useful.

#  re: 关于printf的大部分参数祥解 2004-12-28 02:40 整数
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 that it uses the characters 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 sign if the value is positive.




For the signed %d and %i conversions, if the result doesn't 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 (respectively) to the result. This doesn't do anything useful for the %d, %i, or %u conversions. Using this flag produces output which can be parsed by the strtoul function (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

Pad the field with zeros instead of spaces. The zeros are placed after any indication of sign or base. This flag is ignored if the - flag is also specified, or if a precision is specified.

If a precision is supplied, it specifies the minimum number of digits to appear; leading zeros are produced if necessary. If you don't specify a precision, the number is printed with as many digits as it needs. If you convert a value of zero with an explicit precision of zero, then no characters at all are produced.

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 introduced in ISO C99.


h

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 introduced 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 introduced in Amendment 1 to ISO C90.


L
ll
q

Specifies that the argument is a long long int. (This type is an extension supported by the GNU C compiler. 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 comes from 4.4 BSD; a long long int is sometimes called a "quad" int.


t

Specifies that the argument is a ptrdiff_t.

This modifier was introduced in ISO C99.


z
Z

Specifies that the argument is a size_t.

z was introduced in ISO C99. Z is a GNU extension predating this addition and should not be used in new code.

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: 关于printf的大部分参数祥解 2004-12-28 02:41 浮点数
Floating-Point Conversions

This section discusses the conversion specifications for floating-point numbers: the %f, %e, %E, %g, and %G conversions.

The %f conversion prints its argument in fixed-point notation, producing output of the form [-]ddd.ddd, where the number of digits following the decimal point is controlled by the precision you specify.

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 conversions print the argument in the style of %e or %E (respectively) if the exponent would be less than -4 or greater than or equal to the precision; otherwise they use 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 large 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 but at 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 nan respectively if the conversion specifier is %a, %e, %f, or %g and it is [-]INF or NAN respectively if 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 result is 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 both of them.


#

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 digits of the integer part of the result into groups as specified by the locale specified for the LC_NUMERIC category; see General Numeric. This flag is a GNU extension.


0

Pad the field with zeros instead of spaces; the zeros are placed after any sign. This flag is ignored if the - flag is also specified.

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 cannot 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 promotions, any float arguments are 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 numbers were printed using this template 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 conversion drops trailing zeros.

#  re: 关于printf的大部分参数祥解 2004-12-28 02:41 其他
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 other flags are defined, and 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 there is a l modifier present the argument is expected to be of type 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). (Pointers might print differently in other systems.)

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 are 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: 关于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 explicit 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 them in the format string shall have one. 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. If the value is negative, this means to set the - flag (see below) and to use the absolute value as the field width.

?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 assume a type of int, but you can specify h, l, or L for other integer types.)

?A character that specifies 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: 关于printf的大部分参数祥解 2004-12-28 02:40 顺便
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 synonymous 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 uses 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 uses lower-case letters and %E uses upper-case. See Floating-Point Conversions, for details.


%g, %G

Print a floating-point number in either normal or exponential notation, whichever is more appropriate for its magnitude. %g uses 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 uses lower-case letters and
%A uses upper-case. See Floating-Point Conversions, for details.


%c

Print a single character. See Other Output Conversions.


%C

This is an alias for %lc which is supported for compatibility with the Unix standard.


%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 this conversion specification never produces any output.


%m

Print the string corresponding to the value of errno. (This is a GNU extension.) See Other Output Conversions.


%%

Print a literal % character. See Other Output Conversions.

If the syntax of a conversion specification is invalid, unpredictable things will happen, so don't do this. If there aren't 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 are unpredictable. If you supply more arguments than conversion specifications, the extra argument values are simply ignored; this is sometimes useful.

#  re: 关于printf的大部分参数祥解 2004-12-28 02:40 整数
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 that it uses the characters 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 sign if the value is positive.




For the signed %d and %i conversions, if the result doesn't 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 (respectively) to the result. This doesn't do anything useful for the %d, %i, or %u conversions. Using this flag produces output which can be parsed by the strtoul function (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

Pad the field with zeros instead of spaces. The zeros are placed after any indication of sign or base. This flag is ignored if the - flag is also specified, or if a precision is specified.

If a precision is supplied, it specifies the minimum number of digits to appear; leading zeros are produced if necessary. If you don't specify a precision, the number is printed with as many digits as it needs. If you convert a value of zero with an explicit precision of zero, then no characters at all are produced.

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 introduced in ISO C99.


h

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 introduced 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 introduced in Amendment 1 to ISO C90.


L
ll
q

Specifies that the argument is a long long int. (This type is an extension supported by the GNU C compiler. 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 comes from 4.4 BSD; a long long int is sometimes called a "quad" int.


t

Specifies that the argument is a ptrdiff_t.

This modifier was introduced in ISO C99.


z
Z

Specifies that the argument is a size_t.

z was introduced in ISO C99. Z is a GNU extension predating this addition and should not be used in new code.

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: 关于printf的大部分参数祥解 2004-12-28 02:41 浮点数
Floating-Point Conversions

This section discusses the conversion specifications for floating-point numbers: the %f, %e, %E, %g, and %G conversions.

The %f conversion prints its argument in fixed-point notation, producing output of the form [-]ddd.ddd, where the number of digits following the decimal point is controlled by the precision you specify.

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 conversions print the argument in the style of %e or %E (respectively) if the exponent would be less than -4 or greater than or equal to the precision; otherwise they use 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 large 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 but at 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 nan respectively if the conversion specifier is %a, %e, %f, or %g and it is [-]INF or NAN respectively if 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 result is 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 both of them.


#

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 digits of the integer part of the result into groups as specified by the locale specified for the LC_NUMERIC category; see General Numeric. This flag is a GNU extension.


0

Pad the field with zeros instead of spaces; the zeros are placed after any sign. This flag is ignored if the - flag is also specified.

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 cannot 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 promotions, any float arguments are 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 numbers were printed using this template 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 conversion drops trailing zeros.

#  re: 关于printf的大部分参数祥解 2004-12-28 02:41 其他
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 other flags are defined, and 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 there is a l modifier present the argument is expected to be of type 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). (Pointers might print differently in other systems.)

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 are 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.
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值