sprintf()是标准库的函数。定义在stdio.h头文件中:
1 2 3 4 5 6 7 8 9 |
extern _ARMABI int sprintf(char * __restrict /*s*/, const char * __restrict /*format*/, ...) __attribute__((__nonnull__(1,2))); /* * 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. A null character is written at the end of the characters written; * it is not counted as part of the returned sum. * Returns: the number of characters written to the array, not counting the * terminating null character. */ |
这是一个强大的函数,可以把整型,浮点型数转为字符串。存放在数组里:
1 2 3 4 5 6 |
char str[25]; uint16_t len1 = sprintf(str, "%x", 100); //把100转为十六进制表标的字符串,存放到str里, 返回转换后的字符个数,不包含结尾的空字符。 float time = 12345/1000.0; char msg = "Power On\r\n"; uint16_t len2 = sprintf(str, "[%.3f]%s", time, msg); //输出[12.345]Power On, 这样可以把字符串连接起来。 |
stdio.h位于”C:\Keil_v5\ARM\ARMCC\include\”