📄 sprintf.c
字号:
With <<c>>, specifies that the argument has type <<wint_t>>. With <<s>>, specifies that the argument is a pointer to <<wchar_t>>. With <<n>>, specifies that the argument is a pointer to a <<long>>. With <<a>>, <<A>>, <<e>>, <<E>>, <<f>>, <<F>>, <<g>>, or <<G>>, has no effect (because of vararg promotion rules, there is no need to distinguish between <<float>> and <<double>>). o ll With <<d>>, <<i>>, <<o>>, <<u>>, <<x>>, or <<X>>, specifies that the argument is a <<long long>> or <<unsigned long long>>. With <<n>>, specifies that the argument is a pointer to a <<long long>>. o j With <<d>>, <<i>>, <<o>>, <<u>>, <<x>>, or <<X>>, specifies that the argument is an <<intmax_t>> or <<uintmax_t>>. With <<n>>, specifies that the argument is a pointer to an <<intmax_t>>. o z With <<d>>, <<i>>, <<o>>, <<u>>, <<x>>, or <<X>>, specifies that the argument is a <<ssize_t>> or <<size_t>>. With <<n>>, specifies that the argument is a pointer to a <<ssize_t>>. o t With <<d>>, <<i>>, <<o>>, <<u>>, <<x>>, or <<X>>, specifies that the argument is a <<ptrdiff_t>>. With <<n>>, specifies that the argument is a pointer to a <<ptrdiff_t>>. o L With <<a>>, <<A>>, <<e>>, <<E>>, <<f>>, <<F>>, <<g>>, or <<G>>, specifies that the argument is a <<long double>>. o- o <[type]> <[type]> specifies what kind of conversion <<printf>> performs. Here is a table of these: o+ o % Prints the percent character (<<%>>). o c Prints <[arg]> as single character. If the <<l>> size specifier is in effect, a multibyte character is printed. o C Short for <<%lc>>. o s Prints the elements of a pointer to <<char>> until the precision or a null character is reached. If the <<l>> size specifier is in effect, the pointer is to an array of <<wchar_t>>, and the string is converted to multibyte characters before printing. o S Short for <<%ls>>. o d or i Prints a signed decimal integer; takes an <<int>>. Leading zeros are inserted as necessary to reach the precision. A precision of 0 produces an empty string. o D Newlib extension, short for <<%ld>>. o o Prints an unsigned octal integer; takes an <<unsigned>>. Leading zeros are inserted as necessary to reach the precision. A precision of 0 produces an empty string. o O Newlib extension, short for <<%lo>>. o u Prints an unsigned decimal integer; takes an <<unsigned>>. Leading zeros are inserted as necessary to reach the precision. A precision of 0 produces an empty string. o U Newlib extension, short for <<%lu>>. o x Prints an unsigned hexadecimal integer (using <<abcdef>> as digits beyond <<9>>); takes an <<unsigned>>. Leading zeros are inserted as necessary to reach the precision. A precision of 0 produces an empty string. o X Like <<x>>, but uses <<ABCDEF>> as digits beyond <<9>>. o f Prints a signed value of the form <<[-]9999.9999>>, with the precision determining how many digits follow the decimal point; takes a <<double>> (remember that <<float>> promotes to <<double>> as a vararg). The low order digit is rounded to even. If the precision results in at most DECIMAL_DIG digits, the result is rounded correctly; if more than DECIMAL_DIG digits are printed, the result is only guaranteed to round back to the original value. If the value is infinite, the result is <<inf>>, and no zero padding is performed. If the value is not a number, the result is <<nan>>, and no zero padding is performed. o F Like <<f>>, but uses <<INF>> and <<NAN>> for non-finite numbers. o e Prints a signed value of the form <<[-]9.9999e[+|-]999>>; takes a <<double>>. The digit before the decimal point is non-zero if the value is non-zero. The precision determines how many digits appear between <<.>> and <<e>>, and the exponent always contains at least two digits. The value zero has an exponent of zero. If the value is not finite, it is printed like <<f>>. o E Like <<e>>, but using <<E>> to introduce the exponent, and like <<F>> for non-finite values. o g Prints a signed value in either <<f>> or <<e>> form, based on the given value and precision---an exponent less than -4 or greater than the precision selects the <<e>> form. Trailing zeros and the decimal point are printed only if necessary; takes a <<double>>. o G Like <<g>>, except use <<F>> or <<E>> form. o a Prints a signed value of the form <<[-]0x1.ffffp[+|-]9>>; takes a <<double>>. The letters <<abcdef>> are used for digits beyond <<9>>. The precision determines how many digits appear after the decimal point. The exponent contains at least one digit, and is a decimal value representing the power of 2; a value of 0 has an exponent of 0. Non-finite values are printed like <<f>>. o A Like <<a>>, except uses <<X>>, <<P>>, and <<ABCDEF>> instead of lower case. o n Takes a pointer to <<int>>, and stores a count of the number of bytes written so far. No output is created. o p Takes a pointer to <<void>>, and prints it in an implementation-defined format. This implementation is similar to <<%#tx>>), except that <<0x>> appears even for the NULL pointer. o- O- <<_printf_r>>, <<_fprintf_r>>, <<_asprintf_r>>, <<_sprintf_r>>, <<_snprintf_r>>, <<_asnprintf_r>> are simply reentrant versions of the functions above.RETURNSOn success, <<sprintf>> and <<asprintf>> return the number of bytes inthe output string, except the concluding <<NUL>> is not counted.<<snprintf>> returns the number of bytes that would be in the outputstring, except the concluding <<NUL>> is not counted. <<printf>> and<<fprintf>> return the number of characters transmitted.<<asnprintf>> returns the original <[str]> if there was enough room,otherwise it returns an allocated string.If an error occurs, the result of <<printf>>, <<fprintf>>,<<snprintf>>, and <<asprintf>> is a negative value, and the result of<<asnprintf>> is NULL. No error returns occur for <<sprintf>>. For<<printf>> and <<fprintf>>, <<errno>> may be set according to<<fputc>>. For <<asprintf>> and <<asnprintf>>, <<errno>> may be setto ENOMEM if allocation fails, and for <<snprintf>>, <<errno>> may beset to EOVERFLOW if <[size]> or the output length exceeds INT_MAX.PORTABILITYANSI C requires <<printf>>, <<fprintf>>, <<sprintf>>, and<<snprintf>>. <<asprintf>> and <<asnprintf>> are newlib extensions.The ANSI C standard specifies that implementations must support atleast formatted output of up to 509 characters. This implementationhas no inherent limit.Depending on how newlib was configured, not all format specifiers aresupported.Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,<<lseek>>, <<read>>, <<sbrk>>, <<write>>.*/#include <_ansi.h>#include <reent.h>#include <stdio.h>#ifdef _HAVE_STDC#include <stdarg.h>#else#include <varargs.h>#endif#include <limits.h>#include "local.h"int#ifdef _HAVE_STDC_DEFUN(_sprintf_r, (ptr, str, fmt), struct _reent *ptr _AND char *str _AND _CONST char *fmt _DOTS)#else_sprintf_r(ptr, str, fmt, va_alist) struct _reent *ptr; char *str; _CONST char *fmt; va_dcl#endif{ int ret; va_list ap; FILE f; f._flags = __SWR | __SSTR; f._bf._base = f._p = (unsigned char *) str; f._bf._size = f._w = INT_MAX; f._file = -1; /* No file. */#ifdef _HAVE_STDC va_start (ap, fmt);#else va_start (ap);#endif ret = _vfprintf_r (ptr, &f, fmt, ap); va_end (ap); *f._p = 0; return (ret);}#ifndef _REENT_ONLYint#ifdef _HAVE_STDC_DEFUN(sprintf, (str, fmt), char *str _AND _CONST char *fmt _DOTS)#elsesprintf(str, fmt, va_alist) char *str; _CONST char *fmt; va_dcl#endif{ int ret; va_list ap; FILE f; f._flags = __SWR | __SSTR; f._bf._base = f._p = (unsigned char *) str; f._bf._size = f._w = INT_MAX; f._file = -1; /* No file. */#ifdef _HAVE_STDC va_start (ap, fmt);#else va_start (ap);#endif ret = _vfprintf_r (_REENT, &f, fmt, ap); va_end (ap); *f._p = 0; return (ret);}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -