English 中文(简体)
C 加工剂中的透镜?
原标题:strlen in the C preprocessor?

能否在<>C 预处理器上实施<代码>strlen(?

鉴于:

#define MYSTRING "bob"

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

#define MYSTRING_LEN X(MYSTRING)
最佳回答

它没有使用预处理器,但规模在编纂时得到解决。 如果你身处一个阵列,你可以用来确定在汇编时间上的长短:

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

铭记<代码>STRLEN 以上将包含无效术语,与<代码>strlen(>不同。

问题回答

你可以做到:

#define MYSTRING sizeof("bob")

这在我的机器上说4点,因为那末倒数了。

当然,这只是为了不断加强。


(cl.exe - 所有文件/TC文件。 因此:

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

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

    return 0;
}

产出:

Size: 11

护卫舰的规模加上联军的性质。

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

在大多数汇编者中,这将产生一个不断编辑的固定时间,以便不断提出论点......而且你能够做得好于此。

换言之,你急切需要一种宏观的、公正的使用透镜;汇编者有足够的智慧为你工作。

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.

尽管如此,你真的不想这样做,因为如果你在座外的某个地方通过的话,整个“附加”功能就会明显失败。 C 加工商没有数据类型的概念,也没有记忆消减的概念(如果你希望储存在变数中的体积长度,则使用)。 基本上,这只是“看你能在多大程度上做”的工作,但归根结底,你将拥有一项战略,即: 仅能把你带离目标很远的地方。

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.





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...