C examples for stdio.h:snprintf
function
<cstdio> <stdio.h>
Write formatted output to sized buffer,
int snprintf ( char * s, size_t n, const char * format, ... );
Parameter | Description |
---|---|
s | Pointer to a buffer. |
n | Maximum number of bytes to be used in the buffer. |
format | format string as format in printf. |
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, number of characters written.
On occurs, a negative number is returned.
#include <stdio.h> int main ()//from ww w . java2 s . c om { char buffer [100]; int cx; cx = snprintf ( buffer, 100, "test %d is %d %f", 60, 60/2,1.1234 ); if (cx>=0 && cx<100) snprintf ( buffer+cx, 100-cx, ", this is a test and the half of that is %d.", 60/2/2 ); puts (buffer); return 0; }