0

I need to concatenate strings using macros to generate function names.

#define CONCAT(a,b,c) a ## b

int i=1;
CONCAT(a,i)

This code gives ai as a result, while what I wanted is a1.

As there are many functions in my source code, I don't want to enumerate them.

My goal:

for(int i=0;i<100;i++)
{
    Funi1();//here i should be from  0 to one hundred 
    Funi2();
    Funi3();
    Funi4();
    ..
}

@Potatoswatter

I have written a script to expand it and the output file cost serval hundred lines. @Eric Finn

7
  • 7
    The replacement happens if preprocessing, before the compiler has any concept of a variable i with the value 1. What are you actually trying to write? Commented Jun 7, 2013 at 12:46
  • 2
    I suggest you to read up on how (and when) the preprocessor works... Commented Jun 7, 2013 at 12:47
  • Please explain why you need a set of functions defined with macros, instead of a single function with an integer parameter. The way you described, it's not convincing that you really need what you're trying to do. Commented Jun 7, 2013 at 14:24
  • @hdante I want to avoid to write hardcode like that Fun11,Fun12,Fun13,..Fun21.. Therefore, I want to write macro to get the function name. In javascript, we can use eval convert string to command. However, in c language, I only find macro can work like that. Commented Jun 7, 2013 at 14:36
  • possible duplicate of C preprocessor and concatenation Commented Jun 7, 2013 at 15:32

4 Answers 4

8

It is not possible since macros are expanded during pre-processing stage. So, it can not take the value of a variable and concat.

Sign up to request clarification or add additional context in comments.

8 Comments

@HotLicks What do you mean? If you mean a #define that won't work.
@Hot Licks How can I achieve it?
@mwerschy For there are hundred functions I need to concatenate, I don't want to enumerate them in my source code.
@YulongTian Why are you telling me that? And why do you have so many functions called foo1 ... foo999? You may want to rethink your code...
These functions is generated by ecmangen(a tool for writing manifest file of etw) and I can't control it.
|
0

what about __COUNTER__ predefined macro, you have it on GCC and VC. Does it help you?

#include <stdio.h>
#define FUNC2(x,y) x##y
#define FUNC1(x,y) FUNC2(x,y)
#define FUNC(x) FUNC1(x,__COUNTER__)

int FUNC(my_unique_prefix);
int FUNC(my_unique_prefix);

int main() {
   my_unique_prefix0 = 0;
   printf_s("\n%d",my_unique_prefix0);
   my_unique_prefix0++;
   printf_s("\n%d",my_unique_prefix0);
}

Example from here

1 Comment

Things that only work once, then break, suck. Boost Preprocessor offers reusable mechanisms, but it's hard to help if we don't know what the goal is.
0

you can pre-compile it with: gcc -E sourcecode.c and watch it, it just replace variable name, and not get value and calculate at all, so it's not possible to let it works like you want.

Comments

0

try it with the LINE macro, it gives u numbers :) if u dont care about 1+1+1+1 like static int FUNCADD(once,LINE)=1;if(FUNCADD(once,LINE)>0)

if that worx for u

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.