Snprintf/_ snprintf function differences between different platforms

Source: Internet
Author: User
In VC and GCC, strncpy is called in the same form.
Char * strncpy (char * DEST, const char * SRC, size_t N );
Snprintf functions are not defined in the Standard C/C ++. However, in many compilers, vendors provide their implemented versions.
In GCC, the function name is snprintf, and in VC it is called _ snprintf. (Lai banxian Note: several security string functions of snprintf and strncpy have been added to the c99 standard)
Because it is not a standard function and there is no uniform standard to define the function's behavior, the implementation versions of various vendors may be different. Today, we can see the difference, because this small difference is that my program cannot process data normally.
This small difference occurs in the count parameter. In VC, this count is the total number of characters to be written, for example:
// VC
Int main (INT argc, char * Argv [])
...
{
Char buff [100
];
Printf ("% d", _ snprintf (buff, 10, "1234567890ab"
));
Printf ("% s"
, Buff );
Return 0
;
}

// Linxu: gcc/g ++
# Include <stdio. h>
Int main (INT argc, char * Argv [])
...
{
Char buff [100
];
Printf ("% d", snprintf (buff, 10, "1234567890ab"
));
Printf ("% s"
, Buff );
Return 0
;
}
The output of the VC program is:
-1
1234567890 @
The output of the GCC program is:
12
123456789
From the output result, we can know that the Count parameter of _ snprintf in VC indicates that count characters are written to buff, excluding '\ 0' characters,
The '\ 0' character is not added to the end of the string, and if the string exceeds count, the function returns-1 to mark possible errors; gcc
In the snprintf function, the Count parameter indicates that 10 characters are written to the buff, including '\ 0' characters, and the actual string length is returned,
In this example, 12 is used.
If you do not understand the differences between these functions on different platforms, our program may become very vulnerable during the porting process. We should be careful with various
.

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.