库函数

本文介绍控制器内置的C标准库函数,包含两个部分:

  • 数学库(libm):提供三角函数、指数对数、幂运算、取整等浮点运算函数,使用前需要包含头文件 math.h。

  • C标准库(libc):提供内存与字符串操作、字符串转数值、格式化输出等函数,使用前需要包含对应头文件 string.h、stdlib.h、stdio.h。

以下每个函数都按照 函数、描述、参数、返回值、例子 的顺序进行说明。

数学函数(libm)

三角函数

sin

  • 函数

double sin(double x)
  • 描述

计算参数 x 的正弦值,x 的单位是弧度。特殊值:sin(±INF) 或 sin(NaN) 返回 NaN。

  • 参数

x                 弧度制的角度。
  • 返回值

参数 x 的正弦值,取值范围 [-1, 1]。

  • 例子

#include "sys.h"
#include "math.h"

void init(void)
{
}

void loop(void)
{
    double r = sin(M_PI / 2);
    sysPrintf("sin(pi/2) = %f", r);   /* 输出 1.000000 */
}

cos

  • 函数

double cos(double x)
  • 描述

计算参数 x 的余弦值,x 的单位是弧度。特殊值:cos(±INF) 或 cos(NaN) 返回 NaN。

  • 参数

x                 弧度制的角度。
  • 返回值

参数 x 的余弦值,取值范围 [-1, 1]。

  • 例子

#include "sys.h"
#include "math.h"

void init(void)
{
}

void loop(void)
{
    double r = cos(0.0);
    sysPrintf("cos(0) = %f", r);   /* 输出 1.000000 */
}

tan

  • 函数

double tan(double x)
  • 描述

计算参数 x 的正切值,x 的单位是弧度。特殊值:tan(±INF) 或 tan(NaN) 返回 NaN。

  • 参数

x                 弧度制的角度。
  • 返回值

参数 x 的正切值。

  • 例子

#include "sys.h"
#include "math.h"

void init(void)
{
}

void loop(void)
{
    double r = tan(M_PI / 4);
    sysPrintf("tan(pi/4) = %f", r);   /* 输出约 1.000000 */
}

asin

  • 函数

double asin(double x)
  • 描述

计算参数 x 的反正弦值,结果在 [-π/2, π/2] 区间内。特殊值:|x| > 1 返回 NaN,x 为 NaN 返回 NaN 本身。

  • 参数

x                 取值在 [-1, 1] 之间的数值。
  • 返回值

参数 x 的反正弦值,单位是弧度,范围 [-π/2, π/2]。

  • 例子

#include "sys.h"
#include "math.h"

void init(void)
{
}

void loop(void)
{
    double r = asin(1.0);
    sysPrintf("asin(1) = %f", r);   /* 输出约 1.570796 */
}

acos

  • 函数

double acos(double x)
  • 描述

计算参数 x 的反余弦值,结果在 [0, π] 区间内。特殊值:|x| > 1 返回 NaN,x 为 NaN 返回 NaN 本身。

  • 参数

x                 取值在 [-1, 1] 之间的数值。
  • 返回值

参数 x 的反余弦值,单位是弧度,范围 [0, π]。

  • 例子

#include "sys.h"
#include "math.h"

void init(void)
{
}

void loop(void)
{
    double r = acos(-1.0);
    sysPrintf("acos(-1) = %f", r);   /* 输出约 3.141593 */
}

atan

  • 函数

double atan(double x)
  • 描述

计算参数 x 的反正切值,结果在 [-π/2, π/2] 区间内。

  • 参数

x                 任意实数。
  • 返回值

参数 x 的反正切值,单位是弧度,范围 [-π/2, π/2]。

  • 例子

#include "sys.h"
#include "math.h"

void init(void)
{
}

void loop(void)
{
    double r = atan(1.0);
    sysPrintf("atan(1) = %f", r);   /* 输出约 0.785398 */
}

atan2

  • 函数

double atan2(double y, double x)
  • 描述

计算 y/x 的反正切值,并根据 x 和 y 的符号确定结果所在的象限,结果在 [-π, π] 区间内。它比 atan 更适合处理直角坐标到极坐标的转换,因为能正确处理 x 为 0 的情况。

  • 参数

y                 纵坐标数值。
x                 横坐标数值。
  • 返回值

y/x 的反正切值,单位是弧度,范围 [-π, π]。

  • 例子

#include "sys.h"
#include "math.h"

void init(void)
{
}

void loop(void)
{
    double r = atan2(1.0, -1.0);   /* 第二象限 */
    sysPrintf("atan2(1,-1) = %f", r);   /* 输出约 2.356194 */
}

指数与对数

exp

  • 函数

double exp(double x)
  • 描述

计算自然常数 e 的 x 次幂。特殊值:exp(+INF) 返回 +INF,exp(-INF) 返回 0,exp(NaN) 返回 NaN。当 x 过大时结果溢出返回无穷大。

  • 参数

x                 指数。
  • 返回值

e 的 x 次幂。

  • 例子

#include "sys.h"
#include "math.h"

void init(void)
{
}

void loop(void)
{
    double r = exp(1.0);
    sysPrintf("exp(1) = %f", r);   /* 输出约 2.718282 */
}

log

  • 函数

double log(double x)
  • 描述

计算参数 x 的自然对数(以 e 为底)。特殊值:x < 0 (含 -INF)返回 NaN,log(0) 返回 -INF,log(+INF) 返回 +INF,log(NaN) 返回 NaN。

  • 参数

x                 大于 0 的数值。
  • 返回值

参数 x 的自然对数。

  • 例子

#include "sys.h"
#include "math.h"

void init(void)
{
}

void loop(void)
{
    double r = log(M_E);
    sysPrintf("log(e) = %f", r);   /* 输出约 1.000000 */
}

幂与开方

pow

  • 函数

double pow(double x, double y)
  • 描述

计算 x 的 y 次幂,即 x^y。对于整数次幂会返回精确的整数结果(只要结果可表示)。当 x 为负数且 y 不是整数时返回 NaN。

  • 参数

x                 底数。
y                 指数。
  • 返回值

x 的 y 次幂。

  • 例子

#include "sys.h"
#include "math.h"

void init(void)
{
}

void loop(void)
{
    double r = pow(2.0, 10.0);
    sysPrintf("pow(2,10) = %f", r);   /* 输出 1024.000000 */
}

sqrt

  • 函数

double sqrt(double x)
  • 描述

计算参数 x 的平方根,结果经过正确舍入。特殊值:sqrt(-x) (x>0)返回 NaN,sqrt(+INF) 返回 +INF,sqrt(NaN) 返回 NaN。

  • 参数

x                 大于等于 0 的数值。
  • 返回值

参数 x 的平方根。

  • 例子

#include "sys.h"
#include "math.h"

void init(void)
{
}

void loop(void)
{
    double r = sqrt(2.0);
    sysPrintf("sqrt(2) = %f", r);   /* 输出约 1.414214 */
}

取整与取余

ceil

  • 函数

double ceil(double x)
  • 描述

返回不小于 x 的最小整数值,即向上取整。

  • 参数

x                 任意实数。
  • 返回值

不小于 x 的最小整数值(以 double 类型返回)。

  • 例子

#include "sys.h"
#include "math.h"

void init(void)
{
}

void loop(void)
{
    double r = ceil(2.3);
    sysPrintf("ceil(2.3) = %f", r);   /* 输出 3.000000 */
}

floor

  • 函数

double floor(double x)
  • 描述

返回不大于 x 的最大整数值,即向下取整。

  • 参数

x                 任意实数。
  • 返回值

不大于 x 的最大整数值(以 double 类型返回)。

  • 例子

#include "sys.h"
#include "math.h"

void init(void)
{
}

void loop(void)
{
    double r = floor(2.7);
    sysPrintf("floor(2.7) = %f", r);   /* 输出 2.000000 */
}

fmod

  • 函数

double fmod(double x, double y)
  • 描述

计算 x 除以 y 的浮点余数,即 x - n*y,其中 n 是 x/y 向零取整后的商。结果的符号与 x 相同。特殊值:y 为 0、x 非有限值或 y 为 NaN 时返回 NaN。

  • 参数

x                 被除数。
y                 除数。
  • 返回值

x 除以 y 的浮点余数。

  • 例子

#include "sys.h"
#include "math.h"

void init(void)
{
}

void loop(void)
{
    double r = fmod(5.7, 2.0);
    sysPrintf("fmod(5.7,2) = %f", r);   /* 输出 1.700000 */
}

绝对值与符号

fabs

  • 函数

double fabs(double x)
  • 描述

返回参数 x 的绝对值。

  • 参数

x                 任意实数。
  • 返回值

参数 x 的绝对值。

  • 例子

#include "sys.h"
#include "math.h"

void init(void)
{
}

void loop(void)
{
    double r = fabs(-3.14);
    sysPrintf("fabs(-3.14) = %f", r);   /* 输出 3.140000 */
}

copysign

  • 函数

double copysign(double x, double y)
  • 描述

返回一个数值,其绝对值(大小)取自 x,符号取自 y。

  • 参数

x                 提供数值大小的数。
y                 提供符号的数。
  • 返回值

大小为 |x|、符号与 y 相同的数值。

  • 例子

#include "sys.h"
#include "math.h"

void init(void)
{
}

void loop(void)
{
    double r = copysign(5.0, -1.0);
    sysPrintf("copysign(5,-1) = %f", r);   /* 输出 -5.000000 */
}

特殊值判断

isnan

  • 函数

int isnan(double x)
  • 描述

判断参数 x 是否为 NaN (非数值)。

  • 参数

x                 待判断的数值。
  • 返回值

x 为 NaN 返回 1,否则返回 0。

  • 例子

#include "sys.h"
#include "math.h"

void init(void)
{
}

void loop(void)
{
    if (isnan(NAN))
    {
        sysPrintf("NAN detected");
    }
}

finite

  • 函数

int finite(double x)
  • 描述

判断参数 x 是否为有限值(既不是无穷大也不是 NaN)。

  • 参数

x                 待判断的数值。
  • 返回值

x 为有限值返回 1,否则返回 0。

  • 例子

#include "sys.h"
#include "math.h"

void init(void)
{
}

void loop(void)
{
    if (finite(1.0))
    {
        sysPrintf("1.0 is finite");
    }
}

其他

scalbn

  • 函数

double scalbn(double x, int n)
  • 描述

通过直接操作浮点数的指数位来计算 x * 2^n,比实际的乘幂运算更快。结果溢出时返回 ±INF,下溢时返回 ±0。

  • 参数

x                 被缩放的数值。
n                 指数,可正可负。
  • 返回值

x * 2^n 的值。

  • 例子

#include "sys.h"
#include "math.h"

void init(void)
{
}

void loop(void)
{
    double r = scalbn(1.0, 10);   /* 1.0 * 2^10 */
    sysPrintf("scalbn(1,10) = %f", r);   /* 输出 1024.000000 */
}

nextafter

  • 函数

double nextafter(double x, double y)
  • 描述

返回在 x 朝向 y 的方向上,紧邻 x 的下一个可表示的浮点值。用于计算浮点步长等场景。

  • 参数

x                 起始值。
y                 目标方向。
  • 返回值

在 y 方向上 x 的下一个可表示的浮点值;若 x == y 返回 x。

  • 例子

#include "sys.h"
#include "math.h"

void init(void)
{
}

void loop(void)
{
    double r = nextafter(1.0, 2.0);
    sysPrintf("nextafter(1,2) = %.17f", r);   /* 输出比 1.0 稍大的数 */
}

内存操作(string.h)

memset

  • 函数

void *memset(void *s, int c, unsigned long count)
  • 描述

把 s 指向的内存区域的前 count 个字节全部设置为字符 c。

  • 参数

s                 目标内存的起始地址。
c                 要写入的字节值(转换为 unsigned char)。
count             要填充的字节数。
  • 返回值

返回 s 的地址。

  • 例子

#include "sys.h"
#include "string.h"

void init(void)
{
}

void loop(void)
{
    char buf[16];
    memset(buf, 0, sizeof(buf));   /* 把整个缓冲区清零 */
}

memcpy

  • 函数

void *memcpy(void *dest, const void *src, unsigned long count)
  • 描述

从 src 指向的内存区域复制 count 个字节到 dest 指向的内存区域。源与目标区域不能重叠(重叠时请使用 memmove)。

  • 参数

dest              目标内存的起始地址。
src               源内存的起始地址。
count             要复制的字节数。
  • 返回值

返回 dest 的地址。

  • 例子

#include "sys.h"
#include "string.h"

void init(void)
{
}

void loop(void)
{
    char a[8] = {1, 2, 3, 4, 5, 6, 7, 8};
    char b[8];
    memcpy(b, a, sizeof(a));   /* 把 a 复制到 b */
}

memmove

  • 函数

void *memmove(void *dest, const void *src, int count)
  • 描述

从 src 指向的内存区域复制 count 个字节到 dest。与 memcpy 不同,memmove 能正确处理源与目标区域重叠的情况。

  • 参数

dest              目标内存的起始地址。
src               源内存的起始地址。
count             要复制的字节数。
  • 返回值

返回 dest 的地址。

  • 例子

#include "sys.h"
#include "string.h"

void init(void)
{
}

void loop(void)
{
    char buf[8] = {1, 2, 3, 4, 5, 6, 7, 8};
    memmove(buf + 2, buf, 4);   /* 重叠区域的复制,结果 {1,2,1,2,3,4,7,8} */
}

memcmp

  • 函数

int memcmp(const void *cs, const void *ct, unsigned long count)
  • 描述

逐字节比较 cs 和 ct 指向的两块内存区域的前 count 个字节。

  • 参数

cs                第一块内存区域的地址。
ct                第二块内存区域的地址。
count             要比较的字节数。
  • 返回值

若相等返回 0;不等时返回第一个不相同字节的差值(*cs - *ct),可正可负。

  • 例子

#include "sys.h"
#include "string.h"

void init(void)
{
}

void loop(void)
{
    char a[4] = {1, 2, 3, 4};
    char b[4] = {1, 2, 3, 5};
    int r = memcmp(a, b, 4);   /* r < 0,因为第 4 个字节 4 < 5 */
}

memchr

  • 函数

void *memchr(const void *s, int c, int n)
  • 描述

在 s 指向的内存区域的前 n 个字节中查找字符 c 第一次出现的位置。

  • 参数

s                 内存区域的起始地址。
c                 要查找的字节值(转换为 unsigned char)。
n                 要搜索的字节数。
  • 返回值

找到时返回第一次出现 c 的地址;未找到返回 NULL。

  • 例子

#include "sys.h"
#include "string.h"

void init(void)
{
}

void loop(void)
{
    char buf[8] = {1, 2, 3, 4, 5, 6, 7, 8};
    void *p = memchr(buf, 5, 8);   /* p 指向值为 5 的字节 */
}

字符串操作(string.h)

strlen

  • 函数

int strlen(const char *s)
  • 描述

计算字符串 s 的长度(不包括结尾的空字符 '\0')。

  • 参数

s                 待计算长度的字符串。
  • 返回值

字符串的长度(字节数),不包含结尾空字符。

  • 例子

#include "sys.h"
#include "string.h"

void init(void)
{
}

void loop(void)
{
    int len = strlen("hello");
    sysPrintf("len = %d", len);   /* 输出 5 */
}

strnlen

  • 函数

int strnlen(const char *s, unsigned long count)
  • 描述

计算字符串 s 的长度,但最多只检查 count 个字节,即带长度上限的 strlen。

  • 参数

s                 待计算长度的字符串。
count             最多检查的字节数。
  • 返回值

实际长度与 count 中的较小者。

  • 例子

#include "sys.h"
#include "string.h"

void init(void)
{
}

void loop(void)
{
    int len = strnlen("hello world", 5);
    sysPrintf("len = %d", len);   /* 输出 5 */
}

strcpy

  • 函数

char *strcpy(char *dest, const char *src)
  • 描述

把 src 指向的字符串(包括结尾空字符)复制到 dest。目标缓冲区必须足够大,否则可能溢出。

  • 参数

dest              目标字符串缓冲区。
src               源字符串。
  • 返回值

返回 dest 的地址。

  • 例子

#include "sys.h"
#include "string.h"

void init(void)
{
}

void loop(void)
{
    char buf[16];
    strcpy(buf, "hello");
}

strncpy

  • 函数

char *strncpy(char *dest, const char *src, unsigned long count)
  • 描述

把 src 指向的字符串最多 count 个字符复制到 dest。注意:如果 src 的长度小于 count,剩余部分用 '\0' 填充;如果 src 的长度大于等于 count,结果**不会**以空字符结尾。

  • 参数

dest              目标字符串缓冲区。
src               源字符串。
count             最多复制的字符数。
  • 返回值

返回 dest 的地址。

  • 例子

#include "sys.h"
#include "string.h"

void init(void)
{
}

void loop(void)
{
    char buf[8];
    strncpy(buf, "hello world", 7);
    buf[7] = '\0';   /* 由于源长度超过 count,需要手动补空字符 */
}

strcat

  • 函数

char *strcat(char *dest, const char *src)
  • 描述

把 src 指向的字符串追加到 dest 字符串的末尾(覆盖 dest 结尾的空字符),并添加新的结尾空字符。目标缓冲区必须足够大。

  • 参数

dest              目标字符串缓冲区。
src               要追加的源字符串。
  • 返回值

返回 dest 的地址。

  • 例子

#include "sys.h"
#include "string.h"

void init(void)
{
}

void loop(void)
{
    char buf[16] = "hello ";
    strcat(buf, "world");   /* buf 变成 "hello world" */
}

strcmp

  • 函数

int strcmp(const char *cs, const char *ct)
  • 描述

按字典序比较两个字符串 cs 和 ct。

  • 参数

cs                第一个字符串。
ct                第二个字符串。
  • 返回值

cs 等于 ct 返回 0;cs 小于 ct 返回负值;cs 大于 ct 返回正值。

  • 例子

#include "sys.h"
#include "string.h"

void init(void)
{
}

void loop(void)
{
    if (strcmp("abc", "abd") < 0)
    {
        sysPrintf("abc < abd");
    }
}

strchr

  • 函数

char *strchr(const char *s, int c)
  • 描述

在字符串 s 中查找字符 c 第一次出现的位置。

  • 参数

s                 要搜索的字符串。
c                 要查找的字符。
  • 返回值

找到时返回指向该字符的指针;未找到(包括 c 为 '\0' 但未出现)返回 NULL。

  • 例子

#include "sys.h"
#include "string.h"

void init(void)
{
}

void loop(void)
{
    char *p = strchr("hello", 'l');   /* p 指向第一个 'l' */
}

strrchr

  • 函数

char *strrchr(const char *s, int c)
  • 描述

在字符串 s 中查找字符 c 最后一次出现的位置。

  • 参数

s                 要搜索的字符串。
c                 要查找的字符。
  • 返回值

找到时返回指向该字符的指针;未找到返回 NULL。

  • 例子

#include "sys.h"
#include "string.h"

void init(void)
{
}

void loop(void)
{
    char *p = strrchr("hello", 'l');   /* p 指向最后一个 'l' */
}

strstr

  • 函数

char *strstr(const char *s1, const char *s2)
  • 描述

在字符串 s1 中查找子串 s2 第一次出现的位置。

  • 参数

s1                要搜索的字符串。
s2                要查找的子串。
  • 返回值

找到时返回指向子串起始位置的指针;未找到返回 NULL。若 s2 为空串返回 s1。

  • 例子

#include "sys.h"
#include "string.h"

void init(void)
{
}

void loop(void)
{
    char *p = strstr("hello world", "world");   /* p 指向 "world" */
}

strstrlen

  • 函数

char *strstrlen(const char *s1, int count, const char *s2)
  • 描述

在字符串 s1 的前 count 个字符范围内查找子串 s2 第一次出现的位置,是带长度上限的 strstr。

  • 参数

s1                要搜索的字符串。
count             最多搜索的字符数。
s2                要查找的子串。
  • 返回值

找到时返回指向子串起始位置的指针;未找到返回 NULL。

  • 例子

#include "sys.h"
#include "string.h"

void init(void)
{
}

void loop(void)
{
    char *p = strstrlen("hello world", 5, "world");   /* 只在 "hello" 中找,返回 NULL */
}

strspn

  • 函数

int strspn(const char *s, const char *accept)
  • 描述

计算字符串 s 开头连续由 accept 中的字符组成的子串长度。

  • 参数

s                 要检查的字符串。
accept            允许出现的字符集合。
  • 返回值

s 开头连续由 accept 中字符组成的字符个数。

  • 例子

#include "sys.h"
#include "string.h"

void init(void)
{
}

void loop(void)
{
    int n = strspn("123abc", "0123456789");   /* 输出 3 */
}

strcspn

  • 函数

int strcspn(const char *s, const char *reject)
  • 描述

计算字符串 s 开头连续**不包含** reject 中任何字符的子串长度。

  • 参数

s                 要检查的字符串。
reject            不允许出现的字符集合。
  • 返回值

s 开头连续不含 reject 中字符的字符个数。

  • 例子

#include "sys.h"
#include "string.h"

void init(void)
{
}

void loop(void)
{
    int n = strcspn("abc123", "0123456789");   /* 输出 3 */
}

数值转换(stdlib.h)

strtol

  • 函数

long strtol(const char *nptr, char **endptr, int base)
  • 描述

把字符串 nptr 转换为 long 类型的整数。函数会跳过开头的空白字符,允许可选的 + 或 - 号。当 base 为 0 时,根据前缀自动判断进制(0x 为十六进制、0b 为二进制、0 开头为八进制,否则十进制)。

  • 参数

nptr              待转换的字符串。
endptr            若非 NULL,转换结束后 *endptr 指向第一个未转换的字符。
base              进制(2~36),为 0 时自动判断。
  • 返回值

转换后的数值;若没有进行任何转换返回 0;溢出时返回 LONG_MAX 或 LONG_MIN。

  • 例子

#include "sys.h"
#include "stdlib.h"

void init(void)
{
}

void loop(void)
{
    char *end;
    long v = strtol("0x1F", &end, 0);
    sysPrintf("v = %ld", v);   /* 输出 31 */
}

strtoul

  • 函数

unsigned long strtoul(const char *nptr, char **endptr, int base)
  • 描述

把字符串 nptr 转换为 unsigned long 类型的无符号整数,规则同 strtol。

  • 参数

nptr              待转换的字符串。
endptr            若非 NULL,转换结束后 *endptr 指向第一个未转换的字符。
base              进制(2~36),为 0 时自动判断。
  • 返回值

转换后的无符号数值;没有转换返回 0;溢出返回 ULONG_MAX。

  • 例子

#include "sys.h"
#include "stdlib.h"

void init(void)
{
}

void loop(void)
{
    char *end;
    unsigned long v = strtoul("255", &end, 10);
    sysPrintf("v = %lu", v);   /* 输出 255 */
}

strtoull

  • 函数

unsigned long long strtoull(const char *nptr, char **endptr, int base)
  • 描述

把字符串 nptr 转换为 unsigned long long 类型的无符号整数,规则同 strtol。

  • 参数

nptr              待转换的字符串。
endptr            若非 NULL,转换结束后 *endptr 指向第一个未转换的字符。
base              进制(2~36),为 0 时自动判断。
  • 返回值

转换后的无符号数值;没有转换返回 0;溢出返回 ULLONG_MAX。

  • 例子

#include "sys.h"
#include "stdlib.h"

void init(void)
{
}

void loop(void)
{
    char *end;
    unsigned long long v = strtoull("12345678901234", &end, 10);
    sysPrintf("v = %llu", v);
}

strtod

  • 函数

double strtod(const char *string, char **endPtr)
  • 描述

把字符串 string 转换为 double 类型的浮点数。支持可选的正负号、小数点和 E/e 指数形式(如 -1.5E-3)。

  • 参数

string            待转换的字符串。
endPtr            若非 NULL,转换结束后 *endPtr 指向第一个未转换的字符。
  • 返回值

转换后的浮点数值;没有转换返回 0.0。

  • 例子

#include "sys.h"
#include "stdlib.h"

void init(void)
{
}

void loop(void)
{
    char *end;
    double v = strtod("3.14", &end);
    sysPrintf("v = %f", v);   /* 输出 3.140000 */
}

strtold

  • 函数

long double strtold(const char *nptr, char **endptr)
  • 描述

把字符串 nptr 转换为 long double 类型的浮点数,功能等同 strtod,只是返回类型不同。

  • 参数

nptr              待转换的字符串。
endptr            若非 NULL,转换结束后 *endptr 指向第一个未转换的字符。
  • 返回值

转换后的浮点数值(long double 类型)。

  • 例子

#include "sys.h"
#include "stdlib.h"

void init(void)
{
}

void loop(void)
{
    char *end;
    long double v = strtold("2.5e2", &end);   /* v = 250.0 */
}

格式化输出

sprintf

  • 函数

int sprintf(char *s, const char *format, ...)
  • 描述

按照格式串 format 格式化输出到字符串缓冲区 s。缓冲区必须足够大以容纳格式化结果,否则可能溢出,建议改用 snprintf。

  • 参数

s                 输出缓冲区。
format            格式字符串。
...               与格式说明符对应的可变参数。
  • 返回值

写入缓冲区的字符数(不含结尾空字符)。

  • 例子

#include "sys.h"
#include "stdio.h"

void init(void)
{
}

void loop(void)
{
    char buf[32];
    sprintf(buf, "value = %d", 123);
}

Warning

Limitation: 受编译器限制,sprintf不支持浮点(32位/64位)的格式化,如果需要把浮点转成字符串,使用函数sprintf_double()。

snprintf

  • 函数

int snprintf(char *s, size_t count, const char *format, ...)
  • 描述

按照格式串 format 格式化输出到字符串缓冲区 s,最多写入 count 个字符(含结尾空字符),避免缓冲区溢出。s 可以为 NULL,此时不写入任何内容,只返回本应写入的字符数。

  • 参数

s                 输出缓冲区,可为 NULL。
count             最多写入的字符数(含结尾空字符)。
format            格式字符串。
...               与格式说明符对应的可变参数。
  • 返回值

本应写入的字符数(不含结尾空字符)。返回值大于等于 count 表示结果被截断。

  • 例子

#include "sys.h"
#include "stdio.h"

void init(void)
{
}

void loop(void)
{
    char buf[16];
    int n = snprintf(buf, sizeof(buf), "value = %d", 12345);
    sysPrintf("n = %d", n);
}

Warning

Limitation: 受编译器限制,snprintf不支持浮点(32位/64位)的格式化,如果需要把浮点转成字符串,使用函数sprintf_double()。

sprintf_double

  • 函数

int sprintf_double(char *s, int n, int precision, double d)
  • 描述

把浮点数 d 以定点十进制格式转换到字符串缓冲区 s 中,并指定小数位数 precision。

  • 参数

s                 输出缓冲区。
n                 缓冲区最大字节数。
precision         保留的小数位数。
d                 要转换的浮点数。
  • 返回值

写入的字符数(不含结尾空字符)。

  • 例子

#include "sys.h"
#include "stdio.h"

void init(void)
{
}

void loop(void)
{
    char buf[32];
    sprintf_double(buf, sizeof(buf), 2, 3.14159);   /* buf = "3.14" */
}