C 预处理器中的 strlen ?

发布于 2024-10-17 21:25:32 字数 221 浏览 5 评论 0原文

是否可以在C预处理器中实现strlen()

给定:

#define MYSTRING "bob"

是否有一些预处理器宏,X,它可以让我说:

#define MYSTRING_LEN X(MYSTRING)

Is it possible to implement strlen() in the C preprocessor?

Given:

#define MYSTRING "bob"

Is there some preprocessor macro, X, which would let me say:

#define MYSTRING_LEN X(MYSTRING)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

柠北森屋 2024-10-24 21:25:32

它不使用预处理器,但 sizeof 在编译时解析。如果您的字符串位于数组中,则可以在编译时使用它来确定其长度:

static const char string[] = "bob";
#define STRLEN(s) (sizeof(s)/sizeof(s[0]))

请记住上面的 STRLEN 将包含 null 终止符,这与 strlen().

It doesn't use the preprocessor, but sizeof is resolved at compile time. If your string is in an array, you can use that to determine its length at compile time:

static const char string[] = "bob";
#define STRLEN(s) (sizeof(s)/sizeof(s[0]))

Keep in mind the fact that STRLEN above will include the null terminator, unlike strlen().

最美不过初阳 2024-10-24 21:25:32

你可以这样做:

#define MYSTRING sizeof("bob")

在我的机器上显示 4,因为末尾添加了 null。

当然这仅适用于字符串常量。


使用 MSVC 16 (cl.exe -Wall /TC file.c):

#include "stdio.h"
#define LEN_CONST(x) sizeof(x)

int main(void)
{
    printf("Size: %d\n", LEN_CONST("Hej mannen"));

    return 0;
}

输出:

Size: 11

字符串的大小加上 NUL 字符。

You can do:

#define MYSTRING sizeof("bob")

That says 4 on my machine, because of the null added to the end.

Of course this only works for a string constant.


Using MSVC 16 (cl.exe -Wall /TC file.c) this:

#include "stdio.h"
#define LEN_CONST(x) sizeof(x)

int main(void)
{
    printf("Size: %d\n", LEN_CONST("Hej mannen"));

    return 0;
}

outputs:

Size: 11

The size of the string plus the NUL character.

紫罗兰の梦幻 2024-10-24 21:25:32

是的:
#define MYSTRING_LEN(s) strlen(s)

在大多数编译器中,这将为常量参数生成一个编译时常量...并且您不能做得更好。

换句话说:你不需要宏,只需使用 strlen 即可;编译器足够聪明,可以为您完成这项工作。

Yes:
#define MYSTRING_LEN(s) strlen(s)

In most compilers, this will produce a compile-time constant for a constant argument ... and you can't do better than that.

In other words: you dont need a macro, just use strlen; the compiler is smart enough to do the work for you.

谁与争疯 2024-10-24 21:25:32

一般来说,C 预处理器实际上并不转换任何数据,它只是替换它。这意味着,如果您使用实现(功能性)持久数据结构的数据污染了 C 预处理器命名空间,您也许能够执行此类操作。

也就是说,您确实不想这样做,因为一旦您传入字符串以外的其他内容,整个“添加”功能就会彻底失败。 C 预处理器没有数据类型的概念,也没有内存取消引用的概念(如果您想要存储在变量中的字符串的长度,则很有用)。基本上,这将是一个有趣的“看看你能走多远”的练习,但最终,你会得到一个 MYSTRING_LEN ,它只会带你到达目标一小段距离。

此外,C 预处理器缺乏名称空间意味着这样的宏扩展系统将是不可包含的。人们必须注意防止生成的名称干扰其他有用的宏。最后,您可能会耗尽预处理器中用于任何重要用途的内存,因为预处理器并不是真正构建来保存要转换为“单位”标记的每个字符的名称,以及名称每个“单位”标记被压缩为其最终的十进制表示法。

Generally the C pre-processor doesn't actually transform any data, it only replaces it. This means that you might be able to perform such an operation provided that you pollute your C pre-processor namespace with data implementing (functional) persistent data structures.

That said, you really don't want to do this as the entire "added" functionality will fail spectacularly once you pass in something other than a string. The C pre-processor has no concept of data type, nor does it have the concept of memory de-referencing (useful if you wanted the length of a string stored in a variable). Basically, it would be a fun "see how far you could take it" exercise, but in the end, you would have a MYSTRING_LEN which would only take you a short distance to the goal.

In addition, the C pre-processor's lack of name spaces means that such a macro expansion system would not be containable. One would have to take care to keep the generated names from interfering with other useful macros. In the end, you would probably run out of memory in the pre-processor for any significant use, as the pre-processor isn't really built to hold a name for each character being converted into the "unit" token, and a name for each "unit" token being compressed into its final decimal notation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文