C library functions -snprintf()
C Standard Library - <stdio.h>
Description
snprintf() is a C language standard library function used to format and output a string, and write the result to a specified buffer. Unlike sprintf(), snprintf() limits the number of characters output to avoid buffer overflow.
andsprintf()The difference is that the snprintf() function provides a parameter, size, which 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 mark 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, ...)Let the variable arguments be set(...)according toformatFormats into a string and copies the string tostrAmong them,sizeThe maximum number of characters to be written; if it exceedssizeit will be truncated, and at most size-1 characters will be written.
Parameter
- str-- The destination string, a pointer to a 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 formatting directives in format.
Return Value
The return value is an integer:
-
If successful, returns the number of characters that would have been written (excluding 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 usage of the snprintf() function.
Example
int main()
{
char buffer[50];
char* s = "examplecom";
// Read the string and store it in buffer.
int j = snprintf(buffer, 6, "%s\n", s);
// Output buffer and character count.
printf("string:\n%s\ncharacter count = %d\n", buffer, j);
return 0;
}
The output result is:
string: runoo character count = 10
Here is an example demonstrating how to use%fThe format specifier for outputting a single-precision floating-point number in snprintf():
Example
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 result is:
x = 3.141593 Written characters: 12
In the above example, snprintf() writes the string "x = 3.141593" into the buffer and returns the length of the string, which is 12. Since the size of the buffer is 50, there will be a null character at the end of the string. Finally, the program outputs the string in the buffer and the number of characters returned by snprintf().
Notes:
buffer size:
snprintf()The function limits the number of characters output, but you need to ensure that the size of the passed-in buffer is large enough to accommodate the formatted string. Otherwise, the string may be truncated, leading to 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()When outputting a string, you need to ensure that the buffer size is sufficient to hold all the characters of the string as well as the trailing null character.Format string: When using
snprintf()When formatting a string, you need to ensure that the format specifiers in the format string match the types of the variable arguments. Otherwise, the output result may contain errors.Return Value:
snprintf()The return value of the function is the number of characters written to the buffer, excluding the null character at the end of the string. If the return value equals the size of the buffer, it indicates that the output result has been truncated.Variable arguments:
snprintf()The variable arguments of the function are passed via...This means that variable arguments need to be passed 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 the output result is correct.
other extensions