使用 C 预处理器读取变量
当尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
不会。预处理器在编译之前工作,因此在执行之前工作。
该定义
将导致循环扩展为:
预处理器不知道也不应该知道变量 i。
您可能应该使用数组:
并通过
[]
访问它:No. The preprocessor works before compilation and therefore before execution.
The define
will cause your loop to expand to:
The preprocessor has no knowledge of the variable i, nor should it.
You should probably use an array:
and access it via
[]
:这在预处理器阶段是不可能的,因为您想要的值取决于稍后在运行时才知道的值。
您需要的是一个数组和索引运算符
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]
.您需要注意,在编译文件之前,宏会被解析一次(由预处理器),因此在运行时,循环中的每次迭代在“调用”替代时都会呈现相同的结果。
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.
不,因为预处理器在编译时运行,而不是运行时,但您可以使用数组:
Nope, because the preprocessor runs at compile time, not runtime, but you can use an array:
不,我是运行时评估。预处理器无法知道 I 的值可能是什么。
No i is a runtime evaluation. There is no way for the preprocessor to know what the value of I could be.
为什么要使用预处理器来执行此操作?
您似乎正在尝试重新发明数组:
Why would you do this with the preprocessor?
You seem to be trying to reinvent arrays:
正如许多人所说 - 不,宏不知道程序编译时或运行时发生的情况。但是...如果您希望可以使用直接与堆栈操作的宏生成一些黑客代码(不要单独在家中尝试此操作 - 它可能会炸毁您的计算机!!:-))-定义你的宏为:
并像这样调用:
但请记住,这只是出于好奇,在现实生活中,不建议直接使用堆栈,因为编译器可能(并且通常会)重新排序堆栈上的变量分配,并且你将冒着发生一些令人讨厌的错误等的风险......所以更好其他人说 - 制作一些数组并对其进行操作。
哈!
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:
and call like this:
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!
C 宏仅在编译时展开,您的 printf 行将变成
C macros are only expanded at compile time and your printf line would become