0

Here's the thing:

Let's say I have two function defined in C:

test_1() {};
test_2() {};

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 ## ()
}

I would appreciate, if someone would help, to find a solution, how to concat name of function with macro.

EDIT:

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().

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.

EDIT 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()

Thank you!

a3f
  • 8,517
  • 1
  • 41
  • 46
Addman
  • 341
  • 1
  • 5
  • 13
  • Why do people like macros? Scared of compilers? They usually end in tears. Only good for a few things – Ed Heal Mar 12 '15 at 21:49
  • Are you trying to specify which function to run based on a pre-processor directive? Why not just use a switch? You might also consider using an #ifdef. – Magister Ludi Mar 12 '15 at 21:50
  • Please read your question and make it reasonable –  Mar 12 '15 at 21:50
  • 1
    `test_ ## NUM_TEST ## ()` needs to go inside another macro defintion. – πάντα ῥεῖ Mar 12 '15 at 21:52
  • Please decide on a language. Is it C or C++? The macro preprocessor is the same but the solution you'd actually want to use is probably different. Your function definitions, by the way, are invalid in either language. – 5gon12eder Mar 12 '15 at 21:54
  • It is C. @EdHeal macros can be great thing if you do power computing (which is my case), to exclude lot of if and switch statements. It requires additional compilation before you want to use it, but it can save lot of time. – Addman Mar 12 '15 at 22:07
  • @Addman - They are not. They lack type safety. They make code harder to read. They make code not maintainable. They make debugging more difficult. It is not power computing. (PS: As a "power" programmer why tag this as C++?) – Ed Heal Mar 12 '15 at 22:15
  • @πάνταῥεῖ - Then the whole thing get incomprehensible. – Ed Heal Mar 12 '15 at 22:17
  • @EdHeal Well, I just addressed the technical issue. If I had thought this would be a _good answer_, then I won't have posted it as a comment :-P ... – πάντα ῥεῖ Mar 12 '15 at 22:20
  • @πάνταῥεῖ - I find it amusing that some people have this love affair with macros. My experience is that some people tend to overuse them – Ed Heal Mar 12 '15 at 22:22
  • @EdHeal I'm strongly discouraging use of macros, and I certainly not have a love affair with them. Though the question is about a simple technical issue with the CPP preprocessor. – πάντα ῥεῖ Mar 12 '15 at 22:25
  • @EdHeal True, I agree, macros can make code pretty unreadable. But sometimes they can help safe time, especially when code lengths are no more than few hundreds of lines. And I taget it as C++ because it is not C or C++ issue, but C preprocessor issue, which are common to both (at least I think so). – Addman Mar 12 '15 at 22:28
  • @πάνταῥεῖ - I did not say that you did have a love affair with them. I think that you have the same mind set as myself. I find some questions on this web site that people are doing stupid things with macros. – Ed Heal Mar 12 '15 at 22:29
  • @Addman - Do the (I assume you meant) save time? Try reading/review the code. Try debugging the code. Try coming back in a year and debug/amend the code. Perhaps those few keystrokes that took an extra minute would have been better. – Ed Heal Mar 12 '15 at 22:31
  • @EdHeal As I said: I agree with you. Using macros is dangerious. In this case we are in angreement. Countless times I was cursing my self that I was not more carefull in writing my code. I am trying to say that sometimes it is not such dum thing to use macros. Obviously we have different opinion in this particular case. – Addman Mar 12 '15 at 22:43
  • Just write a switch statement. Or put the function pointers into an array. – Ed Heal Mar 12 '15 at 22:45
  • Yes that is one of more possibilities. This is just an example that was bothering me for a while (few years). I always solved it otherwise, even with function pointer. (actually I am often blamed for overusage of pointers, which is not great either) – Addman Mar 12 '15 at 22:54
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/72919/discussion-between-addman-and-ed-heal). – Addman Mar 13 '15 at 10:40
  • Can you give a little more context on this problem? Are you using the macro to check some system settings and determining the test to run based on the result? What is determining which test to execute? – Magister Ludi Mar 13 '15 at 14:15

2 Answers2

4

Is this what you're looking for?

#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;
}
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
QuestionC
  • 10,006
  • 4
  • 26
  • 44
  • 2
    Seems a rather silly way of coding (and good luck with debugging) – Ed Heal Mar 12 '15 at 22:03
  • Actually I wanted to do opposite, defining one macro NUM which is 1 or 2, I want to execute function test_1() or test_2(). – Addman Mar 12 '15 at 22:15
0

Rather than creating a Macro that determines this, it is better to just pass command line argument to it which represents the test number. Like this:

#include <stdio.h>

int main( int argc, char *argv[] )  
{
   switch(argv[1])
   {
       case "1"  :
           Test_1();
           break; 
       case "2"  :
           Test_2();
           break; 
       default : 
           printf("Test ID not found");
    }
}

If you are, however, just looking to alias a name, you can have your functions Test_1, Test_2, etc. Then just have a generic TESTTORUN wherever you want the selected test to run. On compilation have the preprocessor replace it with the function name you want:

#define TESTTORUN Test_1  //Or Test_2 or whatever

This will cause the compiler to replace TESTTORUN everywhere in the program with Test_1.

  • This is not a help. Of course I can use switch statement or use whole function name as a macro. But is not my question. I am asking if it is possible to merge macro value with function name somewhere in code. Besides your "switch" code is strange. I should decide between test number by number of arguments? So if I want to use Test_15 I should provide 14 arbitrary words? There is any conversion between arguments (char*) to test number (int). – Addman Mar 15 '15 at 15:09
  • You are right, I wasn't paying enough attention when I wrote my answer and I missed that error on the switch statement, I have corrected it. A reason for doing this would go a long way towards getting better answers. If you simply want to use a macro to determine the test to run or change the test to run at run time, I have given solutions to both of these. Why would you want to change the name of a function based on a macro? What is your goal? You seem to not like arbitrary words being used so is it some optimization problem, cause you didn't mention that in the question. – Magister Ludi Mar 16 '15 at 16:19