int snprintf (char *restrict buf, size_t N, const char * restrict format, ...);
Function Description: Copy n-1 characters from the source string to the target string, and then add a 0to the back.
function return value: Returns the length of the written string if successful, and returns a negative value if there is an error, noting that the string has been fully written only if the return is nonnegative and less than N.
#include <stdio.h>
#include <stdlib.h>
int main ()
{
Char Str1[10]={0,};
snprintf (str1, sizeof (str), "01234567890123456");
printf ("str1=%s/n", str1);
return 0;
}
Results
str1=012345678
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 this would be printed if
formatwas used on printf, but instead of being printed, the content is stored as a
C StringIn the buffer pointed by
s(Taking
NAs the maximum buffer capacity to fill).
If The resulting string would be longer than
n-1Characters, the remaining characters is discarded and not stored, but counted is the value returned by the function.
A terminating null character is automatically appended after the content written.
After the
formatparameter, the function expects at least as many additional arguments as needed for
format.
Parameters
-
-
S
-
-
Pointer to a buffer where the resulting c-string is stored.
The buffer should has a size of at least
N characters.
-
-
N
-
Maximum number of bytes to is used in the
-
buffer.
The generated string has a length of in 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 this follows the same specifications as
format in printf (see Prin TF for details).
-
-
...
(Additional arguments)
-
-
depending on the
format string, the function could expect a sequence of additional arguments, each Containi Ng a value to being used to replace a
format specifier in the
format string (or a pointer to a storage Location, for N).
There should is at least as many of these arguments as the number of values specified in the
format specifiers. Additional arguments is ignored by the function.
Return valuethe number of characters that would has been written ifnhad been sufficiently large, not counting the terminating
NULL character.
If an encoding The error occurs, a negative number is returned.
Notice This returned value was non-negative and less thannThe string has been completely written.
Example
123456789101112131415161718
|
/* snprintf Example */ #include <stdio.h> int Main () {char buffer [100]; int CX; CX = snprintf (buffer, "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 which 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 NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The snprintf function uses