嵌入式系统中strcmp sscanf 和sprintf 的替代品是什么?

What is the replacement of strcmp sscanf and sprintf in embedded system?

在嵌入式系统上工作,了解到 sprintfsscanfstrcmp 不被推荐。

  1. sscanf

    在某些情况下,我必须从字符串中解析数字和其他重要数据。此外,字符串可能包含不同类型的数据。

    嵌入式系统中 sscanf 的建议替代品是什么?

  2. sprintf

    类似于sscanf sprintf也是必需的。

  3. strcmp,strstr

    项目中需要 strcmp 和 strstr。

    在嵌入式系统中,memcpy 是否优于 strcmp?

在处理字符串时要牢记哪些主要要点?

char array[10]="data",copy[10];
for(int i=0;i<10 && array[i]!='[=10=]';i++)
   copy[i]=array[i];

这也是 strcpy 的替代品吗?以上代码的可持续性如何

不知道是谁告诉你strcmpstrstr是“不推荐”的,但事实并非如此

如果您使用 printf 和 scanf 系列函数编写具有 16kB FLASH 的非常小的 uC,那就太昂贵了(没有浮点数和长支持的 printf 是 5-7kB)。对于这种微型 - 编写您自己的函数或使用微型 printf 实现 - 您在网上有很多可用的。而不是 scanf 只需编写适合您的应用程序的简单解析器。

您想编写自己的版本strcpy

所以不是这个:

char array[10]="data",copy[10];
for(int i=0;i<10 && array[i]!='[=10=]';i++)
   copy[i]=array[i];

你会得到这样的东西:

// This sticks to specification from the standard C library.
// For your needs you may want something slightly different
// for example controlling the maximum size of the destination buffer

char *strcpy(char *to, const char *from)
{
    char *dest = to;
    while (*to++ = *from++) ;

    return dest;
}
...
char array[10]="data",copy[10];
strcpy(copy, array);