⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sprintf.c

📁 Newlib 嵌入式 C库 标准实现代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * Copyright (c) 1990 The Regents of the University of California. * All rights reserved. * * Redistribution and use in source and binary forms are permitted * provided that the above copyright notice and this paragraph are * duplicated in all such forms and that any documentation, * advertising materials, and other materials related to such * distribution and use acknowledge that the software was developed * by the University of California, Berkeley.  The name of the * University may not be used to endorse or promote products derived * from this software without specific prior written permission. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. *//*FUNCTION<<sprintf>>, <<fprintf>>, <<printf>>, <<snprintf>>, <<asprintf>>, <<asnprintf>>---format outputINDEX	fprintfINDEX	printfINDEX	asprintfINDEX	sprintfINDEX	snprintfINDEX	asnprintfANSI_SYNOPSIS        #include <stdio.h>        int printf(const char *<[format]> [, <[arg]>, ...]);        int fprintf(FILE *<[fd]>, const char *<[format]> [, <[arg]>, ...]);        int sprintf(char *<[str]>, const char *<[format]> [, <[arg]>, ...]);        int snprintf(char *<[str]>, size_t <[size]>, const char *<[format]>                     [, <[arg]>, ...]);        int asprintf(char **<[strp]>, const char *<[format]> [, <[arg]>, ...]);        char *asnprintf(char *<[str]>, size_t *<[size]>, const char *<[format]>                        [, <[arg]>, ...]);        int _printf_r(struct _reent *<[ptr]>, const char *<[format]>                      [, <[arg]>, ...]);        int _fprintf_r(struct _reent *<[ptr]>, FILE *<[fd]>,                       const char *<[format]> [, <[arg]>, ...]);        int _sprintf_r(struct _reent *<[ptr]>, char *<[str]>,                       const char *<[format]> [, <[arg]>, ...]);        int _snprintf_r(struct _reent *<[ptr]>, char *<[str]>, size_t <[size]>,                        const char *<[format]> [, <[arg]>, ...]);        int _asprintf_r(struct _reent *<[ptr]>, char **<[strp]>,                        const char *<[format]> [, <[arg]>, ...]);        char *_asnprintf_r(struct _reent *<[ptr]>, char *<[str]>,                           size_t *<[size]>, const char *<[format]>                           [, <[arg]>, ...]);DESCRIPTION        <<printf>> accepts a series of arguments, applies to each a        format specifier from <<*<[format]>>>, and writes the        formatted data to <<stdout>>, without a terminating NUL        character.  The behavior of <<printf>> is undefined if there        are not enough arguments for the format.  <<printf>> returns        when it reaches the end of the format string.  If there are        more arguments than the format requires, excess arguments are        ignored.        <<fprintf>> is like <<printf>>, except that output is directed        to the stream <[fd]> rather than <<stdout>>.        <<sprintf>> is like <<printf>>, except that output is directed        to the buffer <[str]>, and a terminating NUL is output.        Behavior is undefined if more output is generated than the        buffer can hold.        <<snprintf>> is like <<sprintf>>, except that output is        limited to at most <[size]> bytes, including the terminating        <<NUL>>.  As a special case, if <[size]> is 0, <[str]> can be        NULL, and <<snprintf>> merely calculates how many bytes would        be printed.        <<asprintf>> is like <<sprintf>>, except that the output is        stored in a dynamically allocated buffer, <[pstr]>, which        should be freed later with <<free>>.        <<asnprintf>> is like <<sprintf>>, except that the return type        is either the original <[str]> if it was large enough, or a        dynamically allocated string if the output exceeds *<[size]>;        the length of the result is returned in *<[size]>.  When        dynamic allocation occurs, the contents of the original        <[str]> may have been modified.        For <<sprintf>>, <<snprintf>>, and <<asnprintf>>, the behavior	is undefined if the output <<*<[str]>>> overlaps with one of	the arguments.  Behavior is also undefined if the argument for	<<%n>> within <<*<[format]>>> overlaps another argument.        <[format]> is a pointer to a character string containing two	types of objects: ordinary characters (other than <<%>>),	which are copied unchanged to the output, and conversion	specifications, each of which is introduced by <<%>>. (To	include <<%>> in the output, use <<%%>> in the format string.)	A conversion specification has the following form:.       %[<[pos]>][<[flags]>][<[width]>][.<[prec]>][<[size]>]<[type]>        The fields of the conversion specification have the following        meanings:        O+	o <[pos]>        Conversions normally consume arguments in the order that they        are presented.  However, it is possible to consume arguments        out of order, and reuse an argument for more than one        conversion specification (although the behavior is undefined        if the same argument is requested with different types), by        specifying <[pos]>, which is a decimal integer followed by        '$'.  The integer must be between 1 and <NL_ARGMAX> from        limits.h, and if argument <<%n$>> is requested, all earlier        arguments must be requested somewhere within <[format]>.  If        positional parameters are used, then all conversion        specifications except for <<%%>> must specify a position.	o <[flags]>	<[flags]> is an optional sequence of characters which control	output justification, numeric signs, decimal points, trailing	zeros, and octal and hex prefixes.  The flag characters are	minus (<<->>), plus (<<+>>), space ( ), zero (<<0>>), sharp	(<<#>>), and quote (<<'>>).  They can appear in any	combination, although not all flags can be used for all	conversion specification types.		o+		o '			Since newlib only supports the C locale, this			flag has no effect in this implementation.			But in other locales, when <[type]> is <<i>>,			<<d>>, <<u>>, <<f>>, <<F>>, <<g>>, or <<G>>,			the locale-dependent thousand's separator is			inserted prior to zero padding.		o -			The result of the conversion is left			justified, and the right is padded with			blanks.  If you do not use this flag, the			result is right justified, and padded on the			left.	        o +			The result of a signed conversion (as			determined by <[type]> of <<d>>, <<i>>, <<a>>,			<<A>>, <<e>>, <<E>>, <<f>>, <<F>>, <<g>>, or			<<G>>) will always begin with a plus or minus			sign.  (If you do not use this flag, positive			values do not begin with a plus sign.)		o " " (space)			If the first character of a signed conversion		        specification is not a sign, or if a signed		        conversion results in no characters, the		        result will begin with a space.  If the space		        ( ) flag and the plus (<<+>>) flag both		        appear, the space flag is ignored.	        o 0			If the <[type]> character is <<d>>, <<i>>,			<<o>>, <<u>>, <<x>>, <<X>>, <<a>>, <<A>>,			<<e>>, <<E>>, <<f>>, <<g>>, or <<G>>: leading			zeros are used to pad the field width			(following any indication of sign or base); no			spaces are used for padding.  If the zero			(<<0>>) and minus (<<->>) flags both appear,			the zero (<<0>>) flag will be ignored.  For			<<d>>, <<i>>, <<o>>, <<u>>, <<x>>, and <<X>>			conversions, if a precision <[prec]> is			specified, the zero (<<0>>) flag is ignored.			Note that <<0>> is interpreted as a flag, not		        as the beginning of a field width.	        o #			The result is to be converted to an			alternative form, according to the <[type]>			character:			o+			o o				Increases precision to force the first				digit of the result to be a zero.			o x				A non-zero result will have a <<0x>>				prefix.			o X				A non-zero result will have a <<0X>>				prefix.			o a, A, e, E, f, or F				The result will always contain a			        decimal point even if no digits follow			        the point.  (Normally, a decimal point			        appears only if a digit follows it.)			        Trailing zeros are removed.			o g or G				The result will always contain a			        decimal point even if no digits follow			        the point.  Trailing zeros are not			        removed.			o all others				Undefined.			o-		o-	o <[width]>		<[width]> is an optional minimum field width.  You can		either specify it directly as a decimal integer, or		indirectly by using instead an asterisk (<<*>>), in		which case an <<int>> argument is used as the field		width.  If positional arguments are used, then the		width must also be specified positionally as <<*m$>>,		with m as a decimal integer.  Negative field widths		are treated as specifying the minus (<<->>) flag for		left justfication, along with a positive field width.		The resulting format may be wider than the specified		width.	o <[prec]>		<[prec]> is an optional field; if present, it is		introduced with `<<.>>' (a period). You can specify		the precision either directly as a decimal integer or		indirectly by using an asterisk (<<*>>), in which case		an <<int>> argument is used as the precision.  If		positional arguments are used, then the precision must		also be specified positionally as <<*m$>>, with m as a		decimal integer.  Supplying a negative precision is		equivalent to omitting the precision.  If only a		period is specified the precision is zero. The effect		depends on the conversion <[type]>.		o+		o d, i, o, u, x, or X			Minimum number of digits to appear.  If no			precision is given, defaults to 1.		o a or A			Number of digits to appear after the decimal			point.  If no precision is given, the			precision defaults to the minimum needed for			an exact representation.		o e, E, f or F			Number of digits to appear after the decimal			point.  If no precision is given, the			precision defaults to 6.		o g or G			Maximum number of significant digits.  A			precision of 0 is treated the same as a			precision of 1.  If no precision is given, the			precision defaults to 6.		o s or S			Maximum number of characters to print from the			string.  If no precision is given, the entire			string is printed.		o all others			undefined.		o-	o <[size]>		<[size]> is an optional modifier that changes the data		type that the corresponding argument has.  Behavior is		unspecified if a size is given that does not match the		<[type]>.		o+		o hh			With <<d>>, <<i>>, <<o>>, <<u>>, <<x>>, or			<<X>>, specifies that the argument should be			converted to a <<signed char>> or <<unsigned			char>> before printing.			With <<n>>, specifies that the argument is a			pointer to a <<signed char>>.		o h			With <<d>>, <<i>>, <<o>>, <<u>>, <<x>>, or			<<X>>, specifies that the argument should be			converted to a <<short>> or <<unsigned short>>			before printing.			With <<n>>, specifies that the argument is a			pointer to a <<short>>.		o l			With <<d>>, <<i>>, <<o>>, <<u>>, <<x>>, or			<<X>>, specifies that the argument is a			<<long>> or <<unsigned long>>.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -