CONCAT code在C宏 [英] concat code with macro in C
问题描述
事情是这样的:
比方说,我已经用C定义了两个功能:
Let's say I have two function defined in C:
test_1() {};
test_2() {};
我想有一个宏(例如NUM_TEST),将参照测试号。最好的办法是显示它在code:
I would like to have a macro (e.g. NUM_TEST) that will refer to test number. Best way is to show it in code:
#define NUM_TEST 1
test_1() {};
test_2() {};
int main() {
test_ ## NUM_TEST ## ()
}
我AP preciate,如果有人可以帮助,以找到一个解决方案,如何Concat的函数名宏。
I would appreciate, if someone would help, to find a solution, how to concat name of function with macro.
编辑:
为了使它更清晰。我想仅仅通过不断变化的宏观NUM_TEST改变调用TEST_1之间)和test_2功能(()
To make it more clear. I would like to just by changing of "macro NUM_TEST" change invoked function between test_1() and test_2().
是的,我知道有更容易的方法来做到这一点,但是这仅仅是一个例子,更一般的问题:如何Concat的宏观与C文本不增加新线路或新的宏功能
Yes I know there are more easier ways to do that, but this is just an example to more general problem: How to concat macro with text in C without adding new lines or new macro functions.
编辑2:
很显然,我现在很清楚。比方说,我写了一个程序。它有两个(或更多)的运行类型。我有一个名为NUM_TEST一个宏。通过设置提到宏观到1或2要选择TEST_1()之间test_2运行类型或()
Obviously I was now clear enough. Let's say I wrote a program. It has two (or more) run types. I have one macro called NUM_TEST. By setting mentioned macro to 1 or 2 a want to choose run type between test_1() or test_2()
感谢您!
推荐答案
这是你要找的是什么?
#include <stdio.h>
#define TEST(NUM) test_ ## NUM ()
test_1() { printf ("Hello "); }
test_2() { printf ("World!\n"); }
int main (void)
{
TEST(1);
TEST(2);
// Prints "Hello World!\n"
return 0;
}
这篇关于CONCAT code在C宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!