C macro wrapping - c

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)

Related

Special #define option in C

I have a structure A such that A has fields v0 and v1.
There is an array of A's:
A St[3];
I'd like to define B like that:
#define B[x] St[x].v0
I can't insert x as a parameter to define.
Is there some way to do that in C?
My purpose is to be able to replace each instance of St[x].v0 with B[x].
A macro that substitutes parameters is called a function-like macro, and the C standard only provides for function-like macros using parentheses, not brackets.
You can use below method:
#define B(x) St[x].v0
According to the C reference, the defintion of #define is
#define <identifier>[( parameters, ... )] [replacement-list]
You must not use square brackets within an <identifier> (see C99 standard) and therefore your suggested solution would not be properly processed by the preprocessor.
What would work instead is
#define B(x) St[x].v0 // note the brackets
where you would pass additional parameters to the macro (using parentheses).
However this will obfuscate your code and most probably confuse users/readers of your code. See also why you should be careful with macros.
You can have
#define B(x) St[x].v0
ant then replace each instance of St[x].v0 with B(x).
I'm afraid it's impossible to do it with [], you'd need operator overloading (like the one in C++) for that.

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

What concept is being exhibited in this macro wrapping?

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.

C error: missing whitespace after the macro name

I wrote the following macro:
#define m[a,b] m.values[m.rows*(a)+(b)]
However gcc gives me this error:
error: missing whitespace after the macro name
What is wrong and how do I fix it?
You cannot use [ and ] as delimiters for macro arguments; you must use ( and ). Try this:
#define m(a,b) m.values[m.rows*(a)+(b)]
But note that defining the name of a macro as the name of an existing variable may be confusing. You should avoid shadowing names like this.
I'm not familiar with any C preprocessor syntax that uses square brackets. Change
#define m[a,b] m.values[m.rows*(a)+(b)]
to
#define m(a,b) m.values[m.rows*(a)+(b)]
And it should work.
You cannot have such a macro that will expand when you supply arguments in square brackets. Wherever you got the idea that macros are a smart text-substituting tool, it's just the other way round: macros are extremely obtuse and stupid text-substitution mechanism. What you're trying to do with a macro is absolutely unwarranted - just write a named function.

MACROS - How to implement divide and then round

What is the proper way to perform a divide and round to lower number for a macro?
I am trying to do this:
#define TOTAL_NUM_FFTS (int) NO_SAMPLES / FFT_SIZE
but I am getting a warning of incompaitible redefinition of that macro and the compiler restates the line as:
#define TOTAL_NUM_FFTS(int) NO_SAMPLES / FFT_SIZE without the space between TOTAL_NUM_FFTS and (int).
Thanks for your help!
#define TOTAL_NUM_FFTS ((int) (NO_SAMPLES) / (FFT_SIZE))
The preprocessor thinks (int) is a parameter to the macro.
When defining macros, use as many parentheses as you can. For example, think what will happen if someone defines FFT_SIZE as 2+3. Instead of dividing by 5, you'd be dividing by 2 and then adding 3.
Several things to check:
always properly parenthesize your macros (and macro arguments), as Ilya mentioned
make sure there isn't a duplicate (or near duplicate) definition of the macro somewhere else. The error message should tell you exactly where, but if it doesn't, grep or similar will help (maybe there's an older version of your header hiding in some other directory f the include path?).
make sure your header file is protected against multiple inclusion with include guards. I don't think this is what's happening to you since since identical macro redefinition is supposed to be accepted by a C or C++ compiler, but you should still make sure your header has this.
For dividing, you can use the pow(base, exponent) function available in math.h library.
I suppose you have to implement something like,
#define C A/B //B!=0
then instead of it use the following:
#define C A*pow(B,-1)

Resources