Linux and Windows snprintf differentiate __linux

Source: Internet
Author: User

Today, when you use the snprintf function, you think:
strcpy, strncpy
strcmp, strncmp
Strcat, Strncat
sprintf, snprintf

Most like to use is snprintf, because it will automatically add '/0 ' in the back. Read it on the Internet. The original VC _snprintf is not like this.

VC in the _SNPRINTF function does not follow this rule, it is not large enough output buffer will not output the end of the '/0 ' (similar to the behavior of the strncpy). So to make the above program work properly, you must make the appropriate changes.

Char buf[5];
_snprintf (BUF, 5, "This is a test string."); BUF becomes "This" and buf[4] is '
BUF[4] = 0; BUF[4] is '/0 ' now. To be safe, manually add the
  
_snprintf (buf, 6, "This is a test string."); Error:buffer Overflow
_snprintf (BUF, 5, "abc"); BUF becomes "ABC", the value of buf[3] is '/0 ', buf[4] is undefined.

If you want to ensure portability, you have to follow the VC's writing, do one for the standard is very superfluous "fill 0" operation, and then through the definition of a macro to distinguish between the platform selected snprintf or _snprintf.

A small test code:
You can see that there are two different places:
1>. End of '/0 ' processing.
2>. function return value is not the same.

# include < stdio. H>
# include < string. H>

# define Str_len 32

# ifdef _WIN32
# define SNPRINTF _snprintf
# endif

/**
* Dump string
*/
static void Dump_str (const char * title, const char * str, int len)
{
int i;

printf ("%s[%d].%p/n", title, Len, str);
for (i = 0; i < len; i+ +)
printf ("<%c,%02x>", str[i], (unsigned char) str[i]);

printf ("n");
}

int main (int argc, char * * argv)
{
int t;
Char str1[Str_len], str2[Str_len];

strcpy (str1, "123456789");
Dump_str ("str1", str1, Str_len);

t = snprintf (str2, Str_len, "%s", str1);
Dump_str ("str2", str2, Str_len);
printf ("snprintf () ret.%d/n", t);

t = snprintf (STR2, 5, "%s", str1);
Dump_str ("str2", str2, Str_len);
printf ("snprintf () ret.%d/n", t);

return (0);
}

/*
Test ENV:
GCC version 3.4.5 20051201 (Red Hat 3.4.5-2)

Result:
str1[32].0xbffb9190
<1,31><2,32><3,33><4,34><5,35><6,36><7,37><8,38><9,39> <,00><?fb><?bf><?d5><?82><,04><,08><,00><,00><,00> <,00><,00><,00><,00><, 00><?c8><?91><?fb><?bf><& 26><?85><,04><,08>
str2[32].0xbffb9170
<1,31><2,32><3,33><4,34><5,35><6,36><7,37><8,38><9,39> <,00><w,77><,01><, 00><?92><?fb><?bf><?f8><d,64><?ad ><,00><,00><,00><,00><,00><,00><,00><,00><,00><, 00 ><,00><,00><,00>
snprintf () ret.9
str2[32].0xbffb9170
<1,31><2,32><3,33><4,34><,00><6,36><7,37><8,38><9,39> <,00><w,77><,01><, 00><?92><?fb><?bf><?f8><d,64><?ad ><,00><,00><,00><,00><,00><,00><,00><,00><,00><, 00 ><,00><,00><,00>
snprintf () ret.9

-------------------------------------------------------------------------
Test ENV:
Microsoft Windows XP [version 5.1.2600]
Microsoft (R) 32-bit/C + + optimizing Compiler Version 12.00.8168 for 80x86


Result:
Str1[32].0012ff60
<1,31><2,32><3,33><4,34><5,35><6,36><7,37><8,38><9,39> <,00><,12><,00><,10><,1d><@,40><,00><,00><,08><, 00 ><,00><,04><,00><,00><,00><,00><,00><,00><, 00&GT;&LT;?E0 ><,1e><@,40><,00>
str2[32].0012ff3c
<1,31><2,32><3,33><4,34><5,35><6,36><7,37><8,38><9,39> <,00><?93><|,7c><& #63733;,ff><& #63733;,ff><& #63733;,ff>< & #63733;,ff><?eb><,06><?93><|,7c><?ef><0,30><@,40><,00> <,00><,00><7,37><,00><,09><,00><,00><,00>
snprintf () ret.9
str2[32].0012ff3c
<1,31><2,32><3,33><4,34><5,35><6,36><7,37><8,38><9,39> <,00><?93><|,7c><& #63733;,ff><& #63733;,ff><& #63733;,ff>< & #63733;,ff><?eb><,06><?93><|,7c><?ef><0,30><@,40><,00> <,00><,00><7,37><,00><,09><,00><,00><,00>
snprintf () ret.-1
*/

http://blog.csdn.net/locape/article/details/5657378


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.