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.
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
Related
This question already has an answer here:
Macro expansion and stringification: How to get the macro name (not its value) stringified using another macro?
(1 answer)
Closed 8 months ago.
I am wondering if there is anyway to stringify a macro expanision that is another macro before it gets completley expanded. Easier to show in code:
#define A_MACRO 0
#define ANOTHER_MACRO A_MACRO
I want to expand ANOTHER_MACRO into the string "A_MACRO"
Double stringification does not work, it stringifies ANOTHER_MACRO into 0. I have searched and cannot find an answer and played around with macros in a test application, but I have had no luck. Is it possible?
Edit: I do not have the ability to change either macro. A_MACRO has a descriptive name that I would like to use, and we "point" to it with another macro that follows a standard name and I can grab it with a file parser. I was hoping I could just write a stringification macro to get that name, but I think I will have to find a way to latch onto it with my file parser, thanks everyone for your help.
No, there isn't. You can either not expand the macro argument, or you can fully expand it.
There may be a solution to whatever the underlying problem is, but this is a dead end.
So I encountered the following code in a library for C for RF communication :
#define __COMB(a,b,c) (a##b##c)
#define _COMB(a,b,c) __COMB(a,b,c)
Which is basically used to create various names for constants and functions. It just concatenates the parameters.
What I don't get is the 2nd line. Is there a reason to wrap the macro?
If it's just a naming issue, why not just write :
#define _COMB(a,b,c) (a##b##c)
The library can be found here.
Thanks in advance!
This is a hack to trick the C preprocessor to pre-expand the parameters before they're concatenated. This kind of decoupling is required in certain some situations, for example if __COMB is to be used in a preprocessor macro parameter stringification.
Double macro is used to make sure, that it will work even when parameters are also macros.
Without double macro we would get XXYYZZ instead of xyz:
#define XX x
#define YY y
#define ZZ z
_COMB(XX, YY, ZZ)
A bunch of code just got handed over to me and I got baffled by macros in the header. I could not understand what they are for:
#define WRAPPER_MACRO(symbol) symbol
#define ANOTHER_SYMBOL WRAPPER_MACRO(ANOTHER_SYMBOL)
#define PREFIXED_ANOTHER_SYMBOL WRAPPER_MACRO(PFX_ANOTHER_SYMBOL)
Why do this? What is the benefit?
Edit: This is not the actual verbatim code from my codebase but it has the same template. I just replaced macro names.
As #Michael said, we'll need to see the real macros to know for sure. But without them, I'm willing to take a few guesses that might help you out.
The macro nesting is probably a stringification thing. This bit of code is from a codebase I maintain:
// As per http://gcc.gnu.org/onlinedocs/cpp/Stringification.html:
// "If you want to stringify the result of expansion of a macro argument, you
// have to use two levels of macros."
#ifndef STRINGIFY
#define STRINGIFY(s) TOSTRING(s)
#define TOSTRING(s) #s
#endif
I'm also guessing your PREFIXED_ANOTHER_SYMBOL macro is doing something similar to this, using the # or ## preprocessor directives to prepend a certain symbol to whatever you feed the macro.
Currently, I am using:
#define p printf(
In order to reduce the amount of characters I use within printf's throughout the program, e.g, instead of:
printf("Hello, World.");
I can do:
p"Hello, World.");
My question is, is it possible to further shorten this? E.g, adding the ending brace into the define? Something like (pseudo-code) :
#define p printf()
Or some such? Or even a shorter way of doing the #define?
Edit: I'd better clarify before I get downvoted into oblivion. The point of this is not to save keystrokes because I am lazy, or because I want to make it unreadable. I have already written the program in question (which is quite readable and hopefully won't get me put in programmers hell), and I am curious as to how low I can get the char count. This question would assist in doing so.
You can use macros with variadic arguments:
#define p(...) printf(__VA_ARGS__)
Even though a name such as p is not a good idea, this syntax can very well be used with conditional logging. For example:
#ifndef NDEBUG
# define log(...) fprintf(stderr, __VA_ARGS__)
#else
# define log(...) ((void)0)
#endif
Sometimes, you may want to do something specific with the format specifier. For that, you can take advantage of a gcc extension (and possibly other compilers) for example such as the following:
#define log(fmt, ...) fprintf(stderr, "Log: "fmt"\n", ##__VA_ARGS__)
Note that ##__VA_ARGS__ is not standard. See this answer also.
Yes your macro can take an argument
http://gcc.gnu.org/onlinedocs/cpp/Macro-Arguments.html
That is evil. Stop trying to save keystrokes and improve your typing skill instead. It is not an appropriate use of macros to "simplify" code in this manner, because you will simply make your code harder to read for ordinary C programmers. Always keep readability in mind.
Be happy you don't have to write System.out.println instead.
Based on your demonstrated need, consider puts
This would be safer/better to macro-ize, since it doesn't take a variable number of arguments. You've taken printf and limited it use. Also, a user will type p("%d") and the program will blow up.
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.
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;
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