The returned value of the snprintf function in C

Source: Internet
Author: User
Tags function prototype int size truncated


Today's special record.

The function prototype of snprintf is:
Int snprintf (char * str, size_t size, const char * format ,...);

Note:
Previously, I thought that the returned value of snprintf is the length actually written to the str string.

Case 1: If the length of the string to be output is <size, = is not included here, because snprintf will automatically add it to str,

The returned value of snprintf is the actual str length.

Case 2: If the length of the string to be output is greater than or equal to size, it indicates that the length of str is not enough to write the original length, then the return value of snprintf

In ideal conditions (that is, the length of str is long enough), so the return value may be greater than or equal to size.

In addition, snprintf will automatically append '& prime; to the end of str. The Returned values of snprintf do not include' & prime;

This is clearly written in manual:

If the output was truncated due to this limit then the return
Value is the number of characters (not including the trailing '') which wowould have been written to the final string if enough space had been available.
Thus, a return value of size or more means that the output was truncated. (See also below under NOTES.) If an output error is encountered, a negative value
Is returned.

Let's look at several examples.

Result1 (recommended)


# Include <stdio. h>
# Include <stdlib. h>

Int main ()
{
Char str [10] = {0 ,};
Snprintf (str, sizeof (str), "0123456789012345678 ");
Printf ("str = % s/n", str );
Return 0;
}

Root]/root/lindatest
$./Test
Str= 012345678


Result2: (not recommended)


# Include <stdio. h>
# Include <stdlib. h>

Int main ()
{
Char str [10] = {0 ,};
Snprintf (str, 18, "0123456789012345678 ");
Printf ("str = % s/n", str );
Return 0;
}

Root]/root/lindatest
$./Test
Str= 01234567890123456


Test the returned values of the snprintf function:


# Include <stdio. h>
# Include <stdlib. h>

Int main ()
{
Char str1 [10] = {0 ,};
Char str2 [10] = {0 ,};
Int ret1 = 0, ret2 = 0;
Ret1 = snprintf (str1, sizeof (str1), "% s", "abc ");
Ret2 = snprintf (str2, 4, "% s", "aaabbbccc ");
Printf ("aaabbbccc length = % d/n", strlen ("aaabbbccc "));
Printf ("str1 = % s, ret1 = % d/n", str1, ret1 );
Printf ("str2 = % s, ret2 = % d/n", str2, ret2 );
Return 0;
}

[Root]/root/lindatest
$./Test
Aaabbbccc length = 9
Str1 = abc, ret1 = 3
Str2 = aaa, ret2 = 9


Explanation SIZE:


# Include <stdio. h>
# Include <stdlib. h>
Int main ()
{
Char dst1 [10] = {0,}, dst2 [10] = {0 ,};
Char src1 [10] = "aaa", src2 [15] = "aaabbbcccddd ";
Int size = sizeof (dst1 );
Int ret1 = 0, ret2 = 0;
Ret1 = snprintf (dst1, size, "str: % s", src1 );
Ret2 = snprintf (dst2, size, "str: % s", src2 );
Printf ("sizeof (dst1) = % d, src1 = % s,/" str: % s/"= % s, dst1 = % s, ret1 = % d/n ", sizeof (dst1), src1," str: ", src1, dst1, ret1 );
Printf ("sizeof (dst2) = % d, src2 = % s,/" str: % s/"= % s, dst2 = % s, ret2 = % d/n ", sizeof (dst2), src2," str: ", src2, dst2, ret2 );
Return 0;
}
Root]/root/lindatest
$./Test
Sizeof (dst1) = 10, src1 = aaa, "str: % s" = str: aaa, dst1 = str: aaa, ret1 = 8
Sizeof (dst2) = 10, src2 = aaabbbcccddd, "str: % s" = str: aaabbbcccddd, dst2 = str: aaab, ret2 = 17


In addition, the returned value of snprintf is the length of the string to be written, rather than the degree of actually written string. For example:
Char test [8];
Int ret = snprintf (test, 5, "1234567890 ");
Printf ("% d | % s/n", ret, test );

The running result is:
10 | 1234

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.