https://blog.youkuaiyun.com/unsv29/article/details/83273398 这个链接是《基于C语言sprintf函数的深入理解》
可是不够安全,可能会指针溢出。网上有不少介绍,我也遇到过,所以找了keil里面的函数,找到了
如下:
extern _ARMABI int snprintf(char *__restrict /*s*/, size_t /*n*/,
const char *__restrict /*format*/, ...) __attribute__((__nonnull__(3)));
/*
* is equivalent to fprintf, except that the argument s specifies an array
* into which the generated output is to be written, rather than to a
* stream. The argument n specifies the size of the output array, so as to
* avoid overflowing the buffer.
* A null character is written at the end of the characters written, even
* if the formatting was not completed; it is not counted as part of the
* returned sum. At most n characters of the output buffer are used,
* _including_ the null character.
* Returns: the number of characters that would have been written to the
* array, not counting the terminating null character, if the
* array had been big enough. So if the return is >=0 and <n, then
* the entire string was successfully formatted; if the return is
* >=n, the string was truncated (but there is still a null char
* at the end of what was written); if the return is <0, there was
* an error.
*/
这个函数不错。所以得将程序中的sprintf改为snprintf