Function prototypes
int snprintf (char *str, size_t size, const char *format, ...)
Function
A variable parameter (...) Formats the string as format and then copies it to Str
(1) If the string length < size is formatted, copy the string to str and add a string terminator (' + ') to it;
(2) If the formatted string length >= size, only the (size-1) characters are copied to STR, followed by a string terminator (' + '), and the return value is the length of the string to be written.
return value
Returns the length of the string to be written if successful, or a negative value If an error occurs.
Description
strcpy () sprintf () strcat () has security implications, no cross-border checks, and its corresponding security version is:
strncpy () snprintf () Strncat ()
As an example,
#include <stdio.h> #include <stdlib.h>int main () {char str[10]={0}; int nlen=snprintf (str,sizeof (str), "0123456789012345678"); printf ("str=%s\n", str); printf ("nlen=%d\n", Nlen); return 0;}
Run results
snprintf function Usage