C Library Function - snprintf()
Description
snprintf() is a standard C library function used to format and output a string, writing the result to a specified buffer. Unlike sprintf(), snprintf() limits the number of characters output to avoid buffer overflow.
The difference between sprintf() Unlike the function, the snprintf() function provides a parameter size that can prevent buffer overflow. If the length of the formatted string exceeds size-1, snprintf() will only write size-1 characters and add a null character (\0) at the end of the string to indicate its termination.
Declaration
Below is the declaration of the snprintf() function.
int snprintf ( char * str, size_t size, const char * format, ... );
int snprintf(char str, size_t size, const char format, ...) Suppose the variable arguments(...)** according to format formatted into a string, and the string is copied to str in,size is the maximum number of characters to be written; exceeding size will be truncated, with at most size-1 characters written.
Parameters
- str -- the destination string, a pointer to the character array used to store the formatted string.
- size -- the size of the character array.
- format -- the format string.
- ... -- variable arguments, a variable number of arguments formatted according to the format directives in format.
Return Value
The return value is an integer:
-
On success, returns the number of characters that would have been written (not including the terminating null character), even if this value is greater than size.
-
If an encoding error occurs, a negative value is returned.
Example
The following example demonstrates the use of the snprintf() function.
Example
#include
int
main
(
)
{
char
buffer
[
50
]
;
char
s
=
"runoobcom"
;
// read a string and store it in buffer
int
j
=
snprintf
(
buffer,
6
,
"%s\n"
, s
)
;
// output buffer and the character count
printf
(
"string:\n%s\ncharacter count = %d\n"
, buffer, j
)
;
return
0
;
}
The output is:
string:
runoo
character count = 10
The following is an example showing how to use the %f format specifier in snprintf() to output a single-precision floating-point number:
Example
#include
int
main
(
)
{
char
buffer
[
50
]
;
float
x
=
3.1415926
;
int
len
=
snprintf
(
buffer
,
50
,
"x = %f"
,
x
)
;
printf
(
"%s\n"
,
buffer
)
;
printf
(
"Written characters: %d\n"
,
len
)
;
return
0
;
}
The output is:
x = 3.141593
Written characters: 12
In the above example, snprintf() writes the string "x = 3.141593" into the buffer and returns the string length 12. Since the size of buffer is 50, there will be a null character at the end of the string. Finally, the program outputs the string in buffer and the character count returned by snprintf().
Notes:
-
Buffer Size:
snprintf()The function limits the number of characters output, but you need to ensure the passed buffer size is large enough to hold the formatted string. Otherwise, the string may be truncated, resulting in information loss. -
String terminator:
snprintf()The function adds a null character at the end of the buffer as the string terminator, but this character is not counted in the return value. Therefore, when usingsnprintf()to output a string, you need to ensure the buffer size is large enough to hold all the characters of the string plus the terminating null character. -
Format a string: When using
snprintf()When formatting a string, you need to ensure the format specifiers in the format string match the types of the variable arguments. Otherwise, the output may be incorrect. -
Return Value:
snprintf()The function returns the number of characters written to the buffer, not including the terminating null character. If the return value equals the buffer size, it indicates that the output was truncated. -
Variable arguments (varargs):
snprintf()The variable arguments of the function are passed through..., which means you need to pass variable arguments using types that match the format specifiers. When using variable arguments, you should avoid using uninitialized variables, otherwise unpredictable results may occur.
In summary, when using the snprintf() function, you need to pay attention to issues such as buffer size, string terminator, format string, return value, and variable arguments to ensure correct output.
C Standard Library -