This question already has answers here:
How can I concatenate twice with the C preprocessor and expand a macro as in "arg ## _ ## MACRO"?
(3 answers)
Closed 4 years ago.
I have something like :
#define NBR 42
#define THE_ANS_IS theAnsIsNBR
Currently the second macro is expand as 'theAnsIsNBR' as expected, but i want it to be expand as 'theAnsIs42' i am not sure if it is even possible !?
#define Paste(x, y) x##y
#define Expand(x, y) Paste(x, y)
#define NBR 42
#define THE_ANS_IS Expand(theAnsIs, NBR)
#define _CONCAT(x,y) x ## y
#define CONCAT(x,y) _CONCAT(x,y)
#define NBR 42
#define THE_ANS_IS CONCAT(theAnsIs, NBR)
This works because ## concatenates two tokens. The problem is, they aren't expanded first. But calling another macro on them expands them, therefore you need to nest two function-like macros here.
Related
This question already has answers here:
How to force macro to not expand
(1 answer)
Pre-processor: Expand a macro only once
(3 answers)
C: is it possible to expand marcos before pasting, but not recursively?
(1 answer)
Closed 9 months ago.
Given macro definitions like this:
#define GPIOA ((1234)+42)
#define GPIOB ((1234)+84)
#define LEDA_GPIO_Port GPIOA
#define LEDB_GPIO_Port GPIOB
#define MY_PORT_GPIOA 0
#define MY_PORT_GPIOB 1
I would like to define a macro MY_PORT which, given LEDA as argument somehow substitutes this to LEDA_GPIO_Port, then to GPIOA, turns that into MY_PORT_GPIOA and this into 0. So MY_PORT(LEDA) should evaluate to 0 and MY_PORT(LEDB) to 1. If the macros GPIOA/GPIOB wouldn't exist, this would be easy, but since they are defined as complex expressions (starting with parentheses) only one substitution must be performed, and the occurence of GPIOA must not be substituted by that expression.
I tried
#define MY_PORT2(port) MY_PORT_##port
#define MY_PORT(name) MY_PORT2(name##_GPIO_Port)
but here MY_PORT(LEDA) returns MY_PORT_LEDA_GPIO_Port, i.e. it lacks one substituion.
#define CONCAT(a,b) a##b
#define MY_PORT2(port) CONCAT(MY_PORT,port)
#define MY_PORT(name) MY_PORT2(name##_GPIO_Port)
does not compile, as it tries to concatenate the definition for GPIOA, i.e. one subtitution too many.
Is it even possible to do exactly one subtitution?
This question already has answers here:
How to compare strings in C conditional preprocessor-directives
(14 answers)
Closed 2 years ago.
The following structure for C preprocessor directive #if works well.
#define C 1
#if C==1
...
#endif
But I'd like to use something like this:
#define REAL double
#if REAL==double
...
#elif REAL==float
...
#else
assert(0);
#endif
It's not valid. Is there a solution?
Is there a solution?
There is a whole world of ways you can combine macro expansion with conditional compilation. It's unclear which of them you would consider a solution, but here's a possibility:
#define doubledouble 1
#define floatfloat 1
#define concat_literal(x,y) x ## y
#define realtype(t1,t2) concat_literal(t1,t2)
// ...
#define REAL float
#if realtype(REAL, double)
// REAL is double
#elif realtype(REAL, float)
// REAL is float
#else
// REAL is something else
#endif
The realtype macro works by macro-expanding its arguments and concatenating them together. On rescan, if the result is doubledouble or floatfloat then that is expanded further to 1. If it is an identifier that is not defined as a macro name, then the #if treats it as 0.
It's not foolproof, of course. It does not work if REAL has a multi-token expansion, such as long double. There's also a minor risk of collision with other defined macro names.
With reference to this question Quote macro for defining string, Idea is that I want to print the value of a macro constant, say for the following code:
#include<stdio.h>
#define X 4
#define Tostring(x) #x
main()
{
printf(Tostring(X));
}
It is printng X instead of 4, but with Quote macro for defining string I know how to correct it, but can someone explain what exactly is gong on here?
To print 4 as you wish you can one of these:
Stringizing the result:
#include<stdio.h>
#define Tostring(x) str(x)
#define str(x) #x
#define X 4
int main()
{
printf(Tostring(X));
}
or using function like macros to convert X to int:
#include<stdio.h>
#define X 4
#define Tostring(x)(X)
int main()
{
printf("%d\n", Tostring(X));
}
The first example is one of the examples that is given in documentation. There it is stated that two levels of macros shall be used if you want to stringize the result of the expansion of a macro. The result of the expansion is the following:
Tostring (X)
-> Tostring(4)
-> str (4)
-> "4"
In the second example you are relating the argument of Tostring macro with the X from the #define X 4 and thus you can print the value as an int.
This question already has answers here:
C macros and use of arguments in parentheses
(2 answers)
Closed 3 years ago.
I have a doubt in the output of a code. The code uses preprocessors of c language. The code is given below,
#include <stdio.h>
#define sqr(x) x*x
int main() {
int x = 16/sqr(4);
printf("%d", x);
}
The result of sqr(4) is equal to 16. So, the value in x must be 1 (16/16=1). But if I print the value of x, the output is 16. Please explain me why this is happening.
I am attaching the screenshot of output pane.
Output Window
In C, macros are filled into your code upon compilation. Let's consider what happens in your case:
Your macro is:
#define sqr(x) x*x
When it gets filled into this:
int x = 16/sqr(4);
You would get:
int x = 16/4*4;
Operator precedence being equal for / and *, this gets evaluated as (16/4)*4 = 16, that is, from left to right.
However, if your macro were this:
#define sqr(x) (x*x)
then you would get:
int x = 16/(4*4);
... and that reduces to 1.
However, when the argument for your macro is more complex than a simple number, e.g. 2+3, it would still go wrong. A better macro is:
#define sqr(x) ((x)*(x))
After macro replaces your expression, it becomes int x = 16/4*4 and is evaluated accordingly. i.e. (16/4)*4 which is 16.
sqr(x) doesn't calculate 4*4, it is not a function.
You are actually doing 16/4*4 which is in fact 16.
You can change your define to
#define sqr(x) (x*x)
Now you will be doing 16/(4*4).
It is expanded to
int x = 16/4*4;
Something like
#define sqr(x) ((x) * (x))
would be more safe, precedence and associativity-wise, still works poorly in expressions like sqr(a++). Inline functions are simpler to use in this context.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C preprocessor and concatenation
I have the macro
#define BUS B
I want to make macro BUS_PORT that expands to PORTB.
I did following:
#define BUS_PORT PORT ## BUS
But BUS_PORT expands to PORTBUS. What I did wrong? How to make it right?
As explained in this answer, you need an extra level of indirection. E.g.
#define BUS B
#define PASTER(x,y) x ## y
#define EVALUATOR(x,y) PASTER(x,y)
#define BUS_PORT EVALUATOR(PORT, BUS)