/*Funkcja znajduje się w bibliotece stdio.h
int sprintf (char *bufor, /*zmienna typu tablica znaków do której zapisujemy dane */
const char *format, /*format zapisu */
[, argumenty]…); /* argumenty do kompresji */
Poniższe tabele reprezentują zawartość zmiennej format, która jest statycznym tekstem mogącym zawierać formatowanie, które należy umieścić po znaku "%" :
np. ile=sprintf(char *bufor , "%.3f" , 196.1897); spowoduje wpiasanie do bufor wartości "196.190" gdyż .3 oznacza zaokrąglenie zminennej typu float (f) do 3 miejsc po przecinku.
Zmienna ile przyjmie wartość 7.
Character | Argument Type | Output Format |
d | int | Signed decimal number |
u | unsigned int | Unsigned decimal number |
o | unsigned int | Unsigned octal number |
x | unsigned int | Unsigned hexadecimal number using "0123456789abcdef" |
X | unsigned int | Unsigned hexadecimal number using "0123456789ABCEDF" |
f | float | Floating-point number using the format [-]dddd.dddd |
e | float | Floating-point number using the format [-]d.dddde[-]dd |
E | float | Floating-point number using the format [-]d.ddddE[-]dd |
g | float | Floating-point number using either e or f format, whichever is more compact for the specified value and precision |
G | float | Identical to the g format except that (where applicable) E precedes the exponent instead of e |
c | char | Single character |
s | generic * | String with a terminating null character |
p | generic * | Pointer using the format t:aaaa where t is the memory type the pointer references (c: code, i: data/idata, x: xdata, p: pdata) and aaaa is the hexadecimal address |
Flag | Meaning |
- | Left justify the output in the specified field width. |
+ | Prefix the output value with a + or - sign if the output is a signed type. |
blank (' ') | Prefix the output value with a blank if it is a signed positive value. Otherwise, no blank is prefixed. |
# | Prefixes a non-zero output value with 0, 0x, or 0X when used with o, x, and X field types, respectively. When used with the e, E, f, g, and G field types, the # flag forces the output value to include a decimal point. The # flag is ignored in all other cases. |
* | Ignore format specifier. |
Type | Meaning of Precision Field |
d, u, o, x, X | The precision field is where you specify the minimum number of digits that are included in the output value. Digits are not truncated if the number of digits in the argument exceeds that defined in the precision field. If the number of digits in the argument is less than the precision field, the output value is padded on the left with zeros. |
f | The precision field is where you specify the number of digits to the right of the decimal point. The last digit is rounded. |
e, E | The precision field is where you specify the number of digits to the right of the decimal point. The last digit is rounded. |
g, G | The precision field is where you specify the maximum number of significant digits in the output value. |
c, p | The precision field has no effect on these field types. |
s | The precision field is where you specify the maximum number of characters in the output value. Excess characters are not output. |
//Przykładowy program //Biblioteki #include //Deklaracja zmiennych globalnych int ile_znakow; char naips[9]; //tablica 10 znaków (0-9 char-ów) void main(void){ ile_znakow=sprintf(napis,"%d",1245); /* Powyższa operacja spowodoała wpisanie do zmiennej napis wartość "1245" jako tekstu a nie jako int. Do zmiennej ile_znaków zostało wpisane 4 bo zmienna napis po zmianie ma 4 znaki "1245"*/ }
« poprzednia | następna » |
---|