如何通过宏声明变量?

发布于 2024-11-28 03:59:07 字数 636 浏览 3 评论 0原文

首先,我使用MS的Visual Studio并使用C语言。

最近我需要用一个相同的语句来声明变量,就像宏一样。

然而,如您所知,我只能声明一个具有相同名称的变量。

例如,这是不可能的。

int iVar1;
int iVar1; // this is not possible. 

所以我想到了宏包括 __LINE__ ,如果我可以使用这个预定义的宏,我可以通过一个宏语句声明很多变量。

但制作起来很困难。

我做了这样的宏。

#define MY_LINE_VARIABLE        int g_iLine##__LINE__##Var = 0;

但编译后,我可以得到这个名为“g_iLine_LINE_Var”的变量,而不是“g_iLine123Var”

我想知道这是可能的,我该如何做到这一点。

此外,如果可能的话,我需要使用 __FILE__ 宏。但这个宏可能会随着字符串数据而改变。所以我不能确定。

任何建议都会有所帮助。

提前感谢您的帮助。

first of all, I'm using MS's Visual Studio and using C language.

Recently I need to declare variables with just one same statement which likes a macro.

However as you know, I can declare just one variable which have same name.

for example, this is not possible.

int iVar1;
int iVar1; // this is not possible. 

so I thought about macros include __LINE__ , if I can use this predefined macro, I can declare lots of variables via just one macro statement.

But it was difficult to make.

I made macro like this.

#define MY_LINE_VARIABLE        int g_iLine##__LINE__##Var = 0;

but after compile, i could get this variable named 'g_iLine_LINE_Var' instead of 'g_iLine123Var'

I want to know that is this possile, and how can i make it.

Furthermore, I need to use __FILE__ macro if possible. but this macro might be changed with string data. so I can not be sure.

Any advice will be helpful.

Thank you for your help in advance.

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

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

发布评论

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

评论(2

蔚蓝源自深海 2024-12-05 03:59:07

正如 @Chris Lutz 所说,可能有更好的方法来实现你想要的。考虑询问你想要实现什么目标。

但如果您只是好奇,可以这样做:

#define var(z) int g_iLine##z##var = 0
#define decl(x) var(x)
#define MY_LINE_VARIABLE        decl(__LINE__)
MY_LINE_VARIABLE;
MY_LINE_VARIABLE;

As @Chris Lutz has rightly said that, there might be a better way to accomplish what you want. Consider asking what you want to achieve.

But if you are just curious, this is the way to do:

#define var(z) int g_iLine##z##var = 0
#define decl(x) var(x)
#define MY_LINE_VARIABLE        decl(__LINE__)
MY_LINE_VARIABLE;
MY_LINE_VARIABLE;
坐在坟头思考人生 2024-12-05 03:59:07

来自链接:

预处理器展开宏名称后,宏的定义
body 附加到剩余输入的前面,并且检查
宏调用继续。因此,宏体可以包含调用
到其他宏。

因此,在您的情况下:

MY_VARIABLE_LINE 转换为 int g_iLine__LINE__Var;。但现在 __LINE__ 不再是有效的标记,并且不再被视为预定义的宏。

Aditya 的代码是这样工作的:

MY_VARIABLE_LINE 转换为 decl(__LINE__) ,再转换为 var(123) ,再转换为 int giLine123var = 0

编辑:这是针对 GNU C 的

From this link :

After the preprocessor expands a macro name, the macro's definition
body is appended to the front of the remaining input, and the check
for macro calls continues. Therefore, the macro body can contain calls
to other macros.

So in your case :

MY_VARIABLE_LINE is converted to int g_iLine__LINE__Var;. But now __LINE__ is not a valid token anymore and is not treated as a predefined macro.

Aditya's code works like this:

MY_VARIABLE_LINE is converted to decl(__LINE__) which is converted to var(123) which is converted to int giLine123var = 0.

Edit: This is for GNU C

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