使用 C 预处理器读取变量

发布于 2024-12-10 23:06:47 字数 427 浏览 0 评论 0原文

当尝试使用 C 预处理器进行串联时,我遇到了以下问题:

#define substitute(id) var##id
int main()
{
     int var0 = 999;
     int var1 = 998;
     int var2 = 997;
     int var3 = 996;
     int var4 = 995;

     int i = 0;

     for(i; i < 5; i++)
     {
          printf("Valor: %i \n",  substitute(i));      
     }

     system("PAUSE");
     return 0;

}

有没有办法让预处理器能够读取“i”上的值,而不仅仅是串联“vari” ?

I'm running into the following problem when trying to use concatenation with the C preprocessor:

#define substitute(id) var##id
int main()
{
     int var0 = 999;
     int var1 = 998;
     int var2 = 997;
     int var3 = 996;
     int var4 = 995;

     int i = 0;

     for(i; i < 5; i++)
     {
          printf("Valor: %i \n",  substitute(i));      
     }

     system("PAUSE");
     return 0;

}

Is there a way for the preprocessor to be able to read the value on "i" instead of just concatenating "vari"?

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

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

发布评论

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

评论(8

我ぃ本無心為│何有愛 2024-12-17 23:06:47

不会。预处理器在编译之前工作,因此在执行之前工作。

该定义

#define substitute(id) var##id

将导致循环扩展为:

 for(i; i < 5; i++)
 {
      printf("Valor: %i \n",  vari);      
 }

预处理器不知道也不应该知道变量 i。

您可能应该使用数组:

int var[5] = {999,998,997,996,995};

并通过 [] 访问它:

for(i; i < 5; i++)
{
    printf("Valor: %i \n",  var[i]);      
}

No. The preprocessor works before compilation and therefore before execution.

The define

#define substitute(id) var##id

will cause your loop to expand to:

 for(i; i < 5; i++)
 {
      printf("Valor: %i \n",  vari);      
 }

The preprocessor has no knowledge of the variable i, nor should it.

You should probably use an array:

int var[5] = {999,998,997,996,995};

and access it via []:

for(i; i < 5; i++)
{
    printf("Valor: %i \n",  var[i]);      
}
下壹個目標 2024-12-17 23:06:47

这在预处理器阶段是不可能的,因为您想要的值取决于稍后在运行时才知道的值。

您需要的是一个数组和索引运算符 var[i]

That's not possible at the pre-processor stage because what you want depends on values that are only known later, at runtime.

What you need is an array and the index operator, var[i].

南…巷孤猫 2024-12-17 23:06:47

您需要注意,在编译文件之前,宏会被解析一次(由预处理器),因此在运行时,循环中的每次迭代在“调用”替代时都会呈现相同的结果。

You need to be aware that the macro is resolved once (by the preprocessor) before the file is compiled, so at runtime, each iteration in the loop will render the same result when "calling" substitute.

作妖 2024-12-17 23:06:47

不,因为预处理器在编译时运行,而不是运行时,但您可以使用数组:

int vars[] = { 999, 998, 997, 996, 995 };

for (int i = 0; i < 5; ++i)
    printf("Valor: %i \n", vars[i]);

Nope, because the preprocessor runs at compile time, not runtime, but you can use an array:

int vars[] = { 999, 998, 997, 996, 995 };

for (int i = 0; i < 5; ++i)
    printf("Valor: %i \n", vars[i]);
单身狗的梦 2024-12-17 23:06:47

不,我是运行时评估。预处理器无法知道 I 的值可能是什么。

No i is a runtime evaluation. There is no way for the preprocessor to know what the value of I could be.

迟月 2024-12-17 23:06:47

为什么要使用预处理器来执行此操作?

您似乎正在尝试重新发明数组:

int main() {
    int var[] = {999, 998, 997, 996, 995};
    int i;

    for (i=0; i<5; i++)
        printf("Valor: %i\n", var[i]);
    return 0;
}

Why would you do this with the preprocessor?

You seem to be trying to reinvent arrays:

int main() {
    int var[] = {999, 998, 997, 996, 995};
    int i;

    for (i=0; i<5; i++)
        printf("Valor: %i\n", var[i]);
    return 0;
}
花伊自在美 2024-12-17 23:06:47

正如许多人所说 - 不,宏不知道程序编译时或运行时发生的情况。但是...如果您希望可以使用直接与堆栈操作的宏生成一些黑客代码(不要单独在家中尝试此操作 - 它可能会炸毁您的计算机!!:-))-定义你的宏为:

#define substitute(adr, max, id) *(adr + max - id)

并像这样调用:

printf("Valor: %i \n",  substitute(&var4,4,i));

但请记住,这只是出于好奇,在现实生活中,不建议直接使用堆栈,因为编译器可能(并且通常会)重新排序堆栈上的变量分配,并且你将冒着发生一些令人讨厌的错误等的风险......所以更好其他人说 - 制作一些数组并对其进行操作。

哈!

As many said - no, macros doesn't know anything about what's going on in program compile-time or
run-time. But... if you wish you can generate some hackish code with macro which operates with stack directly (do not try this in home alone - it can blow your computer apart !! :-) )- define your macro as:

#define substitute(adr, max, id) *(adr + max - id)

and call like this:

printf("Valor: %i \n",  substitute(&var4,4,i));

But keep in mind that this is just for curiosity, in real life it is not recommended to play directly with stack, because compiler may (and usually will) re-order variable allocation on stack and also you will risk some nasty bugs to happen and etc... So better as others said - make some array and operate on that.

hth!

十六岁半 2024-12-17 23:06:47

C 宏仅在编译时展开,您的 printf 行将变成

      printf("Valor: %i \n",  vari);      

C macros are only expanded at compile time and your printf line would become

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