Snprintf usage and a small summary after using it

        snprintf() can be considered as an upgraded version of sprintf(), but it has one more parameter than sprintf(), which can control the length of the string to be written, which is safer. As long as you pay attention, it will not cause buffer overflow. So generally we use snprintf more.

1. Introduction to snprintf function and parameters

#include <stdio.h>

int snprintf(char *str, size_t size, char * format [, argument, …]);

Function: Format the variable parameter “…” into a character string according to the format format, and then copy it to str.

parameter:

  • str: the buff stored in the final formatted string
  • size: The length of the buff buffer area, including the string end character '\0' (note: '\0' is automatically added, regardless of whether the subsequent self string contains '\0') , that is, it can be output to str Valid character sizes are: size - 1
  • format: variable parameter, similar to the "%d" format in printf

Return value: When the call fails, the return value is a negative number. When the call succeeds, the return value is the total length of the formatted string (excluding \0). This string may be truncated (because the length of buf may not be enough to put down the entire string).

After reading this, you may not know how to use it. It doesn’t matter, just look at the example below to understand:

#include <stdio.h>

int main()
{
    int     a = 1;
    char    buf[32] = "Nike";
    char    buf1[32] = {0};
    
    snprintf(buf1, sizeof(buf1)-1, "hello, I am %s. [%d]", buf, a);
    printf("%s\n", buf1);

    return 0;
}

operation result:

2. Some points for attention

1. If the length of the formatted string is < size , copy the entire string to str , and add a string terminator ('\0');

2. If the length of the formatted string is >= size , only (size-1) characters will be copied to str , and a string terminator ('\0') will be added after it, and the return value is the length of the string to be written. (Because snprintf will automatically add a '\0' at the end, you have to leave a place for it).

#include <stdio.h>

int main()
{
    int     a = 1;
    char    buf[10] = "Nike";
    char    buf1[10] = {0};
    
    snprintf(buf1, 5, "%s.", buf);
    printf("%s\n", buf1);

    return 0;
}

result:

If snprintf(buf1, 5, "%s.", buf); is changed to snprintf(buf1, 6, "%s.", buf);

result:

 

3.  snprintf will add \0 at the end , regardless of whether the buf space is enough, so don't worry about buffer overflow.

4. If you want to continuously output strings to a buffer, after each function returns, you need to compare the return value of snprintf and the length of previously output characters with the length of the buffer to avoid buffer overflow. Therefore, when formatting strings and outputting them to the cache, use snprintf as much as possible and use sprintf less , otherwise many strange bugs may appear.

if (n < 0): snprintf error.
if ( n >0 && n < sizeof(buf) ) : snprintf succeeds and the full string is formatted.
if ( n >= sizeof(buf) ) : snprintf succeeds, but the string to be formatted is truncated.

おすすめ

転載: blog.csdn.net/qq_51368339/article/details/129891922
おすすめ