Snprintf function usage, snprintf function
Int snprintf (char * restrict buf, size_t n, const char * restrict format ,...);
Function Description: a maximum of N-1 characters can be copied from the source string to the target string, followed by 0.
Function return value: If the return value is successful, the length of the Written string is returned. If an error occurs, a negative value is returned. Note that only when the return value is non-negative and less than n, indicates that the string has been fully written.
# Include <stdio. h>
# Include <stdlib. h>
Int main ()
{
Char str1 [10] = {0 ,};
Snprintf (str1, sizeof (str), "01234567890123456 ");
Printf ("str1 = % s/n", str1 );
Return 0;
}
Result
Str1 = 012345678
Appendix C ++ standard description http://www.cplusplus.com/reference/cstdio/snprintf/
Function <cstdio> snprintf
int snprintf ( char * s, size_t n, const char * format, ... );
Write formatted output to sized bufferComposes a string with the same text that wocould be printed if
FormatWas used on printf, but instead of being printed, the content is stored as
C stringIn the buffer pointed
S(Taking
NAs the maximum buffer capacity to fill ).
If the resulting string wocould be longer
N-1Characters, the remaining characters are discarded and not stored, but counted for the value returned by the function.
A terminating null character is automatically appended after the content written.
After
FormatParameter, the function expects at least as your additional arguments as needed
Format.
Parameters
-
S
-
Pointer to a buffer where the resulting C-string is stored.
The buffer shoshould have a size of at least
NCharacters.
-
N
-
Maximum number of bytes to be used in the buffer.
The generated string has a length of at most N-1, Leaving space for the additional terminating null character.
Size_t is an unsigned integral type.
-
Format
-
C string that contains a format string that follows the same specifications
FormatIn printf (see printf for details ).
-
...
(Additional arguments)
-
Depending on
FormatString, the function may have CT a sequence of additional arguments, each containing a value to be used to replace
Format specifierIn
FormatString (or a pointer to a storage location, N).
There shoshould be at least as your of these arguments as the number of values specified in
Format specifiers. Additional arguments are ignored by the function.
Return ValueThe number of characters that wowould have been written if NHad been sufficiently large, not counting the terminating
Null character.
If an encoding error occurs, a negative number is returned.
Notice that only when this returned value is non-negative and less N, The string has been completely written.
Example
123456789101112131415161718
|
/* snprintf example */#include <stdio.h>int main (){ char buffer [100]; int cx; cx = snprintf ( buffer, 100, "The half of %d is %d", 60, 60/2 ); if (cx>=0 && cx<100) // check returned value snprintf ( buffer+cx, 100-cx, ", and the half of that is %d.", 60/2/2 ); puts (buffer); return 0;}
|
Edit & Run |
Output:
The half of 60 is 30, and the half of that is 15. |
For more examples on formatting see printf.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.