C examples for stdio.h:sprintf
function
<cstdio> <stdio.h>
Write formatted data to string
int sprintf ( char * str, const char * format, ... );
Parameter | Description |
---|---|
str | Pointer to a buffer |
format | a format string |
A format specifier follows this prototype:
%[flags] [width] [.precision] [length] specifier
specifier character defines the type and the interpretation of its corresponding argument:
specifier | Output |
---|---|
d or i | Signed decimal integer |
u | Unsigned decimal integer |
o | Unsigned octal |
x | Unsigned hexadecimal integer |
X | Unsigned hexadecimal integer (uppercase) |
f | Decimal floating point, lowercase |
F | Decimal floating point, uppercase |
e | Scientific notation (mantissa/exponent), lowercase |
E | Scientific notation (mantissa/exponent), uppercase |
g | Use the shortest representation: %e or %f |
G | Use the shortest representation: %E or %F |
a | Hexadecimal floating point, lowercase |
A | Hexadecimal floating point, uppercase |
c | Character |
s | String of characters |
p | Pointer address |
n | Nothing printed. The argument must be a pointer to a signed int. The number of characters written so far is saved to the signed int. |
% | A % followed by another % character will write a single % to the stream. |
The format specifier can contain the following sub-specifiers. They are optional.
The flags can be the following value.
flags | description |
---|---|
- | Left-justify within the given field width; Right justification is the default. |
+ | Add a plus or minus sign (+ or -) for numbers. By default, only negative numbers are preceded with a - sign. |
(space) | If no sign is written, a blank space is inserted before the value. |
# | Used with o, x or X specifiers, the value is preceeded with 0, 0x or 0X respectively for nonzero values. |
0 | Left-pads the number with 0 instead of spaces when padding is specified. |
The width can the following values;.
width | description |
---|---|
(number) | Minimum number of characters to be printed. If the value is shorter, blank spaces is padding. If the result is wider, it is not truncated. |
* | An additional integer value preceding the argument that has to be formatted. |
The .precision can have the following value.
.precision | description |
---|---|
.number | For specifiers (d, i, o, u, x, X): precision sets the minimum number of digits. If the value is shorter, the result is padded with leading zeros. No truncating if the result is wider. A precision of 0 means that no character is written for the value 0. For a, A, e, E, f and F specifiers: it sets the number of digits after the decimal point. Default is 6. For g and G: it is the maximum number of significant digits. For s: it is the maximum number of characters to be printed. By default all characters are printed until the ending null character is encountered. If the period is specified without a value for precision, 0 is assumed. |
.* | The precision is not set, but as an additional integer value argument preceding the argument that has to be formatted. |
On success, the total number of characters written is returned.
On failure, a negative number is returned.
#include <stdio.h> int main ()//from w ww.j a va 2s . c o m { char buffer [50]; int n, a=5, b=3; n=sprintf (buffer, "%d plus %d is %d", a, b, a+b); printf ("[%s] is a string %d chars long\n",buffer,n); return 0; }