Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alter function name using #define

Could I implement this in C?

#define X abc

then X_menu(); will be preprocessed as abc_menu();

In another build if I define X def then X_menu(); will be def_menu();

I'm sure there should be a method, but I could not figure out.

like image 409
xian Avatar asked Apr 21 '26 11:04

xian


1 Answers

No, you wouldn't want this behavior as it would be very unsafe and difficult to deal with replacing every X in every name. However, you could use a macro function to do something similar

#define X(F) abc##F
X(_menu();)

## is macro concatenation, so X(_menu();) will expand to abc_menu();

As Roger points out, the macro will also work with the syntax

X(_menu)();

which you may find easier to read

like image 145
Ryan Haining Avatar answered Apr 24 '26 01:04

Ryan Haining



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!