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
.