📄 stdio.texi
字号:
Specifies that the argument is a @code{size_t}. This is a GNU extension.@end tableHere is an example. Using the template string:@smallexample"|%5d|%-5d|%+5d|%+-5d|% 5d|%05d|%5.0d|%5.2d|%d|\n"@end smallexample@noindentto print numbers using the different options for the @samp{%d}conversion gives results like:@smallexample| 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|@end smallexampleIn particular, notice what happens in the last case where the numberis too large to fit in the minimum field width specified.Here are some more examples showing how unsigned integers print undervarious format options, using the template string:@smallexample"|%5u|%5o|%5x|%5X|%#5o|%#5x|%#5X|%#10.8x|\n"@end smallexample@smallexample| 0| 0| 0| 0| 0| 0x0| 0X0|0x00000000|| 1| 1| 1| 1| 01| 0x1| 0X1|0x00000001||100000|303240|186a0|186A0|0303240|0x186a0|0X186A0|0x000186a0|@end smallexample@node Floating-Point Conversions@subsection Floating-Point ConversionsThis section discusses the conversion specifications for floating-pointnumbers: the @samp{%f}, @samp{%e}, @samp{%E}, @samp{%g}, and @samp{%G}conversions.The @samp{%f} conversion prints its argument in fixed-point notation,producing output of the form@w{[@code{-}]@var{ddd}@code{.}@var{ddd}},where the number of digits following the decimal point is controlledby the precision you specify.The @samp{%e} conversion prints its argument in exponential notation,producing output of the form@w{[@code{-}]@var{d}@code{.}@var{ddd}@code{e}[@code{+}|@code{-}]@var{dd}}.Again, the number of digits following the decimal point is controlled bythe precision. The exponent always contains at least two digits. The@samp{%E} conversion is similar but the exponent is marked with the letter@samp{E} instead of @samp{e}.The @samp{%g} and @samp{%G} conversions print the argument in the styleof @samp{%e} or @samp{%E} (respectively) if the exponent would be lessthan -4 or greater than or equal to the precision; otherwise they use the@samp{%f} style. Trailing zeros are removed from the fractional portionof the result and a decimal-point character appears only if it isfollowed by a digit.The following flags can be used to modify the behavior:@comment We use @asis instead of @samp so we can have ` ' as an item.@table @asis@item @samp{-}Left-justify the result in the field. Normally the result isright-justified.@item @samp{+}Always include a plus or minus sign in the result.@item @samp{ }If the result doesn't start with a plus or minus sign, prefix it with aspace instead. Since the @samp{+} flag ensures that the result includesa sign, this flag is ignored if you supply both of them.@item @samp{#}Specifies that the result should always include a decimal point, evenif no digits follow it. For the @samp{%g} and @samp{%G} conversions,this also forces trailing zeros after the decimal point to be leftin place where they would otherwise be removed.@item @samp{0}Pad the field with zeros instead of spaces; the zeros are placedafter any sign. This flag is ignored if the @samp{-} flag is alsospecified.@end tableThe precision specifies how many digits follow the decimal-pointcharacter for the @samp{%f}, @samp{%e}, and @samp{%E} conversions. Forthese conversions, the default precision is @code{6}. If the precisionis explicitly @code{0}, this suppresses the decimal point characterentirely. For the @samp{%g} and @samp{%G} conversions, the precisionspecifies how many significant digits to print. Significant digits arethe first digit before the decimal point, and all the digits after it.If the precision @code{0} or not specified for @samp{%g} or @samp{%G},it is treated like a value of @code{1}. If the value being printedcannot be expressed accurately in the specified number of digits, thevalue is rounded to the nearest number that fits.Without a type modifier, the floating-point conversions use an argumentof type @code{double}. (By the default argument promotions, any@code{float} arguments are automatically converted to @code{double}.)The following type modifier is supported:@table @samp@item LAn uppercase @samp{L} specifies that the argument is a @code{longdouble}.@end tableHere are some examples showing how numbers print using the variousfloating-point conversions. All of the numbers were printed usingthis template string:@smallexample"|%12.4f|%12.4e|%12.4g|\n"@end smallexampleHere is the output:@smallexample| 0.0000| 0.0000e+00| 0|| 1.0000| 1.0000e+00| 1|| -1.0000| -1.0000e+00| -1|| 100.0000| 1.0000e+02| 100|| 1000.0000| 1.0000e+03| 1000|| 10000.0000| 1.0000e+04| 1e+04|| 12345.0000| 1.2345e+04| 1.234e+04|| 100000.0000| 1.0000e+05| 1e+05|| 123456.0000| 1.2346e+05| 1.234e+05|@end smallexampleNotice how the @samp{%g} conversion drops trailing zeros.@node Other Output Conversions@subsection Other Output ConversionsThis section describes miscellaneous conversions for @code{printf}.The @samp{%c} conversion prints a single character. The @code{int}argument is first converted to an @code{unsigned char}. The @samp{-}flag can be used to specify left-justification in the field, but noother flags are defined, and no precision or type modifier can be given.For example:@smallexampleprintf ("%c%c%c%c%c", 'h', 'e', 'l', 'l', 'o');@end smallexample@noindentprints @samp{hello}.The @samp{%s} conversion prints a string. The corresponding argumentmust be of type @code{char *} (or @code{const char *}). A precision canbe specified to indicate the maximum number of characters to write;otherwise characters in the string up to but not including theterminating null character are written to the output stream. The@samp{-} 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:@smallexampleprintf ("%3s%-6s", "no", "where");@end smallexample@noindentprints @samp{ nowhere }.If you accidentally pass a null pointer as the argument for a @samp{%s}conversion, the GNU library prints it as @samp{(null)}. We think thisis more useful than crashing. But it's not good practice to pass a nullargument intentionally.The @samp{%m} conversion prints the string corresponding to the errorcode in @code{errno}. @xref{Error Messages}. Thus:@smallexamplefprintf (stderr, "can't open `%s': %m\n", filename);@end smallexample@noindentis equivalent to:@smallexamplefprintf (stderr, "can't open `%s': %s\n", filename, strerror (errno));@end smallexample@noindentThe @samp{%m} conversion is a GNU C library extension.The @samp{%p} conversion prints a pointer value. The correspondingargument must be of type @code{void *}. In practice, you can use anytype of pointer.In the GNU system, non-null pointers are printed as unsigned integers,as if a @samp{%#x} conversion were used. Null pointers print as@samp{(nil)}. (Pointers might print differently in other systems.)For example:@smallexampleprintf ("%p", "testing");@end smallexample@noindentprints @samp{0x} followed by a hexadecimal number---the address of thestring constant @code{"testing"}. It does not print the word@samp{testing}.You can supply the @samp{-} flag with the @samp{%p} conversion tospecify left-justification, but no other flags, precision, or typemodifiers are defined.The @samp{%n} conversion is unlike any of the other output conversions.It uses an argument which must be a pointer to an @code{int}, butinstead of printing anything it stores the number of characters printedso far by this call at that location. The @samp{h} and @samp{l} typemodifiers are permitted to specify that the argument is of type@code{short int *} or @code{long int *} instead of @code{int *}, but noflags, field width, or precision are permitted.For example,@smallexampleint nchar;printf ("%d %s%n\n", 3, "bears", &nchar);@end smallexample@noindentprints:@smallexample3 bears@end smallexample@noindentand sets @code{nchar} to @code{7}, because @samp{3 bears} is seven characters.The @samp{%%} conversion prints a literal @samp{%} character. Thisconversion doesn't use an argument, and no flags, field width,precision, or type modifiers are permitted.@node Formatted Output Functions@subsection Formatted Output FunctionsThis section describes how to call @code{printf} and related functions.Prototypes for these functions are in the header file @file{stdio.h}.Because these functions take a variable number of arguments, you@emph{must} declare prototypes for them before using them. Of course,the easiest way to make sure you have all the right prototypes is tojust include @file{stdio.h}.@pindex stdio.h@comment stdio.h@comment ANSI@deftypefun int printf (const char *@var{template}, @dots{})The @code{printf} function prints the optional arguments under thecontrol of the template string @var{template} to the stream@code{stdout}. It returns the number of characters printed, or anegative value if there was an output error.@end deftypefun@comment stdio.h@comment ANSI@deftypefun int fprintf (FILE *@var{stream}, const char *@var{template}, @dots{})This function is just like @code{printf}, except that the output iswritten to the stream @var{stream} instead of @code{stdout}.@end deftypefun@comment stdio.h@comment ANSI@deftypefun int sprintf (char *@var{s}, const char *@var{template}, @dots{})This is like @code{printf}, except that the output is stored in the characterarray @var{s} instead of written to a stream. A null character is writtento mark the end of the string.The @code{sprintf} function returns the number of characters stored inthe array @var{s}, not including the terminating null character.The behavior of this function is undefined if copying takes placebetween objects that overlap---for example, if @var{s} is also givenas an argument to be printed under control of the @samp{%s} conversion.@xref{Copying and Concatenation}.@strong{Warning:} The @code{sprintf} function can be @strong{dangerous}because it can potentially output more characters than can fit in theallocation size of the string @var{s}. Remember that the field widthgiven in a conversion specification is only a @emph{minimum} value.To avoid this problem, you can use @code{snprintf} or @code{asprintf},described below.@end deftypefun@comment stdio.h@comment GNU@deftypefun int snprintf (char *@var{s}, size_t @var{size}, const char *@var{template}, @dots{})The @code{snprintf} function is similar to @code{sprintf}, except thatthe @var{size} argument specifies the maximum number of characters toproduce. The trailing null character is counted towards this limit, soyou should allocate at least @var{size} characters for the string @var{s}.The return value is the number of characters stored, not including theterminating null. If this value equals @code{@var{size} - 1}, thenthere was not enough space in @var{s} for all the output. You shouldtry again with a bigger output string. Here is an example of doingthis:@smallexample@group/* @r{Construct a message describing the value of a variable} @r{whose name is @var{name} and whose value is @var{value}.} */char *make_message (char *name, char *value)@{ /* @r{Guess we need no more than 100 chars of space.} */ int size = 100; char *buffer = (char *) xmalloc (size);@end group@group while (1) @{ /* @r{Try to print in the allocated space.} */ int nchars = snprintf (buffer, size, "value of %s is %s", name, value); /* @r{If that worked, return the string.} */ if (nchars < size) return buffer; /* @r{Else try again with twice as much space.} */ size *= 2; buffer = (char *) xrealloc (size, buffer); @}@}@end group@end smallexample
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -